-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bash_commacd
195 lines (177 loc) · 5.12 KB
/
.bash_commacd
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# commacd - a faster way to move around (Bash 3+).
# https://github.com/shyiko/commacd
#
# ENV variables that can be used to control commacd:
# COMMACD_CD - function to change the directory (by default commacd uses builtin cd and pwd)
# COMMACD_NOTTY - set it to "on" when you want to suppress user input (= print multiple matches and exit)
#
# @version 0.1.0
# @author Stanley Shyiko <[email protected]>
# @license MIT
# turn on case-insensitive search by default
shopt -s nocaseglob
_commacd_split() { echo "$1" | sed $'s|/|\\\n/|g' | sed '/^[[:space:]]*$/d'; }
_commacd_join() { local IFS="$1"; shift; echo "$*"; }
_commacd_expand() ( shopt -s extglob nullglob; local ex=($1); printf "%s\n" "${ex[@]}"; )
_command_cd() {
local dir=$1
if [[ -z "$COMMACD_CD" ]]; then
builtin cd "$dir" && pwd
else
$COMMACD_CD "$dir"
fi
}
# show match selection menu
_commacd_choose_match() {
local matches=("$@")
for i in "${!matches[@]}"; do
printf "%s\t%s\n" "$i" "${matches[$i]}" >&2
done
local selection;
read -e -p ': ' selection >&2
if [[ -n "$selection" ]]; then
echo -n "${matches[$selection]}"
else
echo -n "$PWD"
fi
}
_commacd_forward_by_prefix() {
local path="${*%/}/" IFS=$'\n'
# shellcheck disable=SC2046
local matches=($(_commacd_expand "$(_commacd_join \* $(_commacd_split "$path"))"))
case ${#matches[@]} in
0) echo -n "$PWD";;
*) printf "%s\n" "${matches[@]}"
esac
}
# jump forward (`,`)
_commacd_forward() {
if [[ -z "$*" ]]; then return 1; fi
local IFS=$'\n'
local dir=($(_commacd_forward_by_prefix "$@"))
if [[ "$COMMACD_NOTTY" == "on" ]]; then
printf "%s\n" "${dir[@]}"
return
fi
if [[ ${#dir[@]} -gt 1 ]]; then
dir=$(_commacd_choose_match "${dir[@]}")
fi
_command_cd "$dir"
}
# search backward for the vcs root (`,,`)
_commacd_backward_vcs_root() {
local dir="$PWD"
while [[ ! -d "$dir/.git" && ! -d "$dir/.hg" && ! -d "$dir/.svn" ]]; do
dir="${dir%/*}"
if [[ -z "$dir" ]]; then
echo -n "$PWD"
return
fi
done
echo -n "$dir"
}
# search backward for the directory whose name begins with $1 (`,, $1`)
_commacd_backward_by_prefix() {
local prev_dir dir="${PWD%/*}" matches match IFS=$'\n'
while [[ -n "$dir" ]]; do
prev_dir="$dir"
dir="${dir%/*}"
matches=($(_commacd_expand "$dir/${1}*/"))
for match in "${matches[@]}"; do
if [[ "$match" == "$prev_dir/" ]]; then
echo -n "$prev_dir"
return
fi
done
done
# at this point there is still a possibility that $1 is an actual path (passed in
# by completion or whatever), so let's check that one out
if [[ -d "$1" ]]; then echo -n "$1"; return; fi
# otherwise fallback to pwd
echo -n "$PWD"
}
# replace $1 with $2 in $PWD (`,, $1 $2`)
_commacd_backward_substitute() {
echo -n "${PWD/$1/$2}"
}
# choose `,,` strategy based on a number of arguments
_commacd_backward() {
local dir=
case $# in
0) dir=$(_commacd_backward_vcs_root);;
1) dir=$(_commacd_backward_by_prefix "$@");;
2) dir=$(_commacd_backward_substitute "$@");;
*) return 1
esac
if [[ "$COMMACD_NOTTY" == "on" ]]; then
echo -n "${dir}"
return
fi
_command_cd "$dir"
}
_commacd_backward_forward_by_prefix() {
local dir="$PWD" path="${*%/}/" matches match IFS=$'\n'
if [[ "${path:0:1}" == "/" ]]; then
# assume that we've been brought here by the completion
dir=(${path%/}*)
printf "%s\n" "${dir[@]}"
return
fi
while [[ -n "$dir" ]]; do
dir="${dir%/*}"
# shellcheck disable=SC2046
matches=($(_commacd_expand "$dir/$(_commacd_join \* $(_commacd_split "$path"))"))
case ${#matches[@]} in
0) ;;
*) printf "%s\n" "${matches[@]}"
return;;
esac
done
echo -n "$PWD"
}
# combine backtracking with `, $1` (`,,, $1`)
_commacd_backward_forward() {
if [[ -z "$*" ]]; then return 1; fi
local IFS=$'\n'
local dir=($(_commacd_backward_forward_by_prefix "$@"))
if [[ "$COMMACD_NOTTY" == "on" ]]; then
printf "%s\n" "${dir[@]}"
return
fi
if [[ ${#dir[@]} -gt 1 ]]; then
dir=$(_commacd_choose_match "${dir[@]}")
fi
_command_cd "$dir"
}
_commacd_completion() {
local pattern=${COMP_WORDS[COMP_CWORD]} IFS=$'\n'
# shellcheck disable=SC2088
if [[ "${pattern:0:2}" == "~/" ]]; then
# shellcheck disable=SC2116
pattern=$(echo ~/"${pattern:2}")
fi
local completion=($(COMMACD_NOTTY=on $1 "$pattern"))
if [[ "$completion" == "$PWD" || "${completion// /\\ }" == "$pattern" ]]; then
return
fi
# remove trailing / (if any)
for i in "${!completion[@]}"; do
completion[$i]="${completion[$i]%/}";
done
COMPREPLY=($(compgen -W "$(printf "%s\n" "${completion[@]}")" -- ''))
}
_commacd_forward_completion() {
_commacd_completion _commacd_forward
}
_commacd_backward_completion() {
_commacd_completion _commacd_backward
}
_commacd_backward_forward_completion() {
_commacd_completion _commacd_backward_forward
}
alias ,=_commacd_forward
alias ,,=_commacd_backward
alias ,,,=_commacd_backward_forward
complete -o filenames -F _commacd_forward_completion ,
complete -o filenames -F _commacd_backward_completion ,,
complete -o filenames -F _commacd_backward_forward_completion ,,,