-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd.sh
executable file
·271 lines (245 loc) · 5.48 KB
/
d.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env bash
d() {
local -r D_NAME="${D_NAME:-${FUNCNAME[0]}}"
local -r D_CONFIG_DIR="${D_CONFIG_DIR:-$HOME}"
local -r D_FAV_DIRS_FILE="${D_FAV_DIRS_FILE:-$D_CONFIG_DIR/.d}"
if [[ ! -v D_SELECT_ONE ]]; then
local -ra D_SELECT_ONE=('fzf' '--tac')
fi
if [[ ! -v D_SELECT_MANY ]]; then
local -ra D_SELECT_MANY=('fzf' '--tac' '--multi')
fi
if [[ ! -v D_PARENTS ]]; then
local -ri D_PARENTS=0
else
local -ri D_PARENTS=1
fi
____d_normalize() {
sed '/^$/d' | sort | uniq
}
__d_help() {
if [[ $# -gt 0 ]]; then
echo "$0: error: no args accepted" >&2
return 1
fi
cat << EOF
$D_NAME - fav dirs
syntax:
$D_NAME [<COMMAND> [OPT...] [--] [ARG...]]
commands:
add [-f|--force] [dir...]
- add \`pwd\` or directories to the fav dirs file
clear [-f|--force]
- clear the fav dirs file
config
- show effective configuration
EOF
if [[ ! -v __D_EXECUTED ]]; then
cat << EOF
cd [-p|--parents]
- change current directory (default)
has no \`cd\` effect if ${BASH_SOURCE[0]} is executed as a script (not \`source\`d)
EOF
fi
cat << EOF
edit
- edit the fav dirs file
ls
- list the fav dirs file
prune
- prune missing directories from the fav dirs file
rm [dir...]
- remove directories from the fav dirs file
help
- show this help
EOF
}
__d_add() {
local -i FORCE=0
while [[ $# -gt 0 ]]; do
case "$1" in
'-f'|'--force')
FORCE=1
;;
'--')
shift
break
;;
*)
break
;;
esac
shift
done
if [[ $# -eq 0 ]]; then
set -- "$(pwd)"
fi
local DIR
for DIR; do
if [[ FORCE -eq 0 && ! -d "$DIR" ]]; then
if [[ ! -e "$DIR" ]]; then
echo "$D_NAME: error: $DIR does not exist" >&2
else
echo "$D_NAME: error: $DIR is not a directory" >&2
fi
return 1
fi
DIR="$(readlink -f -- "$DIR")"
printf '%s\n' "$DIR"
echo "add $DIR" >&2
done >> "$D_FAV_DIRS_FILE"
local -r TMP="$(mktemp)"
cp "$D_FAV_DIRS_FILE" "$TMP"
____d_normalize < "$TMP" > "$D_FAV_DIRS_FILE"
rm -f "$TMP"
}
__d_cd() {
if [[ -v __D_EXECUTED ]]; then
echo "$0: warn: the cd command may have no effect if started in limited (script) mode" >&2
fi
local -i PARENTS=0
if [[ $# -gt 0 ]] && [[ "$1" == '-p' || "$1" == '--parents' ]]; then
PARENTS=1
shift
fi
if [[ $# -gt 0 ]]; then
echo "$D_NAME: error: no args accepted" >&2
return 1
fi
if [[ D_PARENTS -ne 0 ]]; then
PARENTS=1
fi
local DIR
if [[ $PARENTS -eq 0 ]]; then
DIR="$(sort < "$D_FAV_DIRS_FILE" | uniq | "${D_SELECT_ONE[@]}")"
else
DIR="$(
{
printf '%s\n' '/'
while read -r DIR; do
while [[ -n "$DIR" ]]; do
printf '%s\n' "$DIR"
DIR="${DIR%/*}"
done
done
} < "$D_FAV_DIRS_FILE" | sort | uniq | "${D_SELECT_ONE[@]}"
)"
fi
cd "$DIR"
if [[ -v __D_EXECUTED ]]; then
pwd >&2
fi
}
__d_clear() {
if [[ $# -ne 1 ]] || [[ "$1" != '-f' && "$1" != '--force' ]]; then
echo "$D_NAME: error: must be confirmed with the -f (--force) switch" >&2
return 1
fi
: > "$D_FAV_DIRS_FILE"
}
__d_config() {
printf '%s=%q\n' \
'D_CONFIG_DIR' "$D_CONFIG_DIR" \
'D_FAV_DIRS_FILE' "$D_FAV_DIRS_FILE" \
'D_PARENTS' "$D_PARENTS" \
'D_SELECT_MANY' "${D_SELECT_MANY[*]}" \
'D_SELECT_ONE' "${D_SELECT_ONE[*]}"
}
__d_edit() {
if [[ $# -gt 0 ]]; then
echo "$D_NAME: error: no args accepted" >&2
return 1
fi
# TODO what is the best way to resolve the default editor?
if [[ -v VISUAL && -n "$VISUAL" ]]; then
local -r EDITOR="$VISUAL"
elif [[ -v EDITOR && -n "$EDITOR" ]]; then
:
elif IFS= read -r EDITOR < <(which editor) && [[ -v EDITOR && -n "$EDITOR" ]]; then
:
else
EDITOR='ed'
fi
"${EDITOR}" "$D_FAV_DIRS_FILE"
local -r TMP="$(mktemp)"
cp "$D_FAV_DIRS_FILE" "$TMP"
____d_normalize < "$TMP" > "$D_FAV_DIRS_FILE"
rm -f "$TMP"
}
__d_ls() {
if [[ $# -gt 0 ]]; then
echo "$D_NAME: error: no args accepted" >&2
return 1
fi
cat "$D_FAV_DIRS_FILE"
}
__d_prune() {
if [[ $# -gt 0 ]]; then
echo "$D_NAME: error: no args accepted" >&2
return 1
fi
local -r TMP="$(mktemp)"
local ORIG_DIR
local DIR
while read -r ORIG_DIR; do
DIR="$(readlink -f -- "$ORIG_DIR")"
if [[ ! -e "$DIR" ]]; then
echo "prune $ORIG_DIR" >&2
continue
fi
if [[ ! -d "$DIR" ]]; then
echo "prune $ORIG_DIR" >&2 # TODO consider indicating a removal for a non-directory
continue
fi
printf '%s\n' "$DIR"
done < "$D_FAV_DIRS_FILE" > "$TMP"
____d_normalize < "$TMP" > "$D_FAV_DIRS_FILE"
rm -f "$TMP"
}
__d_rm() {
local -A RM_INDEX_FILE
local DIR
if [[ $# -eq 0 ]]; then
while IFS= read -r DIR; do
RM_INDEX_FILE["$DIR"]=1
done < <("${D_SELECT_MANY[@]}" < "$D_FAV_DIRS_FILE")
else
for DIR; do
DIR="$(readlink -f -- "$DIR")"
RM_INDEX_FILE["$DIR"]=1
done
fi
if [[ ${#RM_INDEX_FILE[@]} == 0 ]]; then
return
fi
local -r TMP="$(mktemp)"
while IFS= read -r DIR; do
if [[ -n "${RM_INDEX_FILE[$DIR]+TO_RM}" ]]; then
echo "rm $DIR" >&2
unset "RM_INDEX_FILE[$DIR]"
continue
fi
printf '%s\n' "$DIR"
done < "$D_FAV_DIRS_FILE" > "$TMP"
____d_normalize < "$TMP" > "$D_FAV_DIRS_FILE"
rm -f "$TMP"
}
if [[ $# -eq 0 ]]; then
set -- 'cd'
fi
local -r COMMAND="${1?no command}"
shift
case "$COMMAND" in
'add'|'cd'|'clear'|'config'|'edit'|'help'|'ls'|'prune'|'rm')
__d_"$COMMAND" "$@"
;;
*)
echo "$D_NAME: error: unrecognized command: $COMMAND" >&2
return 1
;;
esac
}
if ! (return 2> /dev/null); then
set -TEeuo pipefail
__D_EXECUTED='' D_NAME="$0" d "$@"
fi