-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqc_mini
408 lines (374 loc) · 12.5 KB
/
qc_mini
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#
# Mini-Version of Quick-Change-Directory
#
# This script implements minimal versions of dstore and qc as shell functions.
#
# Call `dstore -h` or qc `-h` to get invocation details.
#
# INDEX
# -----
# The main index files is "$HOME/.dirstore".
# This file contains entries created by the function 'dstore'.
# They are either normal directory names or labeled directories.
# E.g.:
# /home/joe/documents/lyrics
# :scripts /home/joe/code/shell-scripts
#
# Additional index files are named "$HOME/.dirstore-*".
# This indexes are not managed by 'dstore', but have to be created manually.
#
# CHANGING DIRECTORY BY NAME
# --------------------------
# The search syntax for qc is simple:
#
# 1. Every argument gets a '*' appended, except it ends with '/'
# 2. The arguments are concatenated with a '/'
#
# Then a regex is created:
# 1. Every '*' is replaced with '[^/]*'
# 1. Multiple slashes (//, ///, ...) are replaced with '\(.*/\)\?'
#
# So:
#
# search for dir*
# qc dir .*/dir[^/]*$
# search for dir (no '*' appended)
# qc dir/ .*/dir$
# search for dir2* in dir1*
# qc dir1 dir2 .*/dir1[^/]*/dir2[^/]*$
# search for dir2* somewhere below dir1*, could be multiple levels
# qc dir1 /dir2 .*/dir1[^/]*/\(.*/\)\?dir2[^/]*$
# search for dir name starting with "some" and containing "dir"
# qc some*dir .*/some[^/]*dir[^/]*$
#
# NOTE: Wildcard expansion is disabled for qc. So '*' doesn't have to be
# escaped on the qc command line.
#
# CHANGING DIRECTORY BY LABEL
# ---------------------------
# A labeled directory entry is created with 'dstore :<LABEL> directory'.
# To change to it execute 'qc :<LABEL>'.
# Assuming the example content mentioned above, 'qc :scripts' brings you to
# /home/joe/code/shell-scripts.
#
# MANAGING ~/.dirstore
# --------------------
# The file ~/.dirstore is managed with the command 'dstore'.
# The following invocations are supported:
#
# dstore
# Stores the current directory in ~/.dirstore.
# dstore dir [dir...]
# Stores the given directories in ~/.dirstore.
# dstore :<lable>
# Stores the current directory with the given label in ~/.dirstore.
# dstore :<lable> dir
# Stores the given directory with the given label in ~/.dirstore.
# dstore -e
# Open ~/.dirstore in an editor (vi by default).
# dstore -l
# List the content of ~/.dirstore.
# dstore -h
# Display help.
#
# Note: A label starts with a colon and may only contains characters, digits,
# dash and underscore.
#
# PORTABILITY
# -----------
# This script is not strictly POSIX compliant.
# - uses 'typeset' to define local variables
# - uses a select-loop
#
# Works in BASH and should run unchanged in ZSH.
#
# For KSH the 'typeset' does not work as expected as it is only honored when
# used in a function defined with the 'function' keyword. To use it with ksh
# the line 'dstore()' should be changed to 'function dstore' and '__qc' to
# 'function __qc'.
#
# shellcheck shell=sh
# File to store directories (incl. labeled dirs)
[ -z "${QC_DIRSTORE:-}" ] && export QC_DIRSTORE="$HOME/.dirstore"
unalias dstore >/dev/null 2>&1
unalias qc >/dev/null 2>&1
#
# Stores entries in QC_DIRSTORE
#
# Usage:
# Store current directory: dstore
# Store given directories: dstore dir1 ...
# Label current directory: dstore :<label>
# Label given directory: dstore :<label> dir
#
# Also:
# -e opens QC_DIRSTORE in editor
# -l list dirstore content
#
# shellcheck disable=SC3044 # typeset is not POSIX
dstore()
{
# IMPORTANT: reset OPTIND
typeset -i OPTIND=1
typeset qcm_o
while getopts "elh" qcm_o "$@"; do
case "$qcm_o" in
e) ${EDITOR:-vi} "$QC_DIRSTORE"; return $? ;;
l) cat "$QC_DIRSTORE"; return $? ;;
h | *)
echo >&2 "Usage:"
echo >&2 " dstore"
echo >&2 " Store current directory."
echo >&2 " dstore dir.."
echo >&2 " Store dir(s)."
echo >&2 " dstore :label"
echo >&2 " Store current directory with label."
echo >&2 " dstore :label dir"
echo >&2 " Store given directory with label."
echo >&2 " dstore -e"
echo >&2 " Open index in editor."
echo >&2 " dstore -l"
echo >&2 " Display index."
echo >&2 " dstore -h"
echo >&2 " Show this help."
echo >&2 ""
echo >&2 "Index file: $QC_DIRSTORE"
echo >&2 ""
return 1
esac
done
shift $((OPTIND-1))
[ $# -eq 0 ] && set - "$PWD"
typeset qcm_dir
case "$1" in
:*)
if [ ${#1} -eq 1 ] || [ "$(echo "$1" | tr -d -- -A-Za-z0-9_)" != ':' ]; then
echo >&2 "ERROR: Invalid label '$1'."
return 1
fi
if [ $# -eq 1 ]; then
qcm_dir="."
elif [ $# -eq 2 ]; then
qcm_dir="$2"
else
echo >&2 "ERROR: Expected max 2 arguments"
return 1
fi
if [ -d "$qcm_dir" ]; then
qcm_dir=$(cd "$qcm_dir" && /bin/pwd)
echo "$1 $qcm_dir" >> "$QC_DIRSTORE"
else
echo >&2 "$qcm_dir: Not a directory"
fi
;;
*)
for qcm_dir in "$@"; do
if [ -d "$qcm_dir" ]; then
qcm_dir=$(cd "$qcm_dir" && /bin/pwd)
echo "$qcm_dir" >> "$QC_DIRSTORE"
else
echo >&2 "$qcm_dir: Not a directory"
fi
done
;;
esac
# delete duplicates
sort -u "$QC_DIRSTORE" > "$QC_DIRSTORE.sorted"
mv "$QC_DIRSTORE.sorted" "$QC_DIRSTORE"
}
if [ -n "${ZSH_VERSION:-}" ]; then
alias qc='noglob __qc'
else
# bash, ksh, mksh, pdksh, dash
alias qc='set -f;qcm_rst_f=true __qc'
fi
__qc_glob_match()
{
test -e "$1" -o -L "$1"
}
## PATTERN_BUILD-START
# shellcheck disable=SC3044 # typeset is not POSIX
__qc_create_pattern()
{
typeset qcm_pattern=""
case "$1" in
:*) echo "^$1";;
*)
typeset qcm_word
for qcm_word in "$@"; do
# remove trailing '*', replace others with [^/]*
qcm_word="$(echo "$qcm_word" | sed 's%^\*\**/%%;s/\*\+$//;s%\*\+%[^/]*%;s%///*$%//%')"
qcm_word="${qcm_word#/}"
case "$qcm_word" in
*/) qcm_pattern="$qcm_pattern/${qcm_word%/}" ;;
*) qcm_pattern="$qcm_pattern/${qcm_word}[^/]*" ;;
esac
done
qcm_pattern="$(echo "$qcm_pattern" | sed 's%//\+%/\\(.*/\\)*%g;s%?%[^/]%g;s%/\+$%%')"
echo "$qcm_pattern\$"
;;
esac
}
## PATTERN_BUILD-END
# shellcheck disable=SC3008,SC3044 # select and typeset are not POSIX
__qc()
{
[ -n "${qcm_rst_f:-}" ] && set +f
unset qcm_rst_f
# locally no error message (and function exit) on not matching glob
[ -n "${ZSH_VERSION:-}" ] && setopt localoptions nonomatch
if [ ! -r "$QC_DIRSTORE" ]; then
echo >&2 "qc_mini: Index file does not exist/is not readable: $QC_DIRSTORE"
return 1
fi
# IMPORTANT: reset OPTIND
typeset -i OPTIND=1
typeset qcm_o
while getopts "lSh" qcm_o "$@"; do
case "$qcm_o" in
l) # list labeled directories
grep -h "^:[^ ]*" "$QC_DIRSTORE" "$QC_DIRSTORE"-* 2>/dev/null
return 0
;;
S) # show indexes
for qcm_fn in "$QC_DIRSTORE" "$QC_DIRSTORE"-*; do
if [ -e "$qcm_fn" ]; then
echo "$qcm_fn"
echo " Last Chg: $(stat -c "%y" "$qcm_fn")"
printf " Entries: %'d (%'d bytes)\n" "$(wc -l < "$qcm_fn")" "$(wc -c < "$qcm_fn")"
echo " Labeled entries: $(grep -c "^:" "$qcm_fn")"
fi
done
return
;;
h | *)
# unknown option
echo >&2 "Usage:"
echo >&2 " qc dir"
echo >&2 " Change to directory matching 'dir*'"
echo >&2 " qc dir1 dir2"
echo >&2 " Change to directory matching 'dir1*/dir2*'"
echo >&2 " qc -l"
echo >&2 " List labeled dirs."
echo >&2 " qc -S"
echo >&2 " Show used index files."
echo >&2 " qc -h"
echo >&2 " Show this help."
echo >&2 " qc -"
echo >&2 " Act like 'cd -'"
echo >&2 " qc"
echo >&2 " Act like 'cd'"
return 1
;;
esac
done
shift $((OPTIND-1))
if [ $# -eq 0 ]; then
# shellcheck disable=SC2164 # return with exit code below
cd
return
elif [ $# -eq 1 ] && [ "$1" = "-" ]; then
# shellcheck disable=SC2164 # return with exit code below
cd -
return
elif [ "$1" = "--" ]; then
shift
fi
typeset qcm_call_param="$*"
typeset qcm_pattern
qcm_pattern="$(__qc_create_pattern "$@")"
qcm_oldIFS="$IFS"
IFS="$(printf '\nx')" && IFS="${IFS%x}"
# shellcheck disable=SC2046 # IFS is set
if __qc_glob_match "$QC_DIRSTORE"-*; then
set -- $(grep -h -- "$qcm_pattern" "$QC_DIRSTORE" "$QC_DIRSTORE"-* | sed 's/^:[^ ]* //' | sort -u)
else
set -- $(grep -h -- "$qcm_pattern" "$QC_DIRSTORE" | sed 's/^:[^ ]* //' | sort -u)
fi
IFS="$qcm_oldIFS"
case $# in
0 ) echo >&2 "Not found: '$qcm_call_param' ($qcm_pattern)" ;;
1 )
# shellcheck disable=SC2164 # return with exit code below
cd "$1"
return
;;
* )
typeset qcm_dir
typeset qcm_rc
qcm_rc=0
select qcm_dir in "$@"; do
if [ "$qcm_dir" = "" ]; then
[ "$REPLY" = "q" ] && break
else
# shellcheck disable=SC2164 #
cd "$qcm_dir"
qcm_rc=$?
break
fi
done
return $qcm_rc
;;
esac
}
if [ -z "${BASH_VERSION:-}${ZSH_VERSION:-}" ]; then
return
elif [ -n "${BASH_VERSION:-}" ]; then
# Lots of shellcheck directives now as the following is BASH code
# shellcheck disable=SC2207,SC3003,SC3010,SC3030,SC3044,SC3050,SC3054
_qc_complete()
{
typeset cur opts
COMPREPLY=()
#_get_comp_words_by_ref -n : cur
cur="${COMP_WORDS[COMP_CWORD]}"
if [ "${COMP_WORDS[COMP_CWORD-1]}" = ":" ]; then
cur=":$cur"
fi
if [ -n "$cur" ]; then
if [[ "$cur" = ":"* ]]; then
opts=$(grep "^${cur}[^ ]* " "${QC_DIRSTORE}" | sed "s% .*%%;s%^:%%")
else
typeset qcm_pattern
qcm_pattern="$(__qc_create_pattern "${COMP_WORDS[@]:1}")"
qcm_pattern="${qcm_pattern%\$}\\(/\\|$\\)"
opts=$(grep "$qcm_pattern" "${QC_DIRSTORE}" | sed 's/^:[^ ]* //' | grep -o "${cur}[^/]*/\\?")
fi
if [ -n "$opts" ]; then
typeset IFS=$'\n'
COMPREPLY=( $(compgen -W "${opts}" -- "${cur#:}") )
# Do escaping
COMPREPLY=( $(printf '%q\n' "${COMPREPLY[@]}") )
fi
return 0
fi
}
# shellcheck disable=SC3044 # bash only
complete -o nospace -F _qc_complete qc
else
# Lots of shellcheck directives now as the following is not posix sh code
# Would need a real ZSH programmer to improve this function.
# shellcheck disable=SC2207,SC3003,SC3010,SC3030,SC3043,SC3044,SC3054
_qc_complete()
{
local cur="$PREFIX"
local -a opts
if [ -n "$cur" ]; then
if [[ "$cur" == ":"* ]]; then
local IFS=$'\n'
opts=($(grep "^${cur}[^ ]* " "${QC_DIRSTORE}" | sed "s% .*%%"))
compadd "${opts[@]}"
else
typeset qcm_pattern
qcm_pattern="$(__qc_create_pattern "${COMP_WORDS[@]:1}")"
qcm_pattern="${qcm_pattern%\$}\\(/\\|$\\)"
local IFS=$'\n'
opts=( $(grep "$qcm_pattern" "${QC_DIRSTORE}" | sed 's/^:[^ ]* //' | grep -o "${cur}[^/]*/\\?") )
compadd "${opts[@]}"
fi
return 0
fi
}
compdef _qc_complete __qc qc
fi
# vim:ft=sh