fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
a53db7256
master
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" 10type -q awk|| return 11 12set -l token( commandline -ct) 13# We want to filter out suggestions the user has already entered. 14set -l extant( string split --',' $token ) 15set -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. 18while read -l line 19echo $line 20end | string match -er'.' | string match -rv --$filter | string replace -r'^' --( string replace -rf --'^(.+),.*$' '$1,' $token ;or echo "" ) 21 22return 23# Verified compatible with bsd awk and gnu awk 24awk ' 25{ 26lines[NR-1] = $0 27} 28END { 29n = NR 30for (k = 1; k <= n; k++) { 31generate_permutations(lines, n, k) 32} 33} 34function generate_permutations(arr, total_len, perm_len, prefix, _i, _swap) { 35if (perm_len == 0) { 36print prefix 37return 38} 39for (_i = 0; _i < total_len; _i++) { 40_swap = arr[total_len-1]; arr[total_len-1] = arr[_i]; arr[_i] = _swap 41generate_permutations(arr, total_len-1, perm_len-1, prefix arr[total_len-1] (perm_len == 1 ? "" : ",")) 42arr[_i] = arr[total_len-1]; arr[total_len-1] = _swap 43} 44} 45' 46end