Brain Phrye
code cooking diy fiction reviews personal politics tools

Zsh completion for home grown scripts

I use irssi on a cloud server for my IRC needs. Along with it I’ve written some utility scripts - the most useful is kibo to find all messages sent to myself by searching the irssi logs.

Since my home dir is a bit longer lived my scripts keep getting little tweaks and improvements. One I’ve been doing lately is completions. I had hoped yesterday to show a more complicated zsh completion example but so far haven’t gotten it to work.

So instead I’ll show one in progress. I know I’ve likely done the _list functions wrong as well as the last two args to _regex_words but not fully clear how I’m supposed to do it. For some reason I find completion docs (for tcsh, bash and zsh) hard to read. No idea why. Any tips welcome!

‘kibo’ completion script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#compdef kibo
local -a reply nick channel
_nick_list() {
local -a nicks
nicks=($(find ~/.irssi/log/* -name '[a-zA-Z0-9]*.log' |
sed 's@.*/@@;s/.log//' | sort -u))
_describe -t nick-list nickname nicks "$@"
}
nick=(
/$'[^\0]#\0'/ ':nick-list:nickname:_nick_list'
)
_channel_list() {
local -a channels
channels=($(find ~/.irssi/log/* -name '#*.log' |
sed 's/.*#//;s/.log//' | sort -u))
_describe -t channel-list 'channel name' channels "$@"
}
channel=(
/$'[^\0]#\0'/ ':channel-list:channel name:_channel_list'
)
_regex_words \
commands "kibo flags" \
'-h:help' \
'-c:add context' \
'@*:search for nick:$nick' \
'#*:search channel:$channel'
echo "Loading: ${reply[*]}"
_regex_arguments _kibo_util "${reply[@]}"
_kibo() {
echo "hi: '$*'"
_kibo_util "$@"
}
_kibo "$@"

kibo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
file=~/.irssi/log/ilug/\#linux.log
lines=-10
pat=$USER
for arg in "$@"; do
case "$arg" in
-h|--help)
cat $HOME/bin/kibo
exit
;;
-c)
grepargs="-C 3"
;;
-*)
lines="$arg"
;;
@*|/*)
pat="${arg#[@/]}"
;;
\#*)
file=~/.irssi/log/*/${arg}.log
;;
*)
file=~/.irssi/log/*/*${arg}*.log
;;
esac
done
egrep -a $grepargs "$pat" $file | tail $lines