src/klish-plugin-infix: fixes to 'dir' and 'show log' commands

These changes are designed to make the experience working with log files
easier.  Both listing available log files and viewing them.

 - replace C dir@infix with a shell script for easier extensions,
   e.g., we want to show both available .cfg and log files
 - add support for gunzipping .gz log files before viewing them

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-08-26 20:54:49 +02:00
parent a44f83c127
commit 6d03e7ef3c
3 changed files with 45 additions and 19 deletions
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
dir()
{
path=$1
if [ -z "$COLUMS" ]; then
TTY=$(resize)
eval "$TTY"
fi
printf "\033[7m%-*s\033[0m\n" "$COLUMNS" "$path directory"
if [ -d "$path" ]; then
ls -p "$path/"
else
echo "Directory $path does not exist."
fi
echo
}
if [ -d "$1" ]; then
dir "$1"
else
dir "$HOME"
dir "/cfg"
dir "/log"
fi
-14
View File
@@ -300,19 +300,6 @@ done:
return files("/cfg", ".cfg");
}
int infix_dir(kcontext_t *ctx)
{
cd_home(ctx);
fputs("\e[7mHome directory \e[0m\n", stderr);
system("ls --color=always -p >&2");
fputs("\n\e[7m/cfg directory \e[0m\n", stderr);
system("ls --color=always -p /cfg >&2");
return 0;
}
int infix_erase(kcontext_t *ctx)
{
kpargv_t *pargv = kcontext_pargv(ctx);
@@ -760,7 +747,6 @@ int kplugin_infix_init(kcontext_t *ctx)
kplugin_add_syms(plugin, ksym_new("copy", infix_copy));
kplugin_add_syms(plugin, ksym_new("commit", infix_commit));
kplugin_add_syms(plugin, ksym_new("datastore", infix_datastore));
kplugin_add_syms(plugin, ksym_new("dir", infix_dir));
kplugin_add_syms(plugin, ksym_new("erase", infix_erase));
kplugin_add_syms(plugin, ksym_new("files", infix_files));
kplugin_add_syms(plugin, ksym_new("ifaces", infix_ifaces));
+18 -5
View File
@@ -172,8 +172,13 @@
<ACTION sym="copy@infix" in="tty" out="tty" interrupt="true"/>
</COMMAND>
<COMMAND name="dir" help="List available configuration files">
<ACTION sym="dir@infix"/>
<COMMAND name="dir" help="List available files">
<SWITCH name="optional" min="0">
<PARAM name="path" ptype="/STRING" help="Directory contents to list"/>
</SWITCH>
<ACTION sym="script" in="tty" out="tty" interrupt="true">
dir $KLISH_PARAM_path
</ACTION>
</COMMAND>
<COMMAND name="remove" help="Remove a configuration file">
@@ -388,11 +393,19 @@
</COMMAND>
</SWITCH>
<ACTION sym="script">
file=${KLISH_PARAM_fn:-syslog}
file=/log/${KLISH_PARAM_fn:-syslog}
case "$file" in
*.gz)
cmd=zcat
;;
*)
cmd=cat
;;
esac
if [ -n "$KLISH_PARAM_lines" ]; then
tail -n $KLISH_PARAM_lines /log/$file
$cmd "$file" | tail -n $KLISH_PARAM_lines
else
cat /log/$file
$cmd "$file"
fi
</ACTION>
</COMMAND>