Debian Codegraph @

crazy_complete/bash_helpers.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]>
3
4 '''This module contains helper functions for Bash.'''
5
6 from .helpers import GeneralHelpers, ShellFunction
7
8 _VALUES = ShellFunction('values', r'''
9 local word append=0
10
11 [[ "$1" == "-a" ]] && { shift; append=1; }
12 [[ "$1" == "--" ]] && { shift; }
13
14 (( append )) || COMPREPLY=()
15
16 for word; do
17 if [[ "$word" == "$cur"* ]]; then
18 COMPREPLY+=("$(printf '%q' "$word")")
19 fi
20 done
21 ''')
22
23 _DEQUOTE = ShellFunction('dequote', r'''
24 local in="$1" len=${#1} i=0 result='' ___break_pos=-1 ___in_quotes=0
25
26 for ((; i < len; ++i)); do
27 case "${in:i:1}" in
28 "'")
29 ___in_quotes=1
30 for ((++i; i < len; ++i)); do
31 [[ "${in:i:1}" == "'" ]] && { ___in_quotes=0; break; }
32 result+="${in:i:1}"
33 done;;
34 '"')
35 ___in_quotes=1
36 for ((++i; i < len; ++i)); do
37 [[ "${in:i:1}" == '"' ]] && { ___in_quotes=0; break; }
38
39 if [[ "${in:i:1}" == '\' ]]; then
40 result+="${in:$((++i)):1}"
41 else
42 result+="${in:i:1}"
43 fi
44 done;;
45 '\')
46 result+="${in:$((++i)):1}";;
47 [$COMP_WORDBREAKS])
48 result+="${in:i:1}"
49 ___break_pos=${#result};;
50 *)
51 result+="${in:i:1}";;
52 esac
53 done
54
55 local -n ___RESULT=$2
56 local -n ___BREAK_POS=$3
57 local -n ___IN_QUOTES=$4
58 ___RESULT="$result"
59 ___BREAK_POS=$___break_pos
60 ___IN_QUOTES=$___in_quotes
61 ''')
62
63 _DEQUOTE_WORDS = ShellFunction('dequote_words', r'''
64 local word dequoted break_pos in_quotes
65
66 words_dequoted=()
67
68 for word in "${words[@]}"; do
69 dequote "$word" dequoted break_pos in_quotes
70 words_dequoted+=("$dequoted")
71 done
72 ''', ['dequote'])
73
74 _PARSE_LINE = ShellFunction('parse_line', r'''
75 local -n out="$1"
76 local in="$2" len=${#2} i=0 word='' active=0
77
78 if (( len == 0 )); then
79 out+=('')
80 return
81 fi
82
83 for ((i=0; i < len; ++i)); do
84 case "${in:i:1}" in
85 ' ')
86 if (( active )); then
87 out+=("$word")
88 word=''
89 active=0;
90 fi;;
91 '"')
92 for ((; i < len; ++i)); do
93 if [[ "${in:i:1}" == '\' ]]; then
94 word+="${in:$((++i)):1}"
95 else
96 word+="${in:i:1}"
97 [[ "${in:i:1}" == '"' ]] && break;
98 fi
99 done
100 active=1;;
101 "'")
102 for ((; i < len; ++i)); do
103 word+="${in:i:1}"
104 [[ "${in:i:1}" == "'" ]] && break;
105 done
106 active=1;;
107 '\')
108 word+='\'
109 word+="${in:$((++i)):1}"
110 active=1;;
111 *)
112 word+="${in:i:1}"
113 active=1;;
114 esac
115 done
116
117 if (( active )); then
118 out+=("$word")
119 elif [[ "${in:$((len - 1)):1}" == ' ' ]]; then
120 out+=('')
121 fi
122 ''')
123
124 _GET_PREFIX_SUFFIX_LEN = ShellFunction('get_prefix_suffix_len', r'''
125 local s1="$1"
126 local s2="$2"
127
128 local len1=${#s1}
129 local len2=${#s2}
130
131 local maxk=$len1
132 if (( len2 < maxk )); then
133 maxk=$len2
134 fi
135
136 local k=$maxk
137 while (( k > 0 )); do
138 local start=$((len1 - k))
139 local suf="${s1:start:k}"
140 local pre="${s2:0:k}"
141
142 if [[ "$suf" == "$pre" ]]; then
143 if (( len1 == k )); then
144 echo 0
145 else
146 echo $((len1-k))
147 fi
148
149 return
150 fi
151
152 ((k--))
153 done
154
155 echo ${#s1}
156 ''')
157
158 _COMMANDLINE_STRING = ShellFunction('commandline_string', r'''
159 local l line break_pos in_quotes
160 dequote "$cur" line break_pos in_quotes
161
162 local COMP_LINE_OLD="$COMP_LINE"
163 local COMP_POINT_OLD=$COMP_POINT
164 local COMP_WORDS_OLD=("${COMP_WORDS[@]}")
165 local COMP_CWORD_OLD=$COMP_CWORD
166
167 COMP_LINE="$line"
168 COMP_POINT=${#line}
169 COMP_WORDS=()
170 parse_line COMP_WORDS "$line"
171 COMP_CWORD=$(( ${#COMP_WORDS[@]} - 1 ))
172 COMPREPLY=()
173
174 #ifdef bash_completions_v_2_12
175 _comp_command_offset 0
176 #else
177 _command_offset 0
178 #endif
179 compopt -o nospace
180 compopt -o noquote
181
182 if (( break_pos >= 0 )); then
183 line="${line:break_pos}"
184 fi
185
186 if (( ${#COMPREPLY[@]} )); then
187 local len=$(get_prefix_suffix_len "$line" "${COMPREPLY[0]}")
188 line="${line:0:len}"
189
190 for i in "${!COMPREPLY[@]}"; do
191 local REPLY="${COMPREPLY[i]}"
192 if (( in_quotes )); then
193 COMPREPLY[i]="$line$REPLY"
194 else
195 COMPREPLY[i]="$(printf '%q' "$line$REPLY")"
196 fi
197 done
198 fi
199
200 COMP_LINE="$COMP_LINE_OLD"
201 COMP_POINT=$COMP_POINT_OLD
202 COMP_WORDS=("${COMP_WORDS_OLD[@]}")
203 COMP_CWORD=$COMP_CWORD_OLD
204 ''', ['dequote', 'parse_line', 'get_prefix_suffix_len'])
205
206 _EXEC = ShellFunction('exec', r'''
207 local item desc special="$COMP_WORDBREAKS\"'><=;|&({:\\\$\`"
208
209 while IFS=$'\t' read -r item desc; do
210 if [[ "$item" == "$cur"* ]]; then
211 [[ "$item" == *[$special]* ]] && item="$(printf '%q' "$item")"
212 COMPREPLY+=("$item")
213 fi
214 done < <(eval "$1")
215 ''')
216
217 _EXEC_FAST = ShellFunction('exec_fast', r'''
218 local item desc
219
220 while IFS=$'\t' read -r item desc; do
221 if [[ "$item" == "$cur"* ]]; then
222 COMPREPLY+=("$item")
223 fi
224 done < <(eval "$1")
225 ''')
226
227 _ARRAY_CONTAINS = ShellFunction('array_contains', r'''
228 local w='' search="$1"; shift;
229 for w; do [[ "$search" == "$w" ]] && return 0; done
230 return 1
231 ''')
232
233 _ARRAY_CONTAINS_NOCASE = ShellFunction('array_contains_nocase', r'''
234 local w='' search="${1,,}"; shift;
235 for w; do [[ "$search" == "${w,,}" ]] && return 0; done
236 return 1
237 ''')
238
239 _ARRAY_MATCH = ShellFunction('array_match', r'''
240 local shopt_arg='-u'
241 [[ "$1" == "-i" ]] && { shopt_arg='-s'; shift; }
242 [[ "$1" == "--" ]] && shift
243 local w='' regex="$1"; shift
244 (
245 shopt $shopt_arg nocasematch
246 for w; do [[ "$w" =~ $regex ]] && return 0; done
247 return 1
248 )
249 ''')
250
251 _REGEX_MATCH = ShellFunction('regex_match', r'''
252 local shopt_arg='-u'
253 [[ "$1" == "-i" ]] && { shopt_arg='-s'; shift; }
254 [[ "$1" == "--" ]] && shift
255 (shopt $shopt_arg nocasematch; [[ "$1" =~ $2 ]]; )
256 ''')
257
258 _VALUE_LIST = ShellFunction('value_list', r'''
259 #ifdef duplicates
260 local duplicates=0
261 [[ "$1" == '-d' ]] && { duplicates=1; shift; }
262 #endif
263 local separator="$1"; shift
264
265 compopt -o nospace
266
267 local cur_unquoted break_pos in_quotes
268 dequote "$cur" cur_unquoted break_pos in_quotes
269
270 if [[ -z "$cur_unquoted" ]]; then
271 COMPREPLY=("$@")
272 return
273 fi
274
275 local value having_value having_values=() remaining_values=()
276
277 IFS="$separator" read -r -a having_values <<< "$cur_unquoted"
278
279 #ifdef duplicates
280 if (( duplicates )); then
281 remaining_values=("$@")
282 else
283 for value; do
284 if ! array_contains "$value" "${having_values[@]}"; then
285 remaining_values+=("$value")
286 fi
287 done
288 fi
289 #else
290 for value; do
291 if ! array_contains "$value" "${having_values[@]}"; then
292 remaining_values+=("$value")
293 fi
294 done
295 #endif
296
297 COMPREPLY=()
298
299 local cur_stripped="$cur_unquoted"
300 if (( break_pos > -1 )); then
301 cur_stripped="${cur_stripped:break_pos}"
302 fi
303
304 if [[ "${cur_unquoted: -1}" == "$separator" ]]; then
305 for value in "${remaining_values[@]}"; do
306 COMPREPLY+=("$cur_stripped$value")
307 done
308 elif (( ${#remaining_values[@]} )); then
309 if array_contains "${having_values[-1]}" "$@"; then
310 COMPREPLY+=("$cur_stripped$separator")
311 elif (( ${#having_values[@]} )); then
312 local cur_last_value=${having_values[-1]}
313 cur_stripped="${cur_stripped%"$cur_last_value"}"
314
315 for value in "${remaining_values[@]}"; do
316 if [[ "$value" == "$cur_last_value"* ]]; then
317 COMPREPLY+=("$cur_stripped$value")
318 fi
319 done
320 fi
321 fi
322 ''', ['dequote', 'array_contains'])
323
324 _KEY_VALUE_LIST = ShellFunction('key_value_list', r'''
325 local sep1="$1"; shift
326 local sep2="$1"; shift
327 local -A funcs=()
328 local -A excludes=()
329 local i
330
331 for ((i=1; i <= $#; i += 3)); do
332 funcs["${@:i:1}"]="${@:i + 1:1}"
333 excludes["${@:i:1}"]="${@:i + 2:1}"
334 done
335
336 local strip_chars=''
337 [[ "$COMP_WORDBREAKS" == *"$sep1"* ]] && strip_chars+="$sep1"
338 [[ "$COMP_WORDBREAKS" == *"$sep2"* ]] && strip_chars+="$sep2"
339 [[ "${cur:0:1}" == '"' ]] && strip_chars=''
340 [[ "${cur:0:1}" == "'" ]] && strip_chars=''
341 local cur="$cur" break_pos in_quotes
342 dequote "$cur" cur break_pos in_quotes
343
344 if [[ -z "$cur" ]]; then
345 COMPREPLY=("${!funcs[@]}")
346 return
347 fi
348
349 local pair key value found_key cur_stripped="$cur"
350 local -a tmp having_pairs having_keys remaining_keys
351
352 IFS="$sep1" read -r -a having_pairs <<< "$cur"
353
354 for pair in "${having_pairs[@]}"; do
355 key="${pair%%"$sep2"*}"
356 IFS=' ' read -r -a tmp <<< "${excludes[$key]}"
357 having_keys+=("${tmp[@]}")
358 done
359
360 for key in "${!funcs[@]}"; do
361 found_key=0
362
363 for having_key in "${having_keys[@]}"; do
364 if [[ "$key" == "$having_key" ]]; then
365 found_key=1
366 break
367 fi
368 done
369
370 if (( ! found_key )); then
371 remaining_keys+=("$key")
372 fi
373 done
374
375 COMPREPLY=()
376
377 if [[ "${cur: -1}" == "$sep1" ]]; then
378 [[ -n "$strip_chars" ]] && cur_stripped="${cur_stripped##*[$strip_chars]}"
379
380 for key in "${remaining_keys[@]}"; do
381 COMPREPLY+=("$cur_stripped$key")
382 done
383 else
384 pair="${cur##*"$sep1"}"
385 if [[ "$pair" == *"$sep2"* ]]; then
386 key="${pair%%"$sep2"*}"
387 value="${pair#*"$sep2"}"
388 cur="$value"
389 ${funcs[$key]}
390
391 cur_stripped="${cur_stripped:0:$(( ${#cur_stripped} - ${#value} ))}"
392 if [[ -n "$strip_chars" ]]; then
393 cur_stripped="${cur_stripped##*[$strip_chars]}"
394 fi
395
396 for i in "${!COMPREPLY[@]}"; do
397 COMPREPLY[i]="$cur_stripped${COMPREPLY[i]}"
398 done
399 else
400 [[ -n "$strip_chars" ]] && cur_stripped="${cur_stripped##*[$strip_chars]}"
401 cur_stripped="${cur_stripped%"$pair"}"
402
403 for key in "${remaining_keys[@]}"; do
404 if [[ "$key" == "$pair"* ]]; then
405 COMPREPLY+=("$cur_stripped$key")
406 fi
407 done
408 fi
409 fi
410 ''', ['dequote'])
411
412 _KEY_VALUE_PAIR = ShellFunction('key_value_pair', r'''
413 local sep="$1"; shift
414 local -A funcs=()
415 local i
416
417 for ((i=1; i <= $#; i += 2)); do
418 funcs["${@:i:1}"]="${@:i + 1:1}"
419 done
420
421 local strip_chars=''
422 [[ "$COMP_WORDBREAKS" == *"$sep"* ]] && strip_chars+="$sep"
423 [[ "${cur:0:1}" == '"' ]] && strip_chars=''
424 [[ "${cur:0:1}" == "'" ]] && strip_chars=''
425 local cur="$cur" break_pos in_quotes
426 dequote "$cur" cur break_pos in_quotes
427
428 local key="${cur%%"$sep"*}" value="${cur#*"$sep"}" cur_stripped="$cur"
429
430 COMPREPLY=()
431
432 if [[ "$cur" != *"$sep"* ]]; then
433 values "${!funcs[@]}"
434 elif array_contains "$key" "${!funcs[@]}"; then
435 cur="$value"
436 ${funcs[$key]}
437
438 cur_stripped="${cur_stripped:0:$(( ${#cur_stripped} - ${#value} ))}"
439 if [[ -n "$strip_chars" ]]; then
440 cur_stripped="${cur_stripped##*[$strip_chars]}"
441 fi
442
443 for i in "${!COMPREPLY[@]}"; do
444 COMPREPLY[i]="$cur_stripped${COMPREPLY[i]}"
445 done
446 fi
447 ''', ['dequote', 'array_contains', 'values'])
448
449 _KEY_VALUE_PAIR_EXEC = ShellFunction('key_value_pair_exec', r'''
450 local sep="$1"
451 local func="$2"
452
453 local strip_chars=''
454 [[ "$COMP_WORDBREAKS" == *"$sep"* ]] && strip_chars+="$sep"
455 [[ "${cur:0:1}" == '"' ]] && strip_chars=''
456 [[ "${cur:0:1}" == "'" ]] && strip_chars=''
457 local cur="$cur" break_pos in_quotes
458 dequote "$cur" cur break_pos in_quotes
459
460 local key="${cur%%"$sep"*}" value="${cur#*"$sep"}" cur_stripped="$cur"
461
462 COMPREPLY=()
463
464 if [[ "$cur" != *"$sep"* ]]; then
465 exec "$func"
466 else
467 cur="$value"
468 exec "$func $key"
469
470 cur_stripped="${cur_stripped:0:$(( ${#cur_stripped} - ${#value} ))}"
471 if [[ -n "$strip_chars" ]]; then
472 cur_stripped="${cur_stripped##*[$strip_chars]}"
473 fi
474
475 for i in "${!COMPREPLY[@]}"; do
476 COMPREPLY[i]="$cur_stripped${COMPREPLY[i]}"
477 done
478 fi
479 ''', ['dequote', 'exec'])
480
481 _KEY_VALUE_LIST_EXEC = ShellFunction('key_value_list_exec', r'''
482 local sep1="$1"
483 local sep2="$2"
484 local func="$3"
485 local -A excludes=()
486 local -A takes_arg=()
487 local key desc exclude non_repeatable_exclude
488
489 while IFS=$'\t' read -r key desc exclude; do
490 local has_arg=false
491
492 if [[ "$key" == *'=' ]]; then
493 key="${key:0:-1}"
494 has_arg=true
495 elif [[ "$key" == *'=?' ]]; then
496 key="${key:0:-2}"
497 fi
498
499 if [[ "$key" == '*'* ]]; then
500 key="${key:1}"
501 non_repeatable_exclude=''
502 else
503 non_repeatable_exclude=" $key"
504 fi
505
506 takes_arg[$key]=$has_arg
507 excludes[$key]="$exclude$non_repeatable_exclude"
508 done < <($func)
509
510 local strip_chars=''
511 [[ "$COMP_WORDBREAKS" == *"$sep1"* ]] && strip_chars+="$sep1"
512 [[ "$COMP_WORDBREAKS" == *"$sep2"* ]] && strip_chars+="$sep2"
513 [[ "${cur:0:1}" == '"' ]] && strip_chars=''
514 [[ "${cur:0:1}" == "'" ]] && strip_chars=''
515 local cur="$cur" break_pos in_quotes
516 dequote "$cur" cur break_pos in_quotes
517
518 if [[ -z "$cur" ]]; then
519 COMPREPLY=("${!excludes[@]}")
520 return
521 fi
522
523 local pair key value found_key cur_stripped="$cur"
524 local -a tmp having_pairs having_keys remaining_keys
525
526 IFS="$sep1" read -r -a having_pairs <<< "$cur"
527
528 for pair in "${having_pairs[@]}"; do
529 key="${pair%%"$sep2"*}"
530 IFS=' ' read -r -a tmp <<< "${excludes[$key]}"
531 having_keys+=("${tmp[@]}")
532 done
533
534 for key in "${!excludes[@]}"; do
535 found_key=0
536
537 for having_key in "${having_keys[@]}"; do
538 if [[ "$key" == "$having_key" ]]; then
539 found_key=1
540 break
541 fi
542 done
543
544 if (( ! found_key )); then
545 remaining_keys+=("$key")
546 fi
547 done
548
549 COMPREPLY=()
550
551 if [[ "${cur: -1}" == "$sep1" ]]; then
552 [[ -n "$strip_chars" ]] && cur_stripped="${cur_stripped##*[$strip_chars]}"
553
554 for key in "${remaining_keys[@]}"; do
555 COMPREPLY+=("$cur_stripped$key")
556 done
557 else
558 pair="${cur##*"$sep1"}"
559 if [[ "$pair" == *"$sep2"* ]]; then
560 key="${pair%%"$sep2"*}"
561 value="${pair#*"$sep2"}"
562 cur="$value"
563 exec "$func $key"
564
565 cur_stripped="${cur_stripped:0:$(( ${#cur_stripped} - ${#value} ))}"
566 if [[ -n "$strip_chars" ]]; then
567 cur_stripped="${cur_stripped##*[$strip_chars]}"
568 fi
569
570 for i in "${!COMPREPLY[@]}"; do
571 COMPREPLY[i]="$cur_stripped${COMPREPLY[i]}"
572 done
573 else
574 [[ -n "$strip_chars" ]] && cur_stripped="${cur_stripped##*[$strip_chars]}"
575 cur_stripped="${cur_stripped%"$pair"}"
576
577 for key in "${remaining_keys[@]}"; do
578 if [[ "$key" == "$pair"* ]]; then
579 COMPREPLY+=("$cur_stripped$key")
580 fi
581 done
582 fi
583 fi
584 ''', ['dequote', 'exec'])
585
586 _PREFIX_COMPREPLY = ShellFunction('prefix_compreply', r'''
587 [[ "$cur" == *[$COMP_WORDBREAKS]* ]] && return
588
589 local i prefix="$1"
590 for ((i=0; i < ${#COMPREPLY[@]}; ++i)); do
591 COMPREPLY[i]="$prefix${COMPREPLY[i]}"
592 done
593 ''')
594
595 _HISTORY = ShellFunction('history', r'''
596 [[ -f "$HISTFILE" ]] || return
597
598 local match
599
600 while read -r match; do
601 if [[ "$match" == "$cur"* ]]; then
602 COMPREPLY+=("$(printf '%q' "$match")")
603 fi
604 done < <(command grep -E -o -- "$1" "$HISTFILE")
605 ''')
606
607 _GET_LAST_BREAK_POSITION = ShellFunction('get_last_break_position', r'''
608 local in="$1" i=0 len=${#1} pos=-1
609
610 for ((i=0; i < len; ++i)); do
611 case "${in:i:1}" in
612 '"')
613 for ((++i; i < len; ++i)); do
614 if [[ "${in:i:1}" == '\' ]]; then
615 ((++i))
616 elif [[ "${in:i:1}" == '"' ]]; then
617 break;
618 fi
619 done;;
620 "'")
621 for ((++i; i < len; ++i)); do
622 [[ "${in:i:1}" == "'" ]] && break;
623 done;;
624 '\')
625 ((++i));;
626 [$COMP_WORDBREAKS])
627 pos=$i;;
628 esac
629 done
630
631 echo $pos
632 ''')
633
634 _PREFIX = ShellFunction('prefix', r'''
635 local prefix="$1" func="$2"
636 local stripped="$(strip_prefix_keep_quoting "$prefix" "$cur")"
637
638 if [[ "$stripped" == "$cur" ]]; then
639 COMPREPLY=($(compgen -W "$1" -- "$cur"))
640 compopt -o nospace
641 return
642 fi
643
644 local break_position=$(get_last_break_position "$cur")
645
646 local cur_old="$cur" cur="$stripped"
647 $func
648
649 if (( break_position < 0 )); then
650 for i in "${!COMPREPLY[@]}"; do
651 COMPREPLY[i]="$prefix${COMPREPLY[i]}"
652 done
653 fi
654 ''', ['get_last_break_position', 'strip_prefix_keep_quoting'])
655
656 _STRIP_PREFIX_KEEP_QUOTING = ShellFunction('strip_prefix_keep_quoting', r'''
657 local p="$1" pi=0 plen=${#1}
658 local s="$2" si=0 slen=${#2} state=w
659
660 for (( pi=0; pi < plen; ++pi )); do
661 pc=${p:pi:1}
662 sc=''
663
664 while (( si < slen )); do
665 case "$state" in
666 w)
667 case "${s:si:1}" in
668 '"') state=dq; ((++si));;
669 "'") state=sq; ((++si));;
670 '\') ((++si)); sc="${s:si:1}"; ((++si)); break;;
671 *) sc="${s:si:1}"; ((++si)); break;;
672 esac;;
673 sq)
674 case "${s:si:1}" in
675 "'") state=w; ((++si));;
676 *) sc="${s:si:1}"; ((++si)); break;;
677 esac;;
678 dq)
679 case "${s:si:1}" in
680 '"') state=w; ((++si));;
681 '\') ((++si)); sc="${s:si:1}"; ((++si)); break;;
682 *) sc="${s:si:1}"; ((++si)); break;;
683 esac;;
684 esac
685 done
686
687 [[ "$pc" != "$sc" ]] && { echo "$s"; return; }
688 done
689
690 case "$state" in
691 w) printf '%s\n' "${s:si}";;
692 sq) printf '%s\n' "'${s:si}";;
693 dq) printf '%s\n' "\"${s:si}";;
694 esac
695 ''')
696
697 _MIME_FILE = ShellFunction('mime_file', r'''
698 local line file mime i_opt cur_dequoted
699
700 if command file -i /dev/null &>/dev/null; then
701 i_opt="-i"
702 elif command file -I /dev/null &>/dev/null; then
703 i_opt="-I"
704 else
705 #ifdef bash_completions_v_2_12
706 _comp_compgen_filedir
707 #else
708 _filedir
709 #endif
710 return
711 fi
712
713 local cur_dequoted break_pos in_quotes
714 dequote "$cur" cur_dequoted break_pos in_quotes
715
716 while read -r line; do
717 mime="${line##*:}"
718
719 if [[ "$mime" == *inode/directory* ]] || command grep -q -E -- "$1" <<< "$mime"; then
720 file="${line%:*}"
721
722 if [[ "$file" == *\\* ]]; then
723 file="$(command perl -pe 's/\\([0-7]{3})/chr(oct($1))/ge' <<< "$file")"
724 fi
725
726 if [[ "$mime" == *inode/directory* ]]; then
727 file="$file/"
728 fi
729
730 if [[ "$file" == "$cur_dequoted"* ]]; then
731 COMPREPLY+=("$(printf '%q' "$file")")
732 fi
733 fi
734 done < <(command file -L $i_opt -- "$cur_dequoted"* 2>/dev/null)
735 ''', ['dequote'])
736
737 _FILE_FILTER = ShellFunction('file_filter', '''
738 local pattern REPLY COMPREPLY_OLD=("${COMPREPLY[@]}")
739
740 COMPREPLY=()
741
742 for REPLY in "${COMPREPLY_OLD[@]}"; do
743 for pattern; do
744 [[ "${REPLY##*/}" == $pattern ]] && continue 2
745 done
746
747 COMPREPLY+=("$REPLY")
748 done
749 ''')
750
751 _NUMBER = ShellFunction('number', '''
752 local number="$(command grep -E -o '^[0-9,.]+' <<< "$cur")" suffix_and_desc
753
754 [[ -z "$number" ]] && return
755
756 COMPREPLY=("$number")
757
758 for suffix_and_desc; do
759 COMPREPLY+=("${number}${suffix_and_desc%%:*}")
760 done
761 ''')
762
763 # =============================================================================
764 # Bonus
765 # =============================================================================
766
767 _MOUNTPOINT = ShellFunction('mountpoint', r'''
768 local REPLY
769
770 COMPREPLY=()
771
772 while read -r REPLY; do
773 COMPREPLY+=("$(printf '%q' "$REPLY")")
774 done < <(command findmnt -lno TARGET)
775 ''')
776
777 _LOCALES = ShellFunction('''locales''', r'''
778 COMPREPLY=($(compgen -W "$(command locale -a)" -- "$cur"))
779 ''')
780
781 _CHARSETS = ShellFunction('''charsets''', r'''
782 COMPREPLY=($(compgen -W "$(command locale -m)" -- "$cur"))
783 ''')
784
785 _TIMEZONE_LIST = ShellFunction('timezone_list', r'''
786 if ! command timedatectl list-timezones 2>/dev/null; then
787 command find /usr/share/zoneinfo -type f |\
788 command sed 's|/usr/share/zoneinfo/||g' |\
789 command grep -E -v '^(posix|right)'
790 fi''')
791
792 _ALSA_LIST_CARDS = ShellFunction('alsa_list_cards', r'''
793 local card id cards=()
794 while builtin read card; do
795 card="${card#card }"
796 id="${card%%: *}"
797 cards+=("$id")
798 done < <(command aplay -l | command grep -Eo '^card [0-9]+: [^,]+')
799
800 COMPREPLY=($(compgen -W "${cards[*]}" -- "$cur"))
801 ''')
802
803 _ALSA_LIST_DEVICES = ShellFunction('alsa_list_devices', r'''
804 local card id devices=()
805 while builtin read card; do
806 card="${card#card }"
807 id="${card%%: *}"
808 devices+=("hw:$id")
809 done < <(command aplay -l | command grep -Eo '^card [0-9]+: [^,]+')
810
811 COMPREPLY=($(compgen -W "${devices[*]}" -- "$cur"))
812 ''')
813
814
815 class BashHelpers(GeneralHelpers):
816 '''Class holding helper functions for Bash.'''
817
818 def __init__(self, config, function_prefix):
819 super().__init__(config, function_prefix, ShellFunction)
820 self.add_function(_VALUES)
821 self.add_function(_DEQUOTE)
822 self.add_function(_DEQUOTE_WORDS)
823 self.add_function(_PREFIX)
824 self.add_function(_STRIP_PREFIX_KEEP_QUOTING)
825 self.add_function(_ARRAY_CONTAINS)
826 self.add_function(_ARRAY_CONTAINS_NOCASE)
827 self.add_function(_ARRAY_MATCH)
828 self.add_function(_REGEX_MATCH)
829 self.add_function(_GET_LAST_BREAK_POSITION)
830 self.add_function(_PARSE_LINE)
831 self.add_function(_GET_PREFIX_SUFFIX_LEN)
832 self.add_function(_COMMANDLINE_STRING)
833 self.add_function(_EXEC)
834 self.add_function(_EXEC_FAST)
835 self.add_function(_VALUE_LIST)
836 self.add_function(_KEY_VALUE_LIST)
837 self.add_function(_KEY_VALUE_PAIR)
838 self.add_function(_KEY_VALUE_LIST_EXEC)
839 self.add_function(_KEY_VALUE_PAIR_EXEC)
840 self.add_function(_FILE_FILTER)
841 self.add_function(_PREFIX_COMPREPLY)
842 self.add_function(_HISTORY)
843 self.add_function(_NUMBER)
844 self.add_function(_MIME_FILE)
845 self.add_function(_MOUNTPOINT)
846 self.add_function(_LOCALES)
847 self.add_function(_CHARSETS)
848 self.add_function(_TIMEZONE_LIST)
849 self.add_function(_ALSA_LIST_CARDS)
850 self.add_function(_ALSA_LIST_DEVICES)