-
Notifications
You must be signed in to change notification settings - Fork 2
/
_lux
67 lines (52 loc) · 1.72 KB
/
_lux
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#compdef lux
#autoload
# Completion function for 'lux'
# https://github.com/vapniks/zsh-completions/blob/master/zsh-completions-howto.org
# https://unix.stackexchange.com/questions/325647/how-to-add-autocomplete-for-path-to-custom-completion-function-in-zsh
local -a items
local -a modes
# _lux_load_items: Load array of items (1st arg.)
# * 1. List all loaded function names: compgen -A 'function'
# * 2. GREP ^_lux_set_ (and discard that part, \K), keep the rest, sort.
# e.g. _lux_set_macos -> ( macos iterm )
# _lux_set_iterm
_lux_load_items() {
items=( $(compgen -A 'function' | grep -oP "^_lux_set_\K[^ ]+" | sort) )
}
# _lux_load_modes_for_item: Load array of modes (2nd arg.) for an item $1
# * If LUX_$1_EXTRAS is defined, append to default modes
# e.g. LUX_MACOS_EXTRAS not defined
# modes for 'macos' -> ( light dark )
# LUX_ITERM_EXTRAS="blue white"
# modes for 'iterm' -> ( light dark blue white )
_lux_load_modes_for_item() {
modes=( light dark ) # default
local item=$1
# Extra modes: variable name (uppercase))
# e.g. LUX_ITERM_EXTRAS
typeset -u extras_var
local extras_var="LUX_${item}_EXTRAS"
# Extra modes: variable value read into array
# e.g. "blue white" -> ( blue white )
local -a modes_extras
IFS=' ' read -A modes_extras <<< "${(P)${extras_var}}"
# Append extra modes (nothing if none)
modes+=($modes_extras)
}
# Main completion function
# lux <item> <mode>
_lux() {
local state
_arguments '1: :->item' '2: :->mode'
case $state in
item)
_lux_load_items
_describe 'lux item' items
;;
mode)
_lux_load_modes_for_item "$words[2]"
_describe 'lux mode' modes
;;
esac
}
_lux "$@"