Brain Phrye

code cooking diy fiction personal photos politics reviews tools


Zsh completion for home grown scripts (part 2)

When last we checked, our intrepid lazy typer was trying to write a completion rule for kibo to reduce his keystrokes a microscopic amount.

The working ‘kibo’ completion script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#compdef kibo
_whowhere() {
local -a nicks channels
nicks=($(find ~/.irssi/log/* -name '[a-zA-Z0-9]*.log' -size +500c |
sed 'sX.*/X@X;s/.log//' | grep -v '[_|!]' | sort -u))
channels=($(find ~/.irssi/log/* -name '#*.log' -size +500c |
sed 's/.*#/#/;s/.log//' | grep -v '[_|!]' | sort -u))
_values "Who and where to find" "${nicks[@]}" "${channels[@]}"
}
_kibo() {
_alternative \
'args:custom arg:((-h\:"help" -c\:"add context"))' \
'whowhere:Who and where to find:_whowhere'
}
_kibo "$@"

Amusingly it’s way, way shorter than the one that didn’t work. Not an uncommon occurrence really.

The key issue was that I kept thinking of # and @ as “flag markers” like -. But it was just easier to treat them as _values with my custom flag characters prepended.