-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path_sd
54 lines (48 loc) · 1.47 KB
/
_sd
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
#compdef sd
function __list_commands {
local -a subcmds
local line dir next help helpfile command
dir="$1"
# the parens after the glob contains "glob qualifiers" (see man zshexpn):
# * executable
# - follow symlinks
# , "or" separator
# / directories
for file in "$dir"/*(-*,/); do
command=$(basename "$file")
if [[ -d "$file" ]]; then
helpfile="$file/help"
if [[ -f "$helpfile" ]]; then
help=$(head -n1 "$helpfile")
else
help="$command commands"
fi
else
helpfile="$file.help"
if [[ -f "$helpfile" ]]; then
help=$(head -n1 "$helpfile")
else
help=$(sed -nE -e '/^#!/d' -e '/^#/{s/^# *//; p; q;}' "$file")
fi
fi
subcmds=($subcmds "$command:\"$help\"")
done
_arguments -C ": :(($subcmds))" "*::arg:->args"
next="$dir/$line[1]"
if [[ ! -z $line[1] && -d "$next" ]]; then
__list_commands "$next"
elif [[ ! -z $line[1] && -x "$next" ]]; then
# You could imagine wanting to customize the '*:file:_files' fallback
# at some point, but I'm not going to worry about that for now.
_arguments -A '-*' \
'--help[print help text]' \
'--cat[print script contents]' \
'--which[print script path]' \
'--edit[open script for editing]' \
'--really[suppress special argument handling]' \
'*:file:_files'
elif [[ ! -z $line[1] && ! -f "$next" ]]; then
_arguments "--new[create a new script]"
fi
}
__list_commands ${SD_ROOT:-"$HOME/sd"}