-
Notifications
You must be signed in to change notification settings - Fork 1
/
__diraction-dispatch
97 lines (88 loc) Β· 3.96 KB
/
__diraction-dispatch
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#compdef _diraction-dispatch # -*- mode: sh -*-
# completion for the dispatcher command
# words: 1:function 2:directory 3:pattern
local diract=$words[2]
local dirdir=`eval echo $diract`
local subcommand=$words[3]
# Update Pwd to make subcompleter to consider we are in the target directory
local OLDPWD=$PWD
local PWD=$dirdir
if (( CURRENT == 3 ));
then # Subcomand completion
case $subcommand in
//*) # SubPattern :D
local nslash=$(echo $subcommand | grep -o / |tr -d '\n' | wc -c)
local dirpattern=''; for n ({1..$(($nslash-1))}); do dirpattern="$dirpattern*/"; done
local pattern=$(echo "${subcommand#//}" | sed -e "s:\\(.\\):\\.*\\1\\.*:g")
local cmd="setopt null_glob; cd $dirdir && ls -d -- $dirpattern $dirpattern*/ $dirpattern*/*/ 2>>/dev/null| sed -E 's:^:/:g' | grep \"$pattern\""
local matches="$(eval $cmd 2>/dev/null)"
local paths; if [[ -n "$matches" ]]; then paths=("${(f)matches}"); else paths=(); fi
if [[ ${#paths} -gt 0 ]] && [[ -n ${paths[1]} ]]; then
compadd -S '' -X "Matching Nested Subdirs" -U ${paths[@]} # -U no to filter non matching values
return 0
else
_message "No matching nested subdir"
return 1
fi
;;
/*) # Pattern
local nslash=$(echo $subcommand | grep -o / |tr -d '\n' | wc -c)
local dirpattern=''; for n ({1..$nslash}); do dirpattern="$dirpattern*/"; done
local pattern=`eval echo '${subcommand%/*}/$(echo ${subcommand##'$dirpattern'} | sed -e "s:\\(.\\):\\.*\\1\\.*:g")'`
local cmd="cd $dirdir && ls -d -- $dirpattern 2>>/dev/null | sed -E 's:^:/:g' | grep \"$pattern\" "
local matches="$(eval $cmd 2>/dev/null)"
local paths; if [[ -n "$matches" ]]; then paths=("${(f)matches}"); else paths=(); fi
if [[ ${#paths} -gt 0 ]] && [[ -n "${paths[1]}" ]]; then
compadd -S '' -X "Matching Subdirs" -U ${paths[@]}# -U no to filter non matching values
return 0
else
_message "No matching subdir"
return 1
fi
;;
*) # Basic argument
_describe -t commands "Dispatcher subcommand" _DIRACTION_HELP_SUBCOMMAND
compadd -x "Whitelisted commands" $DIRACTION_DISPATCH_WHITELIST
# maybe extend subcommands with these value??
# or extend the array here?
return
esac
elif [[ "$subcommand" =~ "^/." ]]; then
dirdir="$dirdir$subcommand"
shift words; shift words; shift words
((CURRENT--))
words=(__diraction-dispatch $dirdir "$words[@]") __diraction-dispatch
return 0
else # argument completion
shift words; shift words # shifting words args
((CURRENT = CURRENT -2))
case $subcommand in
e|"exec"|[-,_]) # magic native completion
shift words; ((CURRENT--))
if ((CURRENT == 1 )); then
_command_names
elif functions "_${words[1]}" > /dev/null; then
cd $PWD && "_${words[1]}" ; cd $OLDPWD
else
_path_files -W $PWD ; _options_set
fi ; return 0
;;
i|interactive|prompt|shell|help)
_message "Just hit enter, and enter the interactive mode :)" && return 0;;
/)
_directories -W "$PWD"
;;
*)
local compfun="_${subcommand}"
if functions "$compfun" > /dev/null; then
# Β§tofix: git error: zsh:12: command not found: ___diraction-dispatch_main # Β€old
# if so do it up
cd $PWD; $compfun ; ret=$? ; cd $OLDPWD; [ $ret -eq 0 ] && return 0
# beware to file completion:
# https://github.com/robbyrussell/oh-my-zsh/issues/2394
else
_message "No Specific completion available for $subcommand"
fi
;;
esac
fi