-
Notifications
You must be signed in to change notification settings - Fork 0
/
infiren.sh
executable file
·1140 lines (1056 loc) · 47.4 KB
/
infiren.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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# -
# Interactive File Renamer (InFiRen) -
# -
# Created by Fonic <https://github.com/fonic> -
# Date: 04/23/19 - 04/25/24 -
# -
# ------------------------------------------------------------------------------
# --------------------------------------
# -
# Early birds -
# -
# --------------------------------------
# Check if running Bash and required version (NOTE: this check does not rely on
# Bashism to make sure it is guaranteed to work on any POSIX shell)
if [ -z "${BASH_VERSION}" ] || [ "${BASH_VERSION%%.*}" -lt 5 ]; then
echo -e "\e[1;31mError: this script requires Bash >= v5.0 to run, aborting.\e[0m"
exit 1
fi
# Set up error handler (exit on unbound variables and on unhandled errors)
set -ueE; trap "echo -e \"\e[1;31mError: an unhandled error occurred on line \${LINENO}, aborting.\e[0m\"; exit 1" ERR
# --------------------------------------
# -
# Globals -
# -
# --------------------------------------
# Application info (NOTE: realpath is not guaranteed to be available, e.g.
# macOS does not have it while FreeBSD does; stripping trailing '/' off of
# APP_DIR to account for root directory, i.e. '/'; could use ${APP_DIR%/}
# instead to build paths, but that would be needlessly confusing for users
# when being used within config file)
APP_TITLE="Interactive File Renamer (InFiRen)"
APP_VERSION="4.2 (04/25/24)"
if command -v realpath >/dev/null; then
APP_DIR="$(dirname -- "$(realpath -- "$0")")"
APP_FILE="$(basename -- "$(realpath -- "$0")")"
else
APP_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
APP_FILE="$(basename -- "$0")"
fi
APP_DIR="${APP_DIR%/}"
APP_NAME="${APP_FILE%.*}"
APP_CONFIG="${APP_DIR}/${APP_NAME}.conf"
# Input/edit prompt
PROMPT_CMD="cmd> "
PROMPT_EDIT="edit> "
# Help/usage text explaining available commands (NOTE: masking errors as
# read will encounter EOF, which will trip error handler if left unmasked)
read -r -d '' HELP_COMMANDS <<- EOD || :
rs, replace-string STR REP Replace string STR with replacement REP
re, replace-regex RE TMP Match regular expression RE and replace
matching string according to template TMP
(e.g. re "([0-9]+)x([0-9]+)" "S\\1E\\2")
pr, pre, prepend STR Prepend string STR
po, post, append STR Append string STR
rd, replace-dots Replace single dots with spaces
id, insert-dash Insert dash after first word
ca, capitalize Capitalize space-separated words
up, upper, uppercase Convert all characters to uppercase
lo, lower, lowercase Convert all characters to lowercase
tr, trim, st, strip Trim leading and trailing whitespace
rm, record-macro Start/stop recording macro
vm, view-macro View macro contents
cm, clear-macro Clear macro contents
pm, play-macro Play back commands stored in macro
pd, playback-delay VALUE Set delay in between commands for command
playback to VALUE (in seconds, fractions
are supported)
sm, save-macro NAME Save macro using name NAME to macro file
lm, load-macro NAME Load macro named NAME from macro file
dm, delete-macro NAME Delete macro named NAME from macro file
im, list-macros List all macros stored in macro file
hm, history-macro Create macro from command history
vh, view-history View command history
ch, clear-history Clear command history
fp, filter-pattern PATTERN Set filter pattern to PATTERN and reload
files
if, invert-filter Invert filter and reload files
fc, filter-case Toggle filter case and reload files
vf, view-filter View current filter state
rf, reset-filter Reset filter and reload files
ed, edit INDEX Manually edit entry with index INDEX
ud, undo Undo/redo last name-altering operation
rc, recursive Toggle recursive mode and reload files
cd, chdir PATH Change directory to PATH and reload files
apply, save Apply changes (i.e. rename files)
reload, reset Discard changes and reload files
help, usage Display this help/usage text
exit, quit Exit program (shortcut: CTRL+D)
EOD
# --------------------------------------
# -
# Configuration -
# -
# --------------------------------------
# Initial directory (if empty, current working directory is used)
INITIAL_DIRECTORY=""
# Initial filter pattern (see 'man find', option '-name pattern' for syntax;
# '*' == all files)
FILTER_PATTERN="*"
# Initial filter invert setting ('true'/'false'; 'true' == inversion enabled)
FILTER_INVERT="false"
# Initial filter case setting ('true'/'false'; 'true' == case sensitive)
FILTER_CASE="false"
# Initial recursive mode setting ('true'/'false'; 'true' == recursion enabled)
RECURSIVE_MODE="false"
# Initial command playback delay (in seconds, fractions are supported; '0' ==
# no delay)
PLAYBACK_DELAY="0.25"
# Options passed to 'sort' when sorting file/folder listings (see 'man sort'
# for valid/available options)
SORT_OPTS=("-V")
# Load/save command history from/to file on startup/exit ('true'/'false')
PERSISTENT_HISTORY="true"
# File used to store command history (only if PERSISTENT_HISTORY is enabled)
# ${APP_DIR}: directory where app executable ('infiren.sh') is stored
# ${APP_NAME}: name of app executable ('infiren.sh') without extension
# ${HOME}: home directory of user running/executing the application
#HISTORY_FILE="${HOME}/.config/${APP_NAME}/${APP_NAME}.hst"
HISTORY_FILE="${APP_DIR}/${APP_NAME}.hst"
# File used to store macros (managed via commands 'save-macro'/'load-macro')
# ${APP_DIR}: directory where app executable ('infiren.sh') is stored
# ${APP_NAME}: name of app executable ('infiren.sh') without extension
# ${HOME}: home directory of user running/executing the application
#MACROS_FILE="${HOME}/.config/${APP_NAME}/${APP_NAME}.mac"
MACROS_FILE="${APP_DIR}/${APP_NAME}.mac"
# --------------------------------------
# -
# Functions -
# -
# --------------------------------------
# Print normal/hilite/good/warn/error/debug message [$*: message]
# NOTE: warnings/errors are NOT sent to stderr (as script is interactive)
function printn() { echo -e "$*"; }
function printh() { echo -e "\e[1m$*\e[0m"; }
function printg() { echo -e "\e[1;32m$*\e[0m"; }
function printw() { echo -e "\e[1;33m$*\e[0m"; }
function printe() { echo -e "\e[1;31m$*\e[0m"; }
function printd() { echo -e "\e[1;30m$*\e[0m"; }
# Ask user yes/no question [$1: question, $2: newline before ('true'/'false';
# default: 'true'), $3: newline after ('true'/'false'; default: 'false')]
# Return value: 0 == yes, 1 == no
function ask_yes_no() {
local input result
[[ "${2:-"true"}" == "true" ]] && echo
echo -en "\e[1;33m$1 [y/n]:\e[0m "
while true; do
read -s -n 1 input
case "${input}" in
y|Y) echo -e "\e[1;33myes\e[0m"; result=0; break; ;;
n|N) echo -e "\e[1;33mno\e[0m"; result=1; break; ;;
esac
done
[[ "${3:-"false"}" == "true" ]] && echo
return ${result}
}
# Ask user to hit ENTER to continue [$1: name of print function, $2: message]
# Return value: 0 == ENTER, 1 == CTRL+D
function ask_hit_enter() {
"$1" "$2"
read -s || return 1
return 0
}
# Generate list of files in current directory [$1: name of target array, $2:
# recursive mode, $3: filter invert, $4: filter case, $5: filter pattern]
# NOTE:
# Using workaround to account for non-GNU find, which does NOT support option
# '-printf': without '-printf', find results are prepended with './', which
# messes up sorting (version sort in particular) and thus needs to be stripped
# BEFORE sorting is performed; leaving original solution as comment for future
# reference
function generate_file_list() {
local _recursive_opts=() _filter_opts=()
[[ "$2" == "false" ]] && _recursive_opts+=("-maxdepth" "1")
[[ "$3" == "true" ]] && _filter_opts+=("-not")
[[ "$4" == "true" ]] && _filter_opts+=("-name" "$5") || _filter_opts+=("-iname" "$5")
#readarray -t "$1" < <(find . -mindepth 1 "${_recursive_opts[@]}" -type f "${_filter_opts[@]}" -printf "%P\n" | sort "${SORT_OPTS[@]}")
readarray -t "$1" < <(find . -mindepth 1 "${_recursive_opts[@]}" -type f "${_filter_opts[@]}" | cut -c 3- | sort "${SORT_OPTS[@]}")
}
# Generate list of folders in current directory [$1: name of target array]
# NOTE:
# Using workaround to account for non-GNU find, which does NOT support option
# '-printf': without '-printf', find results are prepended with './', which
# messes up sorting (version sort in particular) and thus needs to be stripped
# BEFORE sorting is performed; leaving original solution as comment for future
# reference; using '-mindepth 1' to keep '.' out of find results
function generate_folder_list() {
#readarray -t "$1" < <(find . -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort "${SORT_OPTS[@]}")
readarray -t "$1" < <(find . -mindepth 1 -maxdepth 1 -type d | cut -c 3- | sort "${SORT_OPTS[@]}")
}
# Print array (one line per element) [$1: name of array, $2: display indices
# (true/false)]
function print_array() {
local -n _array="$1"
if [[ "$2" == "true" ]]; then
local _i _iw
_iw=${#_array[@]}; _iw=${#_iw}
for (( _i=0; _i < ${#_array[@]}; _i++ )); do
#printf "%0${_iw}d) %s\n" $((_i+1)) "${_array[_i]}"
#printf "%-${_iw}d) %s\n" $((_i+1)) "${_array[_i]}"
printf "%${_iw}d) %s\n" $((_i+1)) "${_array[_i]}"
done
else
local _item
for _item in "${_array[@]}"; do echo "${_item}"; done
fi
}
# Copy array [$1: name of source array, $2: name of target array]
function copy_array() {
local -n _srcarr="$1"
local -n _dstarr="$2"
_dstarr=("${_srcarr[@]}")
}
# Compare two arrays [$1: name of array 1, $2: name of array 2]
# Return value: 0 == equal, 1 == not equal
# NOTE: arrays are equal if same amount of items and all items match
function compare_arrays() {
local -n _array1="$1"
local -n _array2="$2"
local _i
(( ${#_array1[@]} == ${#_array2[@]} )) || return 1
for (( _i=0; _i < ${#_array1[@]}; _i++ )); do
[[ "${_array1[_i]}" == "${_array2[_i]}" ]] || return 1
done
return 0
}
# Print command about to be played back [$1: cmd, $2: delay]
# Return value: 0 == continue playback, 1 == abort playback
# NOTE:
# - 'if ! read ...; then if (( $? == 1 )); then ... fi; fi' will NOT work as
# expected as $? will always be 0, thus second 'if' is never triggered (not
# sure why); using 'read ... || { (( $? == 1 )) && ...; }' instead, which
# works as expected
# - For delay == 0, 'read -t 0 ...' would return 1, which would trip CTRL+D
# detection -> added shortcut (which relies on normalized delay argument,
# e.g. '000.000' or '0.0' -> normalized to just '0') to bypass read calls
function playback_print() {
local cmd="$1" delay="$2"
[[ "${delay}" == "0" ]] && { echo "${PROMPT_CMD}${cmd}"; return 0; } # shortcut for delay == 0
echo -n "${PROMPT_CMD}" # print command prompt
read -s -t "${delay}" || { (( $? == 1 )) && return 1; } # delay, detect CTRL-D (abort playback)
echo -n "${cmd}" # print command
read -s -t "${delay}" || { (( $? == 1 )) && return 1; } # delay, detect CTRL-D (abort playback)
echo
return 0
}
# Split string into array [$1: string, $2: separator character, $3: escape
# character, $4: maximum items, $5: name of target array]
# NOTE:
# - Maximum items: splitting ends after this many items, rest of string is
# stored as last item in array; set to 0 to disable (i.e. split untils EOS)
# - Escape character: character used for escaping (usually '\'); set to empty
# string to disable escaping
function split_string() {
local _string="$1" _schar="$2" _echar="$3" _maxitems="$4"
local -n _dstarr="$5"; _dstarr=()
local _i _char="" _item="" _items=0 _escape=0 _quote=0 _qchar=""
for (( _i=0; _i < ${#_string}; _i++ )); do
_char="${_string:_i:1}"
if (( ${_escape} == 1 )); then
_item+="${_char}"
_escape=0
continue
fi
if [[ -n "${_echar}" && "${_char}" == "${_echar}" ]] && (( ${_quote} == 0 )); then
_escape=1
continue
fi
if [[ "${_char}" == "\"" || "${_char}" == "'" ]]; then
if (( ${_quote} == 0 )); then
_qchar="${_char}"
_quote=1
continue
elif [[ "${_char}" == "${_qchar}" ]]; then
_quote=0
continue
fi
fi
if [[ "${_char}" == "${_schar}" ]] && (( ${_quote} == 0 )); then
#[[ -n "${_item}" ]] && _dstarr+=("${_item}")
_dstarr+=("${_item}")
_item=""
_items=$((_items + 1))
if (( ${_maxitems} > 0 && ${_items} >= ${_maxitems} )); then
_item="${_string:_i+1}"
break
fi
continue
fi
_item+="${_char}"
done
[[ -n "${_item}" ]] && _dstarr+=("${_item}") || : # '|| :' is required for this to work with 'set -e' (could use 'return 0' instead)
}
# Regular expression string replace [$1: input string, $2: regex to search
# for and replace, $3: replacement template, $4: name of output variable]
# NOTE:
# - Replacement template uses KDE's Kate's syntax format, for example:
# string '10x04', regex '([0-9]{2})x([0-9]{2})', template 'S\1E\2'
# -> output 'S10E04'
# - Replacing behavior is same as KDE's Kate with a minor difference:
# references to non-existent groups are replaced with empty strings
# (seemed more logical this way)
# - Does currently NOT account for group references with leading zeros
# (e.g. '\001'); would require replacing '${BASH_REMATCH[_grpidx]:-}'
# with '${BASH_REMATCH[$((10#_grpidx))]:-}'
# - Based on:
# 'Development/Shell/Playground/bash_regex_replace_all_occurrences.sh'
function replace_regex() {
local _in="$1" _re="$2" _reptmp="$3"
local -n _out="$4"; _out=""
local _repstr _escape _grpidx _i _char _match
if [[ -z "${_re}" ]]; then # trivial case: empty regex -> output = input, no further processing
_out="${_in}"
return
fi
while [[ -n "${_in}" ]] && [[ "${_in}" =~ ${_re} ]]; do # loop while there is still input left and regex matches
_repstr=""; _escape=0; _grpidx="" # generate replacement string from replacement template and regex matches
for (( _i=0; _i < ${#_reptmp}; _i++ )); do # by processing template character by character
_char="${_reptmp:${_i}:1}"
if (( ${_escape} == 1 )); then # if escaping is active and current character is ...
if [[ "${_char}" == [0-9] ]]; then # ... a digit:
_grpidx+="${_char}" # found group reference (e.g. '\12') -> add digit to group index, cycle loop
continue
elif [[ "${_char}" == "\\" ]]; then # ... a backslash:
if (( ${#_grpidx} > 0 )); then # if there is a group index: found backslash after group reference (e.g. '\12\')
_repstr+="${BASH_REMATCH[_grpidx]:-}" # -> add group match to output, reset group index, leave escaping active
_grpidx=""
else # if there is no group index: found escaped backslash (i.e. '\\')
_repstr+="\\" # -> add backslash to output, disable escaping
_escape=0
fi
continue # cycle loop
else # ... something else:
if (( ${#_grpidx} > 0 )); then # if there is a group index: found end of group reference (e.g. '\12x' at 'x')
_repstr+="${BASH_REMATCH[_grpidx]:-}" # -> add group match to output
else # if there is no group index: found non-group-reference escape sequence (e.g. '\r')
_repstr+="\\" # -> add backslash to output
fi
_escape=0 # disable escaping, continue normally
fi
fi
if [[ "${_char}" = "\\" ]]; then # if current character is a backslash: found start of escape sequence (i.e. '\...')
_escape=1 # -> enable escaping, reset group index, cycle loop
_grpidx=""
continue
fi
_repstr+="${_char}" # add current character to replacement string
done
if (( ${_escape} == 1 )); then # same as 'something else' above for end of template (condensed to single line)
(( ${#_grpidx} > 0 )) && _repstr+="${BASH_REMATCH[_grpidx]:-}" || _repstr+="\\" # '||' part: treat trailing backslash as literal backslash
fi
_match="${BASH_REMATCH[0]}" # substring matching ENTIRE regex -> to be replaced with replacement string
if [[ "${_re:0:1}" == "^" ]]; then # special handling for regex starting with '^' (e.g. re='^' or re='^.')
_out="${_repstr}${_in#*"${_match}"}" # output is replacement string + substring after match
_in="" # no futher input, break loop
break
elif [[ "${_re: -1}" == "\$" ]]; then # special handling for regex ending with '$' (e.g. re='$' or re='.$')
_out="${_in%"${_match}"*}${_repstr}" # output is substring before match + replacement string
_in="" # no futher input, break loop
break
fi
if [[ -z "${_match}" ]]; then # if match is empty, prevent endless loop by advancing processing by the smallest
_out+="${_in:0:1}" # amount possible (i.e. move one character from input to output and cycle loop);
_in="${_in:1}" # empty matches are quite common, e.g. in='abc123def457ghi', re='[0-9]*'
continue
fi
_out+="${_in%%"${_match}"*}${_repstr}" # add substring before match + replacement string to output
_in="${_in#*"${_match}"}" # substring after match is input for next loop iteration
done
_out+="${_in}" # add remainder of input string to output
}
# Replace single dots with spaces [$1: string, $2: name of output variable]
function replace_dots() {
local _in="$1"
local -n _out="$2"; _out=""
local _i _j _char="" _dots=0
for (( _i=0; _i < ${#_in}; _i++ )); do
_char="${_in:_i:1}"
if [[ "${_char}" == "." ]]; then
_dots=$((_dots + 1))
continue
else
(( ${_dots} == 1 )) && _out+=" " || for ((_j=0; _j < _dots; _j++)); do _out+="."; done
_dots=0
fi
_out+="${_char}"
done
(( ${_dots} == 1 )) && _out+=" " || for ((_j=0; _j < _dots; _j++)); do _out+="."; done
}
# Save macro to macro file [$1: macro file, $2: macro name, $3..$n: macro
# contents]
# Return value:
# 0 == macro saved (when saving) / macro deleted (when deleting)
# 1 == error occurred
# 2 == saving aborted (when saving) / macro not found (when deleting)
# NOTE: if NO macro contents are provided, macro is DELETED from macro file
function save_macro() {
local file="$1" name="$2" contents=("${@:3}")
local lines=() i starti=-1 endi=-1
if [[ -f "${file}" ]]; then
readarray -t lines < "${file}" || return 1
for ((i=0; i < "${#lines[@]}"; i++)); do
if (( ${starti} == -1 )); then
[[ "${lines[i]}" == "[${name}]" ]] && starti=$i # '[...]' -> start of macro
continue
fi
[[ "${lines[i]}" == "" ]] && { endi=$i; break; } # empty line -> end of macro
done
fi
if (( ${#contents[@]} > 0 )); then # macro non-empty? -> replace or append macro
contents=("${contents[@]//"["/"\["}"); contents=("${contents[@]//"]"/"\]"}") # escape square brackets
if (( ${starti} != -1 && ${endi} != -1 )); then
ask_yes_no "Macro '${name}' already exists. Overwrite?" "false" || return 2 # replace macro
lines=("${lines[@]:0:starti}" "[${name}]" "${contents[@]}" "" "${lines[@]:endi+1}")
else
lines+=("[${name}]") # append macro
lines+=("${contents[@]}")
lines+=("")
fi
else # no macro contents -> delete macro
(( ${starti} != -1 && ${endi} != -1 )) || return 2 # macro not found
lines=("${lines[@]:0:starti}" "${lines[@]:endi+1}") # delete macro
fi
mkdir -p -- "$(dirname -- "${file}")" && printf "%s\n" "${lines[@]}" > "${file}" || return 1
return 0
}
# Load macro from macro file [$1: macro file, $2: macro name, $3: name of
# target array (macro contents)]
# Return value: 0 == macro loaded, 1 == error occurred, 2 == macro not found
function load_macro() {
local _file="$1" _name="$2"
local -n _dstarr="$3" #; _dstarr=()
local _line _contents=() _gotit="false"
#[[ ! -f "${_file}" ]] && return 1 # no macro file -> nothing to load macro from (DISABLED as this would mask errors)
while read -r _line; do
if [[ "${_gotit}" == "false" ]]; then
[[ "${_line}" == "[${_name}]" ]] && _gotit="true" # '[...]' -> start of macro
continue
fi
[[ "${_line}" == "" ]] && break # empty line -> end of macro
_line="${_line//"\["/"["}"; _line="${_line//"\]"/"]"}" # unescape square brackets
_contents+=("${_line}")
done < "${_file}" || return 1
[[ "${_gotit}" == "false" ]] && return 2 # macro not found
_dstarr=("${_contents[@]}") # assign macro contents to target variable
return 0
}
# Delete macro from macro file [$1: macro file, $2: macro name]
# Return value: 0 == macro deleted, 1 == error occurred, 2 == macro not found
# NOTE: convenience wrapper for 'save_macro()' for the sake clarity
function delete_macro() {
save_macro "$1" "$2" && return $? || return $?
}
# List macros stored in macro file [$1: macro file, $2: name of target array
# (to store listing lines)]
# Return value: 0 == output generated, 1 == error occurred
function list_macros() {
local _file="$1"
local -n _dstarr="$2" #; _dstarr=()
local _line _output=()
#[[ ! -f "${_file}" ]] && return 0 # no macro file -> no macros to list (DISABLED as this would mask errors)
while read -r _line; do
[[ "${_line}" =~ ^\[(.+)\]$ ]] && { _output+=("Macro '${BASH_REMATCH[1]}':"); continue; }
_line="${_line//"\["/"["}"; _line="${_line//"\]"/"]"}" # unescape square brackets
_output+=("${_line}")
done < "${_file}" || return 1
_dstarr=("${_output[@]::${#_output[@]}-1}") # assign output to target variable, exclude last line (which is empty)
return 0
}
# --------------------------------------
# -
# Initialization -
# -
# --------------------------------------
# Usage information requested? (NOTE: this refers to the command line usage
# information, NOT to the interactive commands usage information, which may
# be displayed via command 'help'/'usage')
if [[ -n "${1+set}" ]] && [[ "$1" == "-h" || "$1" == "--help" ]]; then
printn "\e[1mUsage:\e[0m ${0##*/} [INITIAL-DIRECTORY] [CMD]..."
printn "\e[1mNote:\e[0m Commands are executed right after startup"
exit 0
fi
# Load configuration from file
if ! source "${APP_CONFIG}"; then
printe "Error: failed to load configuration from '${APP_CONFIG}', aborting."
exit 1
fi
# Process command line (TODO: implement command line parser and offer command
# line options to allow changing all items currently configurable via config
# file -> augment config variables)
[[ -n "${1+set}" ]] && { INITIAL_DIRECTORY="$1"; shift; }
#
# TODO:
# Check, verify and normalize config settings/items here; sort options can be
# verified by running 'sort "${SORT_OPTS[@]}" <<< ""' and checking exit code;
# especially needed for PLAYBACK_DELAY, which needs to be normalized before
# being passed to 'playback_print()'
#
# Change directory to initial directory (if specified/set) and then run 'cd .'
# to reset initial destination of 'cd -'
if [[ "${INITIAL_DIRECTORY}" != "" ]] && ! cd -- "${INITIAL_DIRECTORY}"; then
printe "Error: failed to change to initial directory '${INITIAL_DIRECTORY}', aborting."; exit 1
fi
cd .
# Load command history from file (if enabled)
if [[ "${PERSISTENT_HISTORY}" == "true" && -f "${HISTORY_FILE}" ]]; then
history -r -- "${HISTORY_FILE}" || { printe "Error: failed to load command history from '${HISTORY_FILE}', aborting."; exit 1; }
fi
# Initialize reset lists flag
reset_lists="true"
# Initialize error/info message storage
errors=()
infos=()
# Initialize macro storage/state
macro_storage=()
macro_record="false"
# Initialize filter state
filter_pattern="${FILTER_PATTERN}"
filter_invert="${FILTER_INVERT}"
filter_case="${FILTER_CASE}"
# Initialize recursive mode state
recursive_mode="${RECURSIVE_MODE}"
# Initialize command playback buffer/state, set up playback of commands
# specified on command line
playback_delay="${PLAYBACK_DELAY}"
playback_buffer=("$@")
playback_index=0
(( ${#playback_buffer[@]} > 0 )) && playback_enabled="true" || playback_enabled="false"
# Set up exit handler (for cosmetic reasons) and CTRL+C handler (for read
# calls, see https://stackoverflow.com/a/63713771/1976617)
trap "printn" EXIT
trap ":" INT
# --------------------------------------
# -
# Main -
# -
# --------------------------------------
# Command input loop
infos=("Enter 'help' to display available commands.")
while true; do
# Clear screen and print application header
clear
printh "--==[ ${APP_TITLE} v${APP_VERSION} ]==--"
printn
# Reset folder and file lists if requested
if [[ "${reset_lists}" == "true" ]]; then
generate_folder_list folders
generate_file_list files_in "${recursive_mode}" "${filter_invert}" "${filter_case}" "${filter_pattern}"
copy_array files_in files_out
copy_array files_out files_undo
copy_array files_out files_backup # back up file list (allows undo/redo of played back commands up to last reset -or- macro play start)
reset_lists="false"
fi
# List files and folders, print cwd
printh "Files (${#files_out[@]}):"
(( ${#files_out[@]} > 0 )) && print_array files_out true || printn "<no files>"
printn
printh "Folders (${#folders[@]}):"
(( ${#folders[@]} > 0 )) && print_array folders false || printn "<no folders>"
printn
printh "Path (CWD):"
pwd
printn
# Display error/info messages
if (( ${#errors[@]} > 0 )); then
for line in "${errors[@]}"; do printe "${line}"; done
printn
errors=()
fi
if (( ${#infos[@]} > 0 )); then
for line in "${infos[@]}"; do printw "${line}"; done
printn
infos=()
fi
# Currently playing back commands?
if [[ "${playback_enabled}" == "false" ]]; then
# Prompt user for command input
input=$(read -e -r -p "${PROMPT_CMD}" input && echo "${input}") || { # https://stackoverflow.com/a/63713771/1976617
case $? in
1) echo -e "\e[A\e[2K${PROMPT_CMD}ctrl+d (exit)"; input="exit"; ;; # CTRL+D is treated same as 'exit'/'quit' (i.e. graceful exit)
130) echo -e "\r\e[2K${PROMPT_CMD}ctrl+c (abort)"; break; ;; # CTRL+C exits right away (ignoring unsaved changes)
esac
}
[[ "${input}" == "" ]] && continue # take shortcut if there was no input
[[ "${input}" != "exit" && "${input}" != "quit" ]] && history -s -- "${input}" # add input to history
else
# End of playback buffer reached?
if (( ${playback_index} >= ${#playback_buffer[@]} )); then
infos=("Command playback finished (${#playback_buffer[@]} commands).")
playback_enabled="false"
playback_buffer=()
copy_array files_backup files_undo # write file list backup to undo list (allows undo/redo of played back commands up to last reset -or- macro play start)
continue
fi
# Use next item from buffer as command input
printw "Playing back commands (command $((playback_index+1)) of ${#playback_buffer[@]}, delay ${playback_delay}s), hit CTRL+D to abort..."
printn # using 'printw' instead of 'infos' here to have only one single place where this needs to be done
input="${playback_buffer[${playback_index}]}"
playback_index=$((playback_index + 1))
if ! playback_print "${input}" "${playback_delay}"; then # CTRL+D -> abort playback
infos=("Command playback aborted (at command ${playback_index} of ${#playback_buffer[@]}).")
playback_enabled="false"
playback_buffer=()
copy_array files_backup files_undo # write file list backup to undo list (allows undo/redo of played back commands up to last reset -or- macro play start)
continue
fi
fi
# Evaluate command input
split_string "${input}" " " "" 0 array # split input into array, escaping disabled
cmd="${array[0]}"; args=("${array[@]:1}") # slice array into command and arguments
case "${cmd}" in
# Editing commands
replace-string|rs| \
replace-regex|re| \
prepend|pre|pr| \
append|post|po| \
replace-dots|rd| \
insert-dash|id| \
capitalize|ca| \
uppercase|upper|up| \
lowercase|lower|lo| \
trim|tr|strip|st) # all of these commands affect file names
[[ "${macro_record}" == "true" ]] && macro_storage+=("${input}") # add raw input to macro if currently recording
copy_array files_out files_undo # save undo data
for (( i=0; i < ${#files_out[@]}; i++ )); do
[[ -n "${error+set}" ]] && break # break loop if error was set in previous iteration
file="${files_out[i]}"
[[ "${file}" == */* ]] && dir="${file%/*}/" || dir="" # extract leading directory part if existing
#name="${file##*/}"
name="${file##*/}"; name="${name%.*}" # extract file name part (without directory and extension)
[[ "${file}" == *.* ]] && ext=".${file##*.}" || ext="" # extract trailing extension part if existing
case "${cmd}" in
replace-string|rs)
if (( ${#args[@]} < 1 || ${#args[@]} > 2 )); then
errors=("Error: replace-string: invalid number of arguments (expected 1 or 2, got ${#args[@]})")
continue
fi
name="${name//"${args[0]}"/"${args[1]:-}"}" # using '${args[1]:-}' to not trip 'set -u' for optional argument
;;
replace-regex|re)
if (( ${#args[@]} < 1 || ${#args[@]} > 2 )); then
errors=("Error: replace-regex: invalid number of arguments (expected 1 or 2, got ${#args[@]})")
continue
fi
replace_regex "${name}" "${args[0]}" "${args[1]:-}" name # using '${args[1]:-}' to not trip 'set -u' for optional argument
;;
prepend|pre|pr)
if (( ${#args[@]} != 1 )); then
errors=("Error: prepend: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
name="${args[0]}${name}"
;;
append|post|po)
if (( ${#args[@]} != 1 )); then
errors=("Error: append: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
name="${name}${args[0]}"
;;
replace-dots|rd)
replace_dots "${name}" name
;;
insert-dash|id)
re="([^[:space:]]+)[[:space:]]+(.*)"
#[[ "${name}" =~ $re && "${BASH_REMATCH[1]: -1}" != "-" && "${BASH_REMATCH[2]:0:1}" != "-" ]] && name="${BASH_REMATCH[1]} - ${BASH_REMATCH[2]}"
[[ "${name}" =~ $re && "${BASH_REMATCH[1]: -1}" != "-" && "${BASH_REMATCH[2]:0:1}" != "-" ]] && name="${name//"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]} - ${BASH_REMATCH[2]}"}"
;;
capitalize|ca)
name_new=""
for word in ${name}; do
char="${word:0:1}"
word="${char^^}${word:1:${#word}}"
(( ${#name_new} == 0 )) && name_new="$word" || name_new="${name_new} ${word}"
done
name="${name_new}"
;;
uppercase|upper|up)
name="${name^^}"
ext="${ext^^}"
;;
lowercase|lower|lo)
name="${name,,}"
ext="${ext,,}"
;;
trim|tr|strip|st)
name="${name#"${name%%[![:space:]]*}"}" # trim leading whitespace
name="${name%"${name##*[![:space:]]}"}" # trim trailing whitespace
;;
esac
#files_out[i]="${dir}${name}"
files_out[i]="${dir}${name}${ext}"
done
;;
# Macro commands (1)
record-macro|rm)
if [[ "${macro_record}" == "false" ]]; then
macro_record="true"
(( ${#macro_storage[@]} > 0 )) && infos=("Macro recording started (adding to existing contents).") || infos=("Macro recording started (macro is empty).")
else
macro_record="false"
if (( ${#macro_storage[@]} > 0 )); then
infos=("Macro recording stopped. Macro contents:")
for line in "${macro_storage[@]}"; do infos+=("${line}"); done
else
infos=("Macro recording stopped (macro is empty).")
fi
fi
;;
view-macro|vm)
if (( ${#macro_storage[@]} > 0 )); then
[[ "${macro_record}" == "true" ]] && infos=("Macro contents (still recording):") || infos=("Macro contents:")
for line in "${macro_storage[@]}"; do infos+=("${line}"); done
else
[[ "${macro_record}" == "true" ]] && infos=("Macro is empty (still recording).") || infos=("Macro is empty.")
fi
;;
clear-macro|cm)
macro_storage=()
[[ "${macro_record}" == "true" ]] && infos=("Macro contents cleared (still recording).") || infos=("Macro contents cleared.")
;;
play-macro|pm)
if [[ "${macro_record}" == "true" ]]; then
errors=("Error: play-macro: can't play back macro while recording")
continue
fi
if (( ${#macro_storage[@]} == 0 )); then
errors=("Error: play-macro: can't play back empty macro")
continue
fi
if [[ "${playback_enabled}" == "true" ]]; then # already playing back commands? -> inject macro contents into existing playback buffer stream
playback_buffer=("${playback_buffer[@]::${playback_index}}" "${macro_storage[@]}" "${playback_buffer[@]:${playback_index}}")
else # currently not playing back commands -> set up and start command playback
copy_array files_out files_backup # back up current file list to allow undo/redo of ENTIRE macro (see 'End of playback buffer reached?' above)
playback_buffer=("${macro_storage[@]}")
playback_index=0
playback_enabled="true"
fi
infos=("Starting playback of macro (${#macro_storage[@]} commands).")
;;
playback-delay|pd)
if (( ${#args[@]} != 1 )); then
errors=("Error: playback-delay: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
if ! [[ "${args[0]}" =~ ^[0-9]+$ || "${args[0]}" =~ ^[0-9]+\.[0-9]+$ ]]; then
errors=("Error: playback-delay: value argument must be positive integer or fraction")
continue
fi
if [[ "${args[0]}" =~ ^[0]+$ || "${args[0]}" =~ ^[0]+\.[0]+$ ]]; then # if specified delay == 0 (might be '000', '0.0' or '00.00') ...
playback_delay="0" # ... normalize to just '0' for easier handling within 'playback_print()'
else
playback_delay="${args[0]}"
fi
infos=("Command playback delay set to ${playback_delay}s.")
;;
# Macro commands (2)
save-macro|sm)
if (( ${#args[@]} != 1 )); then
errors=("Error: save-macro: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
if [[ "${args[0]}" == "" ]]; then
errors=("Error: save-macro: name argument must not be empty")
continue
fi
if (( ${#macro_storage[@]} == 0 )); then
errors=("Error: save-macro: can't save empty macro")
continue
fi
name="${args[0]}"; printn
if save_macro "${MACROS_FILE}" "${name}" "${macro_storage[@]}"; then
infos=("Saved macro '${name}' (${#macro_storage[@]} commands).")
else
if (( $? == 2 )); then
infos=("Saving macro '${name}' was aborted.")
continue
fi
#errors=("Error: save-macro: failed to save macro '${name}'")
printn; ask_hit_enter printe "Error: save-macro: failed to save macro '${name}', hit ENTER to continue" || :
fi
;;
load-macro|lm)
if (( ${#args[@]} != 1 )); then
errors=("Error: load-macro: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
if [[ "${args[0]}" == "" ]]; then
errors=("Error: load-macro: name argument must not be empty")
continue
fi
if (( ${#macro_storage[@]} > 0 )); then
ask_yes_no "Macro is non-empty, contents will be replaced. Continue?" || continue
fi
name="${args[0]}"; printn
if load_macro "${MACROS_FILE}" "${name}" macro_storage; then
infos=("Loaded macro '${name}':")
for line in "${macro_storage[@]}"; do infos+=("${line}"); done
else
if (( $? == 2 )); then
errors=("Error: load-macro: no macro named '${name}' found")
continue
fi
#errors=("Error: load-macro: failed to load macro '${name}'")
printn; ask_hit_enter printe "Error: load-macro: failed to load macro '${name}', hit ENTER to continue" || :
fi
;;
delete-macro|dm)
if (( ${#args[@]} != 1 )); then
errors=("Error: delete-macro: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
if [[ "${args[0]}" == "" ]]; then
errors=("Error: delete-macro: name argument must not be empty")
continue
fi
name="${args[0]}"; printn
if delete_macro "${MACROS_FILE}" "${name}"; then
infos=("Deleted macro '${name}'.")
else
if (( $? == 2 )); then
errors=("Error: delete-macro: no macro named '${name}' found")
continue
fi
#errors=("Error: delete-macro: failed to delete macro '${name}'")
printn; ask_hit_enter printe "Error: delete-macro: failed to delete macro '${name}', hit ENTER to continue" || :
fi
;;
list-macros|im)
printn
if list_macros "${MACROS_FILE}" infos; then
#(( ${#infos[@]} > 0 )) || infos=("No macros stored in macro file.")
(( ${#infos[@]} > 0 )) && infos=("Macros stored in macro file:" "" "${infos[@]}") || infos=("No macros stored in macro file.")
else
#errors=("Error: list-macros: failed to list macros")
printn; ask_hit_enter printe "Error: list-macros: failed to list macros, hit ENTER to continue" || :
fi
;;
# History commands
history-macro|hm)
if [[ "${macro_record}" == "true" ]]; then
errors=("Error: history-macro: can't create macro from history while recording macro")
continue
fi
macro_storage=()
re="^[0-9]+[* ] ([^[:space:]]+)(.*)$" # all this relies on IFS containing space character (!)
while read -r line; do
if [[ "${line}" =~ ${re} ]]; then
case "${BASH_REMATCH[1]}" in
replace-string|rs| \
replace-regex|re| \
prepend|pre|pr| \
append|post|po| \
replace-dots|rd| \
insert-dash|id| \
capitalize|ca| \
uppercase|upper|up| \
lowercase|lower|lo| \
trim|tr|strip|st) macro_storage+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}"); ;;
esac
fi
done < <(history)
if (( ${#macro_storage[@]} > 0 )); then
infos=("Created macro from history:")
for line in "${macro_storage[@]}"; do infos+=("${line}"); done
else
errors=("Error: history-macro: no editing commands in history to create macro from")
fi
;;
view-history|vh)
infos=("Command history:")
while read -r line; do infos+=("${line}"); done < <(history) # auto-trims output (relies on IFS containing space character)
;;
clear-history|ch)
history -c
infos=("Command history cleared.")
;;
# Filter commands
filter-pattern|fp)
if (( ${#args[@]} != 1 )); then
errors=("Error: filter-pattern: invalid number of arguments (expected 1, got ${#args[@]})")
continue
fi
if ! compare_arrays files_in files_out; then # if there are unsaved changes ...
ask_yes_no "There are unsaved changes. Continue anyway?" || continue # ... prompt user before continuing
fi
filter_pattern="${args[0]}"
reset_lists="true"
infos=("Filter pattern set to '${filter_pattern}'.")
;;
invert-filter|if)
if ! compare_arrays files_in files_out; then # if there are unsaved changes ...
ask_yes_no "There are unsaved changes. Continue anyway?" || continue # ... prompt user before continuing
fi
[[ "${filter_invert}" == "false" ]] && filter_invert="true" || filter_invert="false"
reset_lists="true"
#infos=("Filter invert set to '${filter_invert}'.")
[[ "${filter_invert}" == "false" ]] && infos=("Filter invert disabled.") || infos=("Filter invert enabled.")
;;
filter-case|fc)
if ! compare_arrays files_in files_out; then # if there are unsaved changes ...
ask_yes_no "There are unsaved changes. Continue anyway?" || continue # ... prompt user before continuing
fi
[[ "${filter_case}" == "false" ]] && filter_case="true" || filter_case="false"
reset_lists="true"
#infos=("Filter case set to '${filter_case}'.")
[[ "${filter_case}" == "false" ]] && infos=("Filter set to case insensitive.") || infos=("Filter set to case sensitive.")
;;
view-filter|vf)
#infos=("Filter state: pattern: '${filter_pattern}', invert: ${filter_invert}, case: ${filter_case}")
infos=("Filter state: pattern: '${filter_pattern}'")
[[ "${filter_invert}" == "false" ]] && infos[0]+=", invert: disabled" || infos[0]+=", invert: enabled"
[[ "${filter_case}" == "false" ]] && infos[0]+=", case: insensitive" || infos[0]+=", case: sensitive"
;;
reset-filter|rf)
if ! compare_arrays files_in files_out; then # if there are unsaved changes ...
ask_yes_no "There are unsaved changes. Continue anyway?" || continue # ... prompt user before continuing
fi
filter_pattern="*"
filter_invert="false"
filter_case="false"
reset_lists="true"