fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMark private functions that don't need localizationa53db7256

master
2.0 KiB46 linesraw
1# localization: skip(private)
2#
3# This function is intended to be used to generate completions in `-a "(...)"` calls for arguments that take 1..n values
4# separated by commas and we dynamically or statically generated a list of n values that must be combined and permuted
5# in order to appease both the `complete` machinery and the target application.
6#
7# TODO: Make this work with completions that have their description appended after a \t
8
9function __fish_concat_completions -d "Generate completions that are specified as comma-separated values from stdin source"
10    type -q awk || return
11
12    set -l token (commandline -ct)
13    # We want to filter out suggestions the user has already entered.
14    set -l extant (string split -- ',' $token)
15    set -l filter (printf '^%s$\n' (printf '%s\n' $extant | string escape --style=regex) | string join '|')
16    # Work around the insanity of trying to read from stdin within a function. Note that we can't place the
17    # `read` call in between () to capture the output because that breaks its connection to stdin.
18    while read -l line
19        echo $line
20    end | string match -er '.' | string match -rv -- $filter | string replace -r '^' -- (string replace -rf -- '^(.+),.*$' '$1,' $token; or echo "")
21
22    return
23    # Verified compatible with bsd awk and gnu awk
24    awk '
25        {
26            lines[NR-1] = $0
27        }
28        END {
29            n = NR
30            for (k = 1; k <= n; k++) {
31                generate_permutations(lines, n, k)
32            }
33        }
34        function generate_permutations(arr, total_len, perm_len, prefix, _i, _swap) {
35            if (perm_len == 0) {
36                print prefix
37                return
38            }
39            for (_i = 0; _i < total_len; _i++) {
40                _swap = arr[total_len-1]; arr[total_len-1] = arr[_i]; arr[_i] = _swap
41                generate_permutations(arr, total_len-1, perm_len-1, prefix arr[total_len-1] (perm_len == 1 ? "" : ","))
42                arr[_i] = arr[total_len-1]; arr[total_len-1] = _swap
43            }
44        }
45    '
46end