Debian Codegraph @

crazy_complete/fish_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 Fish.'''
5
6 from .helpers import GeneralHelpers, FishFunction
7
8 _QUERY_INIT = FishFunction('query_init', r'''
9 # ===========================================================================
10 #
11 # This function implements the parsing of options and positionals in the
12 # Fish shell.
13 #
14 # Usage: query_init COMMANDLINE_DEFINITION...
15 #
16 # COMMANDLINE DEFINITION
17 # A command line definition has the format:
18 # POSITIONAL_PATTERN=>OPTIONS
19 #
20 # Definitions must be ordered from the least specific to the most specific
21 # (global options first, then subcommand options).
22 #
23 # POSITIONAL PATTERN
24 # The text before the '=>' is the positional pattern. An empty pattern
25 # matches the top-level command. Subcommands inside the positional-pattern
26 # are separated by '>'.
27 #
28 # OPTIONS
29 # The text after the '=>' are the options for the command. Options
30 # are separated by space.
31 #
32 # OPTION
33 # Short options (-o), long options (--option), and old-style options (-option)
34 # are supported.
35 #
36 # If an option takes an argument, it is suffixed by '='.
37 # If an option takes an optional argument, it is suffixed by '=?'.
38 #
39 # EXAMPLE
40 # set -l opts \
41 # '=>-f --flag -old-flag --with-arg= --with-optional=?' \
42 # '(subcommand|subcmd)=>--choices=' \
43 # '(subcommand|subcmd)>(subsub)=>--sub-sub-flag'
44 #
45 # query_init $opts
46 #
47 # Here, -f, --flag and -old-flag don't take options, --with-arg requires an
48 # argument and --with-optional takes an optional argument.
49 #
50 # If the first positional matches "subcommand" or "subcmd", --choices is
51 # available as an option.
52 #
53 # If, additionally, the seconds positional matches "subsub", --sub-sub-flag
54 # is available as an option.
55 #
56 # ===========================================================================
57 #
58 set -l definitions $argv
59 set -l positionals
60 #ifdef positional_position
61 set -l positionals_positions
62 #endif
63 set -l having_options
64 #ifdef short_options
65 set -l leading_stacked_short_options
66 #endif
67 set -l option_values
68 set -l last_arg_is_option_argument false
69
70 #ifdef subcommands
71 function %PREFIX%_match_positionals -S
72 set -l patterns (string split -- '>' "$argv[1]")
73 set -l i (count $patterns)
74
75 while test $i -ge 1
76 string match -q -r -- "^$patterns[$i]\$" "$positionals[$i]" || return 1
77 set i (math $i - 1)
78 end
79 end
80 #endif
81
82 function %PREFIX%_get_option -S
83 set -l option $argv[1]
84 set -l i (count $definitions)
85
86 while test $i -ge 1
87 set -l split (string split -m 1 -- '=>' $definitions[$i])
88 #ifdef subcommands
89 if %PREFIX%_match_positionals "$split[1]"
90 #else
91 if true
92 #endif
93 set -l opt
94 for opt in (string split -- ' ' $split[2])
95 test "$opt" = "$option" && begin echo '0'; return; end
96 test "$opt" = "$option=" && begin echo '1'; return; end
97 test "$opt" = "$option=?" && begin echo '?'; return; end
98 end
99 end
100
101 set i (math $i - 1)
102 end
103 end
104
105 set -l cmdline (commandline -poc)
106 set -l cmdline_count (count $cmdline)
107
108 set -l argi 2 # cmdline[1] is command name
109 while test $argi -le $cmdline_count
110 set -l arg "$cmdline[$argi]"
111 set -l have_trailing_arg (test $argi -lt $cmdline_count && echo true || echo false)
112 #ifdef short_options
113 set leading_stacked_short_options
114 #endif
115
116 switch $arg
117 case '-'
118 set -a positionals -
119 #ifdef positional_position
120 set -a positionals_positions $argi
121 #endif
122 case '--'
123 set -a positionals $cmdline[$(math $argi + 1)..]
124 #ifdef positional_position
125 set -a positionals_positions (seq (math $argi + 1) $cmdline_count)
126 #endif
127 break
128 case '--*=*'
129 set -l split (string split -m 1 -- '=' $arg)
130 set -a having_options $split[1]
131 set -a option_values "$split[2]"
132 case '--*'
133 #ifdef long_options
134 set -l option_type (%PREFIX%_get_option $arg)
135 if test "$option_type" = '1'
136 if $have_trailing_arg
137 set -a having_options $arg
138 set -a option_values $cmdline[(math $argi + 1)]
139 set argi (math $argi + 1)
140 else
141 set last_arg_is_option_argument true
142 end
143 else
144 set -a having_options $arg
145 set -a option_values ''
146 end
147 #endif
148 case '-*'
149 set -l end_of_parsing false
150 #ifdef old_options
151
152 if string match -q -- '*=*' $arg
153 set -l split (string split -m 1 -- '=' $arg)
154 set -l option_type (%PREFIX%_get_option $split[1])
155 if contains -- "$option_type" '1' '?'
156 set -a having_options $split[1]
157 set -a option_values "$split[2]"
158 set end_of_parsing true
159 end
160 else
161 set -l option_type (%PREFIX%_get_option $arg)
162 if test "$option_type" = '1'
163 set end_of_parsing true
164 if $have_trailing_arg
165 set -a having_options $arg
166 set -a option_values $cmdline[(math $argi + 1)]
167 set argi (math $argi + 1)
168 else
169 set last_arg_is_option_argument true
170 end
171 else if contains -- "$option_type" '0' '?'
172 set -a having_options $arg
173 set -a option_values ''
174 set end_of_parsing true
175 end
176 end
177 #endif
178 #ifdef short_options
179
180 set -l arg_length (string length -- $arg)
181 set -l i 2
182 while not $end_of_parsing; and test $i -le $arg_length
183 set -l option "-$(string sub -s $i -l 1 -- $arg)"
184 set -l trailing_chars "$(string sub -s (math $i + 1) -- $arg)"
185 set -l option_type (%PREFIX%_get_option $option)
186
187 if test "$option_type" = '0'
188 set -a having_options $option
189 set -a option_values ''
190 test $i -lt $arg_length && set -a leading_stacked_short_options $option
191 else if test "$option_type" = '1'
192 set end_of_parsing true
193
194 if test -n "$trailing_chars"
195 set -a having_options $option
196 set -a option_values $trailing_chars
197 else if $have_trailing_arg
198 set -a having_options $option
199 set -a option_values $cmdline[(math $argi + 1)]
200 set argi (math $argi + 1)
201 else
202 set last_arg_is_option_argument true
203 end
204 else if test "$option_type" = '?'
205 set end_of_parsing true
206 set -a having_options $option
207 set -a option_values "$trailing_chars" # may be empty
208 end
209
210 set i (math $i + 1)
211 end
212 #endif
213 case '*'
214 set -a positionals $arg
215 #ifdef positional_position
216 set -a positionals_positions $argi
217 #endif
218 end
219
220 set argi (math $argi + 1)
221 end
222
223 set -g __QUERY_CACHE_POSITIONALS $positionals
224 #ifdef positional_position
225 set -g __QUERY_CACHE_POSITIONALS_POSITIONS $positionals_positions
226 #endif
227 set -g __QUERY_CACHE_HAVING_OPTIONS $having_options
228 set -g __QUERY_CACHE_OPTION_VALUES $option_values
229 #ifdef short_options
230 set -g __QUERY_CACHE_LEADING_STACKED_OPTIONS $leading_stacked_short_options
231 #endif
232
233 set -l cmdline_last_arg (commandline -ct | string unescape)
234 set -g __QUERY_CACHE_CURRENT_ARG $cmdline_last_arg
235
236 $last_arg_is_option_argument && return
237
238 set -l split (string split -m1 -- '=' $cmdline_last_arg)
239 # `string split` returns 0 if it did split the string
240 if test $status -eq 0 && contains -- (%PREFIX%_get_option $split[1]) '1' '?'
241 set -g __QUERY_CACHE_CURRENT_ARG $split[2]
242 return
243 end
244 #ifdef short_options
245
246 set -l arg_length (string length -- $cmdline_last_arg)
247 set -l i 2
248 while test $i -le $arg_length
249 set -l option "-$(string sub -s $i -l 1 -- $cmdline_last_arg)"
250 if contains -- (%PREFIX%_get_option $option) '1' '?'
251 set -g __QUERY_CACHE_CURRENT_ARG (string sub -s (math $i + 1) -- $cmdline_last_arg)
252 return
253 end
254
255 set i (math $i + 1)
256 end
257 #endif
258 ''')
259
260 _POSITIONAL_CONTAINS = FishFunction('positional_contains', r'''
261 # positional_contains [-i] <NUM> <WORDS>
262 #
263 # Checks if the positional argument number NUM is one of WORDS.
264 # NUM counts from one.
265 #
266 #ifdef nocase
267 set -l nocase false
268 if test "$argv[1]" = -i; set nocase true; set -e argv[1]; end
269
270 #endif
271 #ifdef DEBUG
272 if test (count $argv) -eq 0
273 echo '%FUNCNAME%: missing number' >&2
274 return 1
275 end
276
277 #endif
278 set -l positional_num $argv[1]
279 set -e argv[1]
280 #ifdef nocase
281 if $nocase
282 contains -- (string lower -- $__QUERY_CACHE_POSITIONALS[$positional_num] $argv)
283 else
284 contains -- $__QUERY_CACHE_POSITIONALS[$positional_num] $argv
285 end
286 #else
287 contains -- $__QUERY_CACHE_POSITIONALS[$positional_num] $argv
288 #endif
289 ''', ['query_init'])
290
291 _POSITIONAL_MATCH = FishFunction('positional_match', r'''
292 # positional_match [-i] <NUM> <REGEX>
293 #
294 # Checks if the positional argument number NUM matches REGEX.
295 # NUM counts from one.
296 #
297 #ifdef nocase
298 set -l nocase_opt ''
299 if test "$argv[1]" = -i; set nocase_opt 'i'; set -e argv[1]; end
300
301 #endif
302 #ifdef DEBUG
303 if test (count $argv) -eq 0
304 echo '%FUNCNAME%: missing number' >&2
305 return 1
306 end
307
308 #endif
309 set -l positional_num $argv[1]
310 set -l regex $argv[2]
311
312 #ifdef nocase
313 string match -qr$nocase_opt $regex $__QUERY_CACHE_POSITIONALS[$positional_num]
314 #else
315 string match -qr $regex $__QUERY_CACHE_POSITIONALS[$positional_num]
316 #endif
317 ''', ['query_init'])
318
319 _HAS_OPTION = FishFunction('has_option', r'''
320 # has_option [WITH_INCOMPLETE] <OPTIONS>
321 #
322 # Checks if an option given in OPTIONS is passed on commandline.
323 # If an option requires an argument, this command returns true only if the
324 # option includes an argument. If 'WITH_INCOMPLETE' is specified, it also
325 # returns true for options missing their arguments.
326 #
327 set -l option
328 #ifdef with_incomplete
329 set -l with_incomplete false
330
331 if test $argv[1] = 'WITH_INCOMPLETE'
332 set with_incomplete true
333 set -e argv[1]
334 end
335
336 #endif
337 for option in $__QUERY_CACHE_LEADING_STACKED_OPTIONS
338 contains -- $option $argv && return 1
339 end
340
341 for option in $__QUERY_CACHE_HAVING_OPTIONS
342 contains -- $option $argv && return 0
343 end
344
345 #ifdef with_incomplete
346 if $with_incomplete
347 set -l tokens (commandline -po)
348 set -e tokens[1]
349 for option in $argv
350 if test 2 -eq (string length -- $option)
351 set option (string sub -l 1 -s 2 -- $option)
352 string match -rq -- "^-[A-z0-9]*$option\$" $tokens[-2] && return 0
353 string match -rq -- "^-[A-z0-9]*$option.*\$" $tokens[-1] && return 0
354 else
355 string match -q -r -- "^$option\$" $tokens[-2] && return 0
356 string match -q -r -- "^$option(=.*)?\$" $tokens[-1] && return 0
357 contains -- $tokens[-1] $argv && return 0
358 end
359 end
360 end
361
362 #endif
363 return 1
364 ''', ['query_init'])
365
366 _OPTION_IS = FishFunction('option_is', r'''
367 # option_is [-a] [-i] -- <OPTIONS...> -- <VALUES...>
368 #
369 # Checks if any option in OPTIONS has a value of VALUES.
370 #
371 #ifdef any
372 set -l any false
373 #endif
374 #ifdef nocase
375 set -l nocase false
376 #endif
377
378 while set -q argv[1]
379 #ifdef any
380 if test "$argv[1]" = -a; set any true; set -e argv[1]; continue; end
381 #endif
382 #ifdef nocase
383 if test "$argv[1]" = -i; set nocase true; set -e argv[1]; continue; end
384 #endif
385 if test "$argv[1]" = --; set -e argv[1]; break; end
386 end
387
388 set -l eof_string (contains -i -- -- $argv || math (count $argv) + 1)
389 set -l options $argv[1..$(math $eof_string - 1)]
390 set -l values $argv[$(math $eof_string + 1)..]
391 #ifdef nocase
392 set -l option_values $__QUERY_CACHE_OPTION_VALUES
393 $nocase && set values (string lower -- $values)
394 $nocase && set option_values (string lower -- $option_values)
395 #endif
396
397 #ifdef DEBUG
398 if test (count $options) -eq 0
399 echo '%FUNCNAME%: missing options' >&2
400 return 1
401 end
402
403 if test (count $values) -eq 0
404 echo '%FUNCNAME%: missing values' >&2
405 return 1
406 end
407
408 #endif
409 set -l i (count $__QUERY_CACHE_HAVING_OPTIONS)
410 while test $i -ge 1
411 if contains -- $__QUERY_CACHE_HAVING_OPTIONS[$i] $options
412 #ifdef nocase
413 contains -- $option_values[$i] $values && return 0
414 #else
415 contains -- $__QUERY_CACHE_OPTION_VALUES[$i] $values && return 0
416 #endif
417 #ifdef any
418 $any || return 1
419 #else
420 return 1
421 #endif
422 end
423
424 set i (math $i - 1)
425 end
426
427 return 1
428 ''', ['query_init'])
429
430 _OPTION_MATCH = FishFunction('option_match', r'''
431 # option_match [-a] [-i] -- <OPTIONS...> -- <REGEX>
432 #
433 # Checks if any option in OPTIONS has a value that matches REGEX.
434 #
435 #ifdef any
436 set -l any false
437 #endif
438 #ifdef nocase
439 set -l nocase ''
440 #endif
441
442 while set -q argv[1]
443 #ifdef any
444 if test "$argv[1]" = -a; set any true; set -e argv[1]; continue; end
445 #endif
446 #ifdef nocase
447 if test "$argv[1]" = -i; set nocase i; set -e argv[1]; continue; end
448 #endif
449 if test "$argv[1]" = --; set -e argv[1]; break; end
450 end
451
452 set -l eof_string (contains -i -- -- $argv || math (count $argv) + 1)
453 set -l options $argv[1..$(math $eof_string - 1)]
454 set -l regex $argv[(math $eof_string + 1)]
455
456 #ifdef DEBUG
457 if test (count $options) -eq 0
458 echo '%FUNCNAME%: missing options' >&2
459 return 1
460 end
461
462 if test (count $regex) -eq 0
463 echo '%FUNCNAME%: missing regex' >&2
464 return 1
465 end
466
467 #endif
468 set -l i (count $__QUERY_CACHE_HAVING_OPTIONS)
469 while test $i -ge 1
470 if contains -- $__QUERY_CACHE_HAVING_OPTIONS[$i] $options
471 #ifdef nocase
472 string match -rq$nocase -- $regex $__QUERY_CACHE_OPTION_VALUES[$i] && return 0
473 #else
474 string match -rq -- $regex $__QUERY_CACHE_OPTION_VALUES[$i] && return 0
475 #endif
476 #ifdef any
477 $any || return 1
478 #else
479 return 1
480 #endif
481 end
482
483 set i (math $i - 1)
484 end
485
486 return 1
487 ''', ['query_init'])
488
489 _NUM_OF_POSITIONALS = FishFunction('num_of_positionals', r'''
490 # num_of_positionals [<OPERATOR> <NUMBER>]
491 #
492 # If no arguments are provided, print the count of positional arguments.
493 #
494 # If two arguments are provided, the first argument should be one of
495 # the comparison operators: '-lt', '-le', '-eq', '-ne', '-gt', '-ge'.
496 # Returns 0 if the count of positional arguments matches the
497 # specified NUMBER according to the comparison operator, otherwise
498 # returns 1.
499 #
500 switch (count $argv)
501 case 0
502 count $__QUERY_CACHE_POSITIONALS
503 #ifdef DEBUG
504 case 1
505 echo '%FUNCNAME%: $argv[1]: missing operand' >&2
506 return 1
507 #endif
508 case 2
509 #ifdef DEBUG
510 if not contains -- $argv[1] -lt -le -eq -ne -gt -ge;
511 echo '%FUNCNAME%: $argv[1]: unknown operator' >&2
512 return 1
513 end
514 #endif
515 test (count $__QUERY_CACHE_POSITIONALS) $argv[1] $argv[2] && return 0 || return 1
516 #ifdef DEBUG
517 case '*'
518 echo '%FUNCNAME%: too many arguments' >&2
519 return 1
520 #endif
521 end
522 ''', ['query_init'])
523
524 _POSITIONAL_POSITION = FishFunction('positional_position', r'''
525 # positional_position <POSITIONAL_NUMBER>
526 #
527 # Prints the command-line index of the specified positional argument.
528 #
529 echo $__QUERY_CACHE_POSITIONALS_POSITIONS[$argv[1]]
530 ''', ['query_init'])
531
532 _CAPTURE_OPTION = FishFunction('capture_option', r'''
533 # capture_option <VARIABLE> <OPTIONS>
534 #
535 # Stores all values supplied to OPTIONS in VARIABLE.
536 # VARIABLE is global.
537 #
538 set -l variable $argv[1]
539 set -l options $argv[2..]
540 set -l values
541 set -l i 1
542
543 while test $i -le (count $__QUERY_CACHE_HAVING_OPTIONS)
544 if contains -- $__QUERY_CACHE_HAVING_OPTIONS[$i] $options
545 set -a values $__QUERY_CACHE_OPTION_VALUES[$i]
546 end
547
548 set i (math $i + 1)
549 end
550
551 set -g $variable $values
552 ''', ['query_init'])
553
554 _GET_COMPLETING_ARG = FishFunction('get_completing_arg', r'''
555 if test -n "$__fish_stripprefix"
556 string replace -r -- $__fish_stripprefix '' "$__QUERY_CACHE_CURRENT_ARG"
557 else
558 printf '%s\n' "$__QUERY_CACHE_CURRENT_ARG"
559 end
560 ''')
561
562 _FILEDIR = FishFunction('filedir', r'''
563 # Function for completing files or directories
564 #
565 # Options:
566 # -d|--description=DESC The description for completed entries
567 # -D|--directories Only complete directories
568 # -C|--cd=DIR List contents in DIR
569 #ifdef regex
570 # -r|--regex=PATTERN Only list files matching pattern
571 #endif
572 #ifdef regex_ignore
573 # -i|--ignore=PATTERN Ignore files matching pattern
574 #endif
575
576 argparse --max-args 0 'd/description=' 'D/directories' 'C/cd=' \
577 #ifdef regex
578 'r/regex=' \
579 #endif
580 #ifdef regex_ignore
581 'i/ignore=' \
582 #endif
583 -- $argv || return 1
584
585 set -l comp (get_completing_arg)
586 set -l desc
587 set -l files
588
589 if set -q _flag_cd[1]
590 pushd $_flag_cd 2>/dev/null || return 1
591 set files (complete -C"'' $comp")
592 popd
593 else
594 set files (complete -C"'' $comp")
595 end
596
597 if set -q _flag_description[1]
598 set desc $_flag_description
599 else if set -g _flag_directories
600 set desc 'Directory'
601 end
602
603 if set -q files[1]
604 if set -q _flag_directories[1]
605 set files (printf '%s\n' $files | string match -r '.*/$')
606 end
607 #ifdef regex
608
609 if set -q _flag_regex[1]
610 set files (printf '%s\n' $files | string match -rg "(.*/\$)|($_flag_regex[1])\$")
611 end
612 #endif
613 #ifdef regex_ignore
614
615 if set -q _flag_ignore[1]
616 set files (printf '%s\n' $files | string match -rv "(^|.*/)($_flag_ignore[1])\$")
617 end
618 #endif
619
620 printf '%s\n' $files\t"$desc"
621 end
622 ''', ['get_completing_arg'])
623
624 _LIST = FishFunction('list', r'''
625 set -l duplicates false
626
627 if test $argv[1] = '-d'
628 set duplicates true
629 set -e argv[1]
630 end
631
632 set -l separator $argv[1]
633 set -l func $argv[2]
634 set -l comp (get_completing_arg)
635
636 if test -z "$comp"
637 eval $func
638 return
639 end
640
641 set -l i
642 set -l value
643 set -l values
644 set -l descriptions
645
646 set -g __fish_stripprefix "^.*"(string escape --style=regex -- $separator)
647
648 for value in (eval $func)
649 set -l split (string split -- \t $value)
650 set -a values $split[1]
651 set -a descriptions "$split[2]"
652 end
653
654 set -e __fish_stripprefix
655
656 set -l having_values (string split -- $separator $comp)
657 set -l remaining_values_idxs
658
659 if $duplicates
660 set remaining_values_idxs (seq 1 (count $values))
661 else
662 set i 1
663 for value in $values
664 if not contains -- $value $having_values
665 set -a remaining_values_idxs $i
666 end
667
668 set i (math $i + 1)
669 end
670 end
671
672 switch $comp
673 case "*$separator"
674 for i in $remaining_values_idxs
675 printf '%s%s\t%s\n' $comp $values[$i] "$descriptions[$i]"
676 end
677 case "*$separator*"
678 set comp (string split -r -m 1 -- $separator $comp)[1]
679
680 for i in $remaining_values_idxs
681 printf '%s%s%s\t%s\n' "$comp" $separator $values[$i] "$descriptions[$i]"
682 end
683 case '*'
684 for i in $remaining_values_idxs
685 printf '%s\t%s\n' $values[$i] "$descriptions[$i]"
686 end
687 end
688 ''', ['get_completing_arg'])
689
690 _PREFIX = FishFunction('prefix', r'''
691 set -l comp (get_completing_arg)
692 set -l prefix_escaped (string escape --style=regex -- $argv[1])
693 if string match -qr -- $prefix_escaped $comp
694 set -g __fish_stripprefix "^"$prefix_escaped
695 printf "%s\n" $argv[1](eval $argv[2])
696 set -e __fish_stripprefix
697 else
698 printf "%s\n" $argv[1]
699 end
700 ''', ['get_completing_arg'])
701
702 _KEY_VALUE_LIST = FishFunction('key_value_list', r'''
703 set -l sep1 $argv[1]
704 set -l sep2 $argv[2]
705 set -l value
706 set -l keys
707 set -l descriptions
708 set -l functions
709 set -l excludes
710 set -l i
711
712 for i in (seq 3 4 (count $argv))
713 set -a keys $argv[$i]
714 set -a descriptions $argv[(math $i + 1)]
715 set -a functions $argv[(math $i + 2)]
716 set -a excludes $argv[(math $i + 3)]
717 end
718
719 set -l comp (get_completing_arg)
720 set -l remaining (seq 1 (count $keys))
721 set -l pairs (string split -- $sep1 $comp)
722 set -l pair
723
724 for pair in $pairs
725 set -l split (string split -m1 -- $sep2 $pair)
726 set i (contains -i -- $split[1] $keys)
727
728 if test $status -eq 0
729 set -l exclude
730 for exclude in (string split -- ' ' $excludes[$i])
731 set i (contains -i -- $exclude $keys)
732 if test $status -eq 0
733 set remaining (string match -v $i -- $remaining)
734 end
735 end
736 end
737 end
738
739 if test -z "$comp" || test (string sub -s -1 -l 1 -- $comp) = $sep1
740 for i in $remaining
741 if test "$functions[$i]" = false
742 printf '%s%s\t%s\n' "$comp" $keys[$i] "$descriptions[$i]"
743 else
744 printf '%s%s%s\t%s\n' "$comp" $keys[$i] $sep2 "$descriptions[$i]"
745 end
746 end
747 return
748 end
749
750 function %PREFIX%__call_func_for_key -S
751 set -l i
752 for i in (seq 1 (count $keys))
753 if test $keys[$i] = $argv[1]
754 set -g __fish_stripprefix "^.*"(string escape --style=regex -- $sep2)
755 $functions[$i]
756 set -e __fish_stripprefix
757 return
758 end
759 end
760 end
761
762 set -l pair $pairs[-1]
763 set -l split (string split -m1 -- $sep2 $pair)
764
765 switch $pair
766 case "*$sep2*"
767 set -l value_len (string length -- $split[2])
768
769 if test $value_len -gt 0
770 set comp (string sub -e -$value_len -- $comp)
771 end
772
773 for value in (%PREFIX%__call_func_for_key $split[1])
774 printf '%s%s\n' $comp $value
775 end
776 case '*'
777 set -l key_len (string length -- $split[1])
778 set comp (string sub -e -$key_len -- $comp)
779
780 for i in $remaining
781 if test "$functions[$i]" = false
782 printf '%s%s\t%s\n' "$comp" $keys[$i] "$descriptions[$i]"
783 else
784 printf '%s%s%s\t%s\n' "$comp" $keys[$i] $sep2 "$descriptions[$i]"
785 end
786 end
787 end
788 ''', ['get_completing_arg'])
789
790 _KEY_VALUE_PAIR = FishFunction('key_value_pair', r'''
791 set -l sep $argv[1]
792 set -l keys
793 set -l descriptions
794 set -l functions
795 set -l i
796
797 for i in (seq 2 3 (count $argv))
798 set -a keys $argv[$i]
799 set -a descriptions $argv[(math $i + 1)]
800 set -a functions $argv[(math $i + 2)]
801 end
802
803 function %PREFIX%__call_func_for_key -S
804 set -l i
805 for i in (seq 1 (count $keys))
806 if test $keys[$i] = $argv[1]
807 set -g __fish_stripprefix "^.*"(string escape --style=regex -- $sep)
808 $functions[$i]
809 set -e __fish_stripprefix
810 return
811 end
812 end
813 end
814
815 set -l comp (get_completing_arg)
816 set -l split (string split -m1 -- $sep $comp)
817
818 if test (count $split) -eq 2
819 set -l value
820 for value in (%PREFIX%__call_func_for_key $split[1])
821 printf '%s%s%s\n' $split[1] $sep $value
822 end
823 else
824 for i in (seq 1 (count $keys))
825 if test "$functions[$i]" = false
826 printf '%s\t%s\n' $keys[$i] "$descriptions[$i]"
827 else
828 printf '%s%s\t%s\n' $keys[$i] $sep "$descriptions[$i]"
829 end
830 end
831 end
832 ''', ['get_completing_arg'])
833
834 _KEY_VALUE_PAIR_EXEC = FishFunction('key_value_pair_exec', r'''
835 set -l sep $argv[1]
836 set -l func $argv[2]
837 set -l comp (get_completing_arg)
838 set -l split (string split -m1 -- $sep $comp)
839
840 if test (count $split) -eq 2
841 set -l value
842 set -g __fish_stripprefix "^.*"(string escape --style=regex -- $sep)
843 for value in ($func $split[1])
844 printf '%s%s%s\n' $split[1] $sep $value
845 end
846 set -e __fish_stripprefix
847 else
848 $func
849 end
850 ''', ['get_completing_arg'])
851
852 _KEY_VALUE_LIST_EXEC = FishFunction('key_value_list_exec', r'''
853 set -l sep1 $argv[1]
854 set -l sep2 $argv[2]
855 set -l func $argv[3]
856 set -l comp (get_completing_arg)
857 set -l pairs (string split -- $sep1 $comp)
858
859 set -l keys
860 set -l descriptions
861 set -l takes_arg
862 set -l excludes
863
864 set -l line
865 set -l i
866
867 for line in ($func)
868 set -l split (string split -- \t $line)
869 set -l key $split[1]
870 set -l non_repeatable_exclude ''
871
872 if string match -qr -- '=$' $key
873 set key (string sub -e -1 -- $key)
874 set -a takes_arg true
875 else if string match -qr -- '=\?$' $key
876 set key (string sub -e -2 -- $key)
877 set -a takes_arg false
878 else
879 set -a takes_arg false
880 end
881
882 if string match -q -- '\**' $key
883 set key (string sub -s 2 -- $key)
884 else
885 set non_repeatable_exclude " $key"
886 end
887
888 set -a keys $key
889 set -a descriptions $split[2]
890 set -a excludes "$split[3]$non_repeatable_exclude"
891 end
892
893 set -l remaining (seq 1 (count $keys))
894
895 set -l pair
896 for pair in $pairs
897 set -l split (string split -m1 -- $sep2 $pair)
898 set i (contains -i -- $split[1] $keys)
899
900 if test $status -eq 0
901 set -l exclude
902 for exclude in (string split -n -- ' ' $excludes[$i])
903 set i (contains -i -- $exclude $keys)
904 if test $status -eq 0
905 set remaining (string match -v $i -- $remaining)
906 end
907 end
908 end
909 end
910
911 if test -z "$comp" || test (string sub -s -1 -l 1 -- $comp) = $sep1
912 for i in $remaining
913 if test "$takes_arg[$i]" = false
914 printf '%s%s\t%s\n' "$comp" $keys[$i] "$descriptions[$i]"
915 else
916 printf '%s%s%s\t%s\n' "$comp" $keys[$i] $sep2 "$descriptions[$i]"
917 end
918 end
919 return
920 end
921
922 set -l pair $pairs[-1]
923 set -l split (string split -m1 -- $sep2 $pair)
924
925 switch $pair
926 case "*$sep2*"
927 set -l value_len (string length -- $split[2])
928
929 if test $value_len -gt 0
930 set comp (string sub -e -$value_len -- $comp)
931 end
932
933 set -g __fish_stripprefix "^.*"(string escape --style=regex -- $sep)
934 for value in ($func $split[1])
935 printf '%s%s\n' $comp $value
936 end
937 set -e __fish_stripprefix
938 case '*'
939 set -l key_len (string length -- $split[1])
940 set comp (string sub -e -$key_len -- $comp)
941
942 for i in $remaining
943 if test "$takes_arg[$i]" = false
944 printf '%s%s\t%s\n' "$comp" $keys[$i] "$descriptions[$i]"
945 else
946 printf '%s%s%s\t%s\n' "$comp" $keys[$i] $sep2 "$descriptions[$i]"
947 end
948 end
949 end
950 ''', ['get_completing_arg'])
951
952 _HISTORY = FishFunction('history', r'''
953 builtin history | command grep -E -o -- $argv[1]
954 ''')
955
956 _MIME_FILE = FishFunction('mime_file', r'''
957 set -l i_opt
958
959 set -l comp (get_completing_arg)
960
961 if command file -i /dev/null &>/dev/null
962 set i_opt '-i'
963 else if command file -I /dev/null &>/dev/null
964 set i_opt '-I'
965 else
966 complete -C"'' $comp"
967 return
968 end
969
970 set -l line
971 command file -L $i_opt -- "$comp"* 2>/dev/null | while read line
972 set -l split (string split -m 1 -r ':' "$line")
973
974 if string match -q -- '*inode/directory*' $split[2]
975 printf '%s/\n' "$split[1]"
976 else if begin; echo "$split[2]" | command grep -q -E -- $argv[1]; end
977 printf '%s\n' "$split[1]"
978 end
979 end
980 ''', ['get_completing_arg'])
981
982 _SUBSTRACT_PREFIX_SUFFIX = FishFunction('subtract_prefix_suffix', r'''
983 set -l s1 $argv[1]
984 set -l s2 $argv[2]
985
986 set -l len1 (string length -- $s1)
987 set -l len2 (string length -- $s2)
988
989 set -l maxk $len1
990 if test $len2 -lt $maxk
991 set maxk $len2
992 end
993
994 set -l k $maxk
995 while test $k -gt 0
996 set -l start (math $len1 - $k + 1)
997 set -l suf (string sub -s $start -l $k -- $s1)
998 set -l pre (string sub -s 1 -l $k -- $s2)
999
1000 if test "$suf" = "$pre"
1001 if test $len1 -eq $k
1002 echo ''
1003 else
1004 string sub -l (math $len1 - $k) -- $s1
1005 end
1006 return
1007 end
1008
1009 set k (math $k - 1)
1010 end
1011
1012 echo $s1
1013 ''')
1014
1015 _COMMANDLINE_STRING = FishFunction('commandline_string', r'''
1016 set -l line
1017 set -l comp (get_completing_arg)
1018
1019 complete -C "$comp" | while read line
1020 set -l split (string split -m 1 -- \t $line)
1021 set -l comp2 (subtract_prefix_suffix "$comp" "$split[1]")
1022 printf '%s\t%s\n' "$comp2$split[1]" "$split[2]"
1023 end''', ['subtract_prefix_suffix', 'get_completing_arg'])
1024
1025 _DATE_FORMAT = FishFunction('date_format', r'''
1026 set -l comp (get_completing_arg)
1027
1028 if test "$(string sub -s -1 -l 1 -- $comp)" = '%'
1029 printf '%s%s\t%s\n' \
1030 "$comp" 'a' 'abbreviated day name' \
1031 "$comp" 'A' 'full day name' \
1032 "$comp" 'b' 'abbreviated month name' \
1033 "$comp" 'h' 'abbreviated month name' \
1034 "$comp" 'B' 'full month name' \
1035 "$comp" 'c' 'preferred locale date and time' \
1036 "$comp" 'C' '2-digit century' \
1037 "$comp" 'd' 'day of month (01-31)' \
1038 "$comp" 'D' 'American format month/day/year (%m/%d/%y)' \
1039 "$comp" 'e' 'day of month ( 1-31)' \
1040 "$comp" 'F' 'ISO 8601 year-month-date (%Y-%m-%d)' \
1041 "$comp" 'G' '4-digit ISO 8601 week-based year' \
1042 "$comp" 'g' '2-digit ISO 8601 week-based year' \
1043 "$comp" 'H' 'hour (00-23)' \
1044 "$comp" 'I' 'hour (01-12)' \
1045 "$comp" 'j' 'day of year (001-366)' \
1046 "$comp" 'k' 'hour ( 0-23)' \
1047 "$comp" 'l' 'hour ( 1-12)' \
1048 "$comp" 'm' 'month (01-12)' \
1049 "$comp" 'M' 'minute (00-59)' \
1050 "$comp" 'n' 'newline' \
1051 "$comp" 'p' 'locale dependent AM/PM' \
1052 "$comp" 'r' 'locale dependent a.m. or p.m. time (%I:%M:%S %p)' \
1053 "$comp" 'R' '24-hour notation time (%H:%M)' \
1054 "$comp" 's' 'seconds since the epoch' \
1055 "$comp" 'S' 'seconds (00-60)' \
1056 "$comp" 't' 'tab' \
1057 "$comp" 'T' '24-hour notation with seconds (%H:%M:%S)' \
1058 "$comp" 'u' 'day of week (1-7, 1=Monday)' \
1059 "$comp" 'U' 'week number of current year, Sunday based (00-53)' \
1060 "$comp" 'V' 'ISO 8601 week number of current year, week 1 has 4 days in current year (01-53)' \
1061 "$comp" 'w' 'day of week (0-6, 0=Sunday)' \
1062 "$comp" 'W' 'week number of current year, Monday based (00-53)' \
1063 "$comp" 'x' 'locale dependent date representation without time' \
1064 "$comp" 'X' 'locale dependent time representation without date' \
1065 "$comp" 'y' '2-digit year (00-99)' \
1066 "$comp" 'Y' 'full year' \
1067 "$comp" 'z' 'UTC offset' \
1068 "$comp" 'Z' 'timezone name' \
1069 "$comp" '%' 'literal %'
1070 end
1071 ''', ['get_completing_arg'])
1072
1073 _NUMBER = FishFunction('number', r'''
1074 set -l comp (get_completing_arg)
1075 set -l number (string match -r -- '^[0-9,.]*' $comp)
1076
1077 if test -z "$number"
1078 return
1079 end
1080
1081 set -l suffix_and_desc
1082 for suffix_and_desc in $argv
1083 set suffix_and_desc (string split -m 1 -- ':' $suffix_and_desc)
1084 printf '%s%s\t%s\n' $number $suffix_and_desc[1] $suffix_and_desc[2]
1085 end
1086 ''', ['get_completing_arg'])
1087
1088 # =============================================================================
1089 # Bonus
1090 # =============================================================================
1091
1092 _TIMEZONE_LIST = FishFunction('timezone_list', r'''
1093 if ! command timedatectl list-timezones 2>/dev/null
1094 command find /usr/share/zoneinfo -type f |\
1095 command sed 's|/usr/share/zoneinfo/||g' |\
1096 command grep -E -v '^(posix|right)'
1097 end
1098 ''')
1099
1100 _ALSA_LIST_CARDS = FishFunction('alsa_list_cards', r'''
1101 set -l card
1102
1103 for card in (command aplay -l | string match -r '^card [0-9]+: [^,]+')
1104 set card (string replace 'card ' '' $card)
1105 set -l split (string split ': ' $card)
1106
1107 if set -q split[2]
1108 printf "%s\t%s\n" $split[1] $split[2]
1109 end
1110 end
1111 ''')
1112
1113 _ALSA_LIST_DEVICES = FishFunction('alsa_list_devices', r'''
1114 set -l card
1115
1116 for card in (command aplay -l | string match -r '^card [0-9]+: [^,]+')
1117 set card (string replace 'card ' '' $card)
1118 set -l split (string split ': ' $card)
1119
1120 if set -q split[2]
1121 printf "hw:%s\t%s\n" $split[1] $split[2]
1122 end
1123 end
1124 ''')
1125
1126
1127 class FishHelpers(GeneralHelpers):
1128 '''Class holding helper functions for Fish.'''
1129
1130 def __init__(self, config, function_prefix):
1131 super().__init__(config, function_prefix, FishFunction)
1132 self.add_function(_QUERY_INIT)
1133 self.add_function(_POSITIONAL_CONTAINS)
1134 self.add_function(_POSITIONAL_MATCH)
1135 self.add_function(_HAS_OPTION)
1136 self.add_function(_OPTION_IS)
1137 self.add_function(_OPTION_MATCH)
1138 self.add_function(_NUM_OF_POSITIONALS)
1139 self.add_function(_POSITIONAL_POSITION)
1140 self.add_function(_CAPTURE_OPTION)
1141
1142 self.add_function(_GET_COMPLETING_ARG)
1143 self.add_function(_FILEDIR)
1144 self.add_function(_LIST)
1145 self.add_function(_PREFIX)
1146 self.add_function(_KEY_VALUE_LIST)
1147 self.add_function(_KEY_VALUE_PAIR)
1148 self.add_function(_KEY_VALUE_LIST_EXEC)
1149 self.add_function(_KEY_VALUE_PAIR_EXEC)
1150 self.add_function(_HISTORY)
1151 self.add_function(_DATE_FORMAT)
1152 self.add_function(_NUMBER)
1153 self.add_function(_MIME_FILE)
1154 self.add_function(_SUBSTRACT_PREFIX_SUFFIX)
1155 self.add_function(_COMMANDLINE_STRING)
1156 self.add_function(_TIMEZONE_LIST)
1157 self.add_function(_ALSA_LIST_CARDS)
1158 self.add_function(_ALSA_LIST_DEVICES)