-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqc-backend
executable file
·685 lines (611 loc) · 19.7 KB
/
qc-backend
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/usr/bin/env bash
#
# FILE: qc-backend
#
# ABSTRACT: Selects a directory by pattern from the index
#
# AUTHOR: Ralf Schandl
#
qc_version=2.0
# Directory for qc index files
[ -z "${QC_DIR:-}" ] && QC_DIR=$HOME/.qc
QC_DIR_CFG="$QC_DIR"
QC_DIR_DATA="$QC_DIR"
QC_DIR_INDEX="$QC_DIR/index"
CFG_FILE="$QC_DIR_CFG/qc-index.cfg"
# Manual index file storing directory names and bookmarked directories. This
# file is managed using the command 'dstore'.
QC_DSTORE_INDEX=$QC_DIR_DATA/index.dstore
# error on unset variable
set -u
# enable extended globbing
shopt -s extglob nullglob
show_help()
{
echo "Quick Change -- index based directory changer"
echo ""
echo "Usage:"
echo " qc [OPTION]... <dir-pattern>"
echo " Search for dir in index and cd to it."
echo " -i Search case-insensitive. Default when QC_NO_CASE is set."
echo " -c Search case-sensitive. Default when QC_NO_CASE is not set."
echo " -h Also search indexes of hidden dirs."
echo " -H Only search indexes of hidden dirs."
echo " -e Also search extended indexes."
echo " -E Only search extended indexes."
echo " -a Search all indexes."
echo " dir-pattern:"
echo " 'Documents/Adm' searches for '**/Documents/Adm*'"
echo " 'Doc Adm' searches for '**/Doc*/Adm*'"
echo " 'Doc //Adm' searches for '**/Doc*/**/Adm*'"
echo " qc :name"
echo " Search for dir labeled with ':name' in index and cd to it."
echo " Use the command 'dstore' to manage labeled directories."
echo " qc -u [dir...]"
echo " qc -U [dir...]"
echo " Updates the indexes. '-U' also updates extended indexes. If dirs are"
echo " given only indexes containing any of those directories are updated."
echo " qc -l"
echo " List labeled dirs."
echo " qc -S"
echo " Print index statistics."
echo " qc --config"
echo " Open index config file 'qc-index.cfg' in a editor. Uses \$VISUAL,"
echo " \$EDITOR or vi."
echo " qc --cron [minutes]"
echo " Configure crontab to update the index."
echo " minutes:"
echo " not given: Lists current crontab config."
echo " 0: Removes entry from crontab."
echo " 1 - 30: Configures to run on the full hour and every multiple"
echo " of given minutes. E.g. for '12' it will run at "
echo " *:00, *:12, *:24, *:36 and *:48."
echo " qc -"
echo " Act like 'cd -'."
echo " qc"
echo " Act like 'cd \$HOME'."
echo ""
echo "See also: dstore --help"
echo ""
check_initialized
}
check_initialized()
{
# shellcheck disable=SC2143
if [ -z "$(get_indexes a | grep -v "$QC_DSTORE_INDEX")" ]; then
echo "==============================================================================="
echo
echo "Quick Change Directory not initialized."
echo
echo "Your home directory needs to be scanned to create a index. Depending on its"
echo "size this might take while."
echo
echo "Call 'qc -u' or 'qc -U' to create a index of your home directory."
echo
echo "See 'qc --help' for details."
echo
echo "==============================================================================="
return 1
else
return 0
fi
}
# Create regex from qc argument
# Used from qc and completion
# $*: The pattern to create a regex
args2regex()
{
typeset glob regex w
glob=''
regex=''
for w in "$@"; do
if [[ "$w" = /* ]]; then
w="${w#/}"
fi
case "$w" in
*/) # don't append '*' if word ends with '/'
w="${w%?}";;
*\*) # don't append '*' if it already ends with '*'
: ;;
*) w="$w*" ;;
esac
glob="$glob/$w"
done
if [ -n "${cl_completion:-}" ]; then
# fix multiple trailing '*'
if [[ "$glob" = *\*\* ]]; then
glob="${glob%%*(\*)}*"
fi
fi
# replace leading '/./' with current dir (without leading slash)
glob="${glob/#\/.\//$PWD/}"
# Remove
# - leading slashes
# - than leading asterisks followed by slashes
# - than trailing slashes
glob="$(echo "$glob" | sed 's%^/\+%%;s%^\*\+//*%%;s%/\+$%%')"
if [ -n "$PRINT_EXPR" ]; then
echo "$glob"
fi
while [ -n "$glob" ]; do
case "$glob" in
\*\**)
glob="${glob#"${glob%%[!*]*}"}"
if [ "${glob:0:1}" = "/" ]; then
# '**/' -> '\(.*/\)*'
glob="${glob:1}"
regex+='\(.*/\)*'
else
# '**' -> '.*'
regex+='.*'
fi
;;
//*) # '//' -> '/.*/'
regex+='/\(.*/\)*'
glob="${glob#"${glob%%[!/]*}"}"
;;
\**) # '*' -> '[^/]*'
regex+='[^/]*'
glob="${glob:1}"
;;
\?*) # '?' -> '[^/]'
regex+='[^/]'
glob="${glob:1}"
;;
\\\?*) # copy escaped char
regex+='?'
glob="${glob:2}"
;;
\\*) # copy escaped char
regex+="${glob:0:2}"
glob="${glob:2}"
;;
.*)
regex+='\.'
glob="${glob:1}"
;;
*)
regex+="${glob:0:1}"
glob="${glob:1}"
;;
esac
done
regex="${regex}$"
echo "$regex"
}
# Determine index files. One file per line
# "$1": zero or more chars describing indexes to select
# a: all indexes
# h: default & hidden indexes
# H: hidden indexes
# e: default & extended indexes
# E: extended indexes
# EMPTY: default indexes
get_indexes()
{
typeset IFS=$'\n'
typeset sel="${1:-}"
typeset default=0
typeset hidden=0
typeset extended=0
if [[ -z $sel ]]; then
default=1
elif [[ $sel == *a* ]]; then
default=1
hidden=1
extended=1
else
if [[ $sel = *[H]* ]]; then
hidden=1
fi
if [[ $sel = *[h]* ]]; then
default=1
hidden=1
fi
if [[ $sel = *[E]* ]]; then
extended=1
fi
if [[ $sel = *[e]* ]]; then
default=1
extended=1
fi
fi
# make the hostname lower case
typeset -l HOSTNAME="$HOSTNAME"
typeset -a idx_files
if [[ $default = 1 ]]; then
idx_files+=( "$QC_DIR_INDEX"/*.index "$QC_DIR_INDEX/$HOSTNAME"/*.index )
fi
if [[ $hidden = 1 ]]; then
idx_files+=( "$QC_DIR_INDEX"/*.index.hidden "$QC_DIR_INDEX/$HOSTNAME"/*.index.hidden )
fi
if [[ $extended = 1 ]]; then
idx_files+=( "$QC_DIR_INDEX"/*.index.ext "$QC_DIR_INDEX/$HOSTNAME"/*.index.ext )
fi
[ -e "$QC_DSTORE_INDEX" ] && idx_files+=( "$QC_DSTORE_INDEX" )
if [ ${#idx_files[*]} -gt 0 ]; then
printf "%s\n" "${idx_files[@]}"
fi
}
# Searches the index.
# Used by dir_select and by completion
# $1: index(es) select string, see comment on function get_indexes for details
# $2-: text to search for. With leading ':' triggers search by label.
search_dirs()
{
typeset indexSelect="$1"
shift
typeset IFS=$'\n'
typeset -a dl idxFiles
case $1 in
:*)
pattern=$1
if [ ! -e "$QC_DSTORE_INDEX" ]; then
echo >&2 "qc: Dstore index file not found! See 'dstore --help'."
return 1
fi
# grep: -h: Suppress the prefixing of file names on output.
mapfile -d$'\n' -t dl < <(grep -ahi -- "^${pattern}[^ ]* " "$QC_DSTORE_INDEX" | cut "-d " -f2- | sort -u)
if [[ $# -gt 1 ]]; then
shift
for bd in "${dl[@]}"; do
bd="${bd%/}/"
ABSOLUTE_SEARCH=true search_dirs "a" "$bd" "$@"
done
return 0
elif [ $# -gt 1 ]; then
echo >&2 "WARNING: Extra arguments ignored ($pattern returned none or more than one directory)"
fi
;;
*)
mapfile -d$'\n' -t idxFiles < <( get_indexes "$indexSelect")
if [ ${#idxFiles[@]} = 0 ]; then
echo >&2 "qc: No index file found! Use 'qc -u' or 'dstore' to create it."
return 1
fi
pattern="/$(args2regex "$@")"
if [ -n "${ABSOLUTE_SEARCH:-}" ]; then
pattern="^\\(:[^ ]* \\+\\)\\?$pattern"
fi
typeset -a grepOpt
if [ -n "${QC_NO_CASE:-}" ]; then
grepOpt+=( "-i" )
fi
mapfile -d$'\n' -t dl < <(grep --color=always -ah "${grepOpt[@]}" -- "$pattern" "${idxFiles[@]}" | sed 's/^:[^ ]* //;s/^\(\x1B\[..\;..m.\[K\):[^ ]* /\1/' | sort -u)
;;
esac
if [ ${#dl[*]} -gt 0 ]; then
printf "%s\n" "${dl[@]}"
fi
}
# Select a directory from the index(es).
# $1: index(es) select string, see comment on function get_indexes for details
# $2-: text to search for. With leading ':' triggers search by label.
dir_select()
{
mapfile -d$'\n' -t dl < <(search_dirs "$@")
# we don't need index select string anymore
shift
typeset cnt=${#dl[*]}
if [ "$cnt" -eq 0 ]; then
typeset IFS=' '
echo >&2 "qc: $*: No matching directory found"
return 1
elif [ "$cnt" -eq 1 ]; then
# echo and remove color codes
echo "${dl[0]}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
return 0
else
# If selection list needs more than terminal hight lines
# ask user, if he/she really want's to see it.
if [ -n "${LINES:-}" ] && [ "$cnt" -gt $((LINES -1)) ]; then
echo >&2 -n "Display all $cnt directories? (y or n) "
read -rN1 yn
case $yn in
y|Y) echo >&2
;;
*) echo >&2
return
;;
esac
fi
if [ -z "${QC_SKIP_FILTER_EXISTING:-}" ]; then
# filter out none existing dirs
typeset i
typeset orgCnt=$cnt
for (( i=orgCnt-1; i>=0; i-- )); do
printf >&2 "\rFiltering none existing directories: %d/%d" $((orgCnt-i)) "$orgCnt"
# remove color codes for check
if [ ! -d "$(echo "${dl[$i]}"| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" ]; then
if [ -n "${QC_DBG:-}" ]; then
echo >&2 "REMOVING: ${dl[$i]}"
fi
unset "dl[$i]"
fi
done
dl=( "${dl[@]}" )
printf >&2 "\r\e[K"
cnt=${#dl[*]}
if [ "$orgCnt" -ne "$cnt" ]; then
echo >&2 "qc: Removed $((orgCnt - cnt)) non existing directories from result"
fi
fi
if [ "$cnt" -eq 0 ]; then
echo >&2 "qc: $*: No matching directory found"
return
elif [ "$cnt" -eq 1 ]; then
# echo and remove color codes
echo "${dl[@]}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
return
fi
if [ -n "${QC_SORT_LENGTH:-}" ]; then
#mapfile -d$'\n' -t dl < <(IFS=$'\n';perl -e 'print sort { length($a) <=> length($b) } <>' <<<"${dl[*]}" )
mapfile -d$'\n' -t dl < <( IFS=$'\n'; ( awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2- ) <<<"${dl[*]}" )
fi
typeset COLUMNS=0
# replace home directory with '~' for display and later re-add it on
# selected dir.
dl=( "${dl[@]/#$HOME/\~}" )
if [ -n "${QC_FZF:-}" ]; then
the_dir="$(printf "%s\n" "${dl[@]}" | fzf -i --ansi )" # --preview='d={};ls --color ${d/#~/$HOME}')"
if [ -n "$the_dir" ]; then
echo "${the_dir/#\~/$HOME}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
fi
else
local LINES=0
local COLUMNS=1
local PS3="# or 'q' to quit? "
select dir in "${dl[@]}" ; do
if [ "$dir" = "" ]; then
[ "$REPLY" = "q" ] && break
else
# replace ~ with $HOME and remove color codes
echo "${dir/#\~/$HOME}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
break
fi
done
fi
fi
}
# Update/create the index.
updateIndex()
{
if command -v qc-build-index &>/dev/null; then
echo "Updating index ..."
typeset -a ARGS
typeset d
for d in "$@"; do
if [ "$d" = "-E" ]; then
ARGS+=( "-E" )
elif [ "$d" = "--" ]; then
# skip
:
else
d="$(readlink -f "$d")"
ARGS+=( "-i" "$d")
fi
done
echo "Executing qc-build-index ${ARGS[*]}"
qc-build-index "${ARGS[@]}"
else
echo "Updating index (internal update)..."
internalUpdateIndex "$@"
fi
}
internalUpdateIndex()
{
mkdir -p "$QC_DIR_INDEX" || return 1
typeset INDEX_FILE=$QC_DIR_INDEX/home.index
# shellcheck disable=SC2155 # Declare and assign separately
typeset NEW_INDEX=$(mktemp "${INDEX_FILE}.XXXX")
find "${HOME}" -xdev -type d \( -name '.*' -o -name CVS \) -prune -o -xtype d -print >> "$NEW_INDEX"
mv -f "$NEW_INDEX" "$INDEX_FILE"
echo "Stored $(wc -l < "$INDEX_FILE") directory names."
}
# Show statistics of indexes. List index.dstore last.
index_statistics()
{
mapfile -d$'\n' -t idx_list < <(get_indexes "a")
for fn in "${idx_list[@]}"; do
echo >&2 "$fn"
echo >&2 " Last Upd: $(stat -c "%y" "$fn")"
# shellcheck disable=SC2183,SC2046 # wc produces two args
printf >&2 " Entries: %'d (%'d bytes)\n" $(wc -lc <"$fn")
if [ "$fn" = "$QC_DSTORE_INDEX" ]; then
echo >&2 " Labeled entries: $(grep -c "^:" "$fn")"
fi
done
}
complete_index_dir()
{
typeset indexSelect="$1"
shift
typeset IFS=$'\n'
typeset -a dl idxFiles compl
typeset last_part last_part_re grps
if [[ $# -eq 1 && $1 = ':'* ]]; then
if [ ! -e "$QC_DSTORE_INDEX" ]; then
return 1
fi
# grep: -h: Suppress the prefixing of file names on output.
grep -ahi -- "^${1}[^ ]* " "$QC_DSTORE_INDEX" | cut "-d " -f1 | sort -u
else
# shellcheck disable=SC2124 # this expands to one element
last_part="${@:$#}"
last_part_re="$(args2regex "$last_part")"
last_part_re="${last_part_re%[*}"
last_part_re="${last_part_re/%\$/\/}"
grps="${last_part_re//[^(]/}"
grps=$(( ${#grps} + 1))
if [[ "$last_part" = *'/' ]]; then
set -- "$@" ""
fi
#{
# echo "last_part >>$last_part<<"
# echo "last_part_re >>$last_part_re<<"
# echo "grps >>$grps<<"
# search_dirs "$indexSelect" "$@"
#} >&2
mapfile -d$'\n' -t compl < <(search_dirs "$indexSelect" "$@" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | sed "s|^.*/${last_part_re}\([^/]*/\?\)$|$last_part\\$grps|${QC_NO_CASE+i} ;s|$|/|" | sort -u)
if [ "${#compl[*]}" -gt 0 ]; then
printf '%q\n' "${compl[@]}"
fi
fi
}
complete_local_dir()
{
compgen -o dirnames -- "${@:$#}"
exit 0
}
complete_option()
{
# shellcheck disable=SC2124 # this expands to one element
local cur="${@:$#}"
if [[ " $*" = *--help* ]]; then
exit 0
elif [[ "$cur" = --* ]]; then
printf "%s\n" --help --config --version | grep -- "$cur"
exit 0
elif [[ "$cur" = -* ]]; then
printf -- "-%s\n" i e E h H a u U S l
if [[ "$cur" != -?* ]]; then
printf "%s\n" --help --config --version | grep -- "$cur"
fi
exit 1
fi
}
#---------[ MAIN ]-------------------------------------------------------------
typeset indexSelect=""
typeset PRINT_EXPR=""
# absolute search is needed when a sub-dir of a labeled dir is searched.
typeset ABSOLUTE_SEARCH=""
PATH="$PATH:$QC_DIR"
cl_completion=""
if [ "${1:-}" = "--complete" ]; then
shift
cl_completion=true
complete_option "$@"
fi
if ! _args="$(getopt ${cl_completion:+-q} -n "qc" -o "ieEhHauUSl" --long "help,version,config,cron,print-expr" -- "$@")"; then
[ -z "$cl_completion" ] && echo >&2 "Try 'qc --help' for more information."
exit 1
fi
eval "set -- $_args"
while true; do
case $1 in
-i) QC_NO_CASE=true
;;
-c) QC_NO_CASE=
;;
-a) indexSelect="${indexSelect}a"
;;
-h) indexSelect="${indexSelect}h"
;;
-H) indexSelect="${indexSelect}H"
;;
-e) indexSelect="${indexSelect}e"
;;
-E) indexSelect="${indexSelect}E"
;;
-u)
[ -n "$cl_completion" ] && complete_local_dir "$@"
shift
updateIndex -E "$@" >&2
exit 0
;;
-U)
[ -n "$cl_completion" ] && complete_local_dir "$@"
shift
updateIndex "$@" >&2
exit 0
;;
-S)
[ -n "$cl_completion" ] && exit 1
index_statistics
exit 0
;;
-l)
[ -n "$cl_completion" ] && exit 1
if [ -e "$QC_DSTORE_INDEX" ]; then
grep --color=always '^:[^ ]* ' "$QC_DSTORE_INDEX" >&2
exit 0
else
echo >&2 "qc: Dstore index file not found! See 'dstore --help'."
exit 1
fi
;;
--print-expr) PRINT_EXPR=true
;;
--help)
[ -n "$cl_completion" ] && complete_local_dir "$@"
show_help >&2
exit 0
;;
--version)
[ -n "$cl_completion" ] && exit 1
echo "qc - Quick Change Directory v$qc_version" >&2
exit 0
;;
--config)
[ -n "$cl_completion" ] && exit 1
if [ ! -e "$CFG_FILE" ]; then
echo >&2 "ERROR: $CFG_FILE does not exist."
echo >&2 " Use 'qc -u' to create a default config file."
exit 1
fi
${VISUAL:-${EDITOR:-vi}} "$CFG_FILE" </dev/tty >/dev/tty
exit 0
;;
--cron)
[ -n "$cl_completion" ] && exit 1
qc-build-index "$@" >&2
exit $?
;;
--) shift
break
;;
*)
[ -n "$cl_completion" ] && exit 1
echo >&2 "Invalid option '$1'"
echo >&2 "Try 'qc --help' for more information."
exit 1
;;
esac
shift
done
if [ 1 -ne "$(echo "$*" | wc -l)" ]; then
echo >&2 "ERROR: Search string contains line break -- not supported"
exit 1
fi
if [ -n "$PRINT_EXPR" ]; then
args2regex "$@" >&2
exit 0
fi
if ! check_initialized >&2; then
exit 1
fi
# if args include hidden filename act like '-h' was given
if [[ " $*" = *[\ /].[!/]* ]] && [[ "$indexSelect" != [hH] ]]; then
indexSelect="h"
fi
if [ -n "${cl_completion:-}" ]; then
if [ "$*" = "" ]; then
exit 0
fi
complete_index_dir "$indexSelect" "$@"
exit 0
fi
if [ "$*" = "" ]; then
echo "$HOME"
elif [ "$*" = "-" ]; then
echo "-"
else
# shellcheck disable=SC2155 # Declare and assign separately
typeset tgt=$(dir_select "$indexSelect" "$@")
if [ -n "${tgt:-}" ]; then
echo "$tgt"
else
exit 1
fi
fi
# vim: ft=sh