fork/fish-shell

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

Johannes Altmanningercompletions/wpctl: fix when "settings" subcommand is not available6fec5ab32

master
4.0 KiB79 linesraw
1set -l commands status get-volume inspect set-default set-volume set-mute set-profile clear-default
2
3if wpctl settings &>/dev/null
4    set --append commands settings
5end
6
7function __wpctl_get_nodes -a section -a type
8    set -l havesection
9    set -l havetype
10    wpctl status | while read -l line
11        if set -q havesection[1]
12            test -z "$line"; and break
13            if set -q havetype[1]
14                string match -rq '^\s*\│\s*$' -- $line; and break
15                printf '%s\t%s\n' (string match -r '\d+' $line) (string match -rg '\d+\. ([^\[]*)' $line)
16            else
17                string match -q "*$type*" -- $line
18                and set havetype 1
19            end
20        else
21            string match -q "$section*" -- $line
22            and set havesection 1
23        end
24    end
25end
26
27function __wpctl_command_shape
28    set -l shape $argv
29    set -l command (commandline -pxc)
30    set -e command[1] # Remove command name
31
32    # Remove flags as we won't count them with the shape
33    set -l command (string match -v -- '-*' $command)
34
35    if test (count $command) != (count $shape)
36        return 1
37    end
38
39    while set -q command[1]
40        string match -q -- $shape[1] $command[1]; or return 1
41        set -e shape[1] command[1]
42    end
43end
44
45function __wpctl_get_settings
46    set -l wpctl_settings (wpctl settings 2>/dev/null| string collect)
47    or return
48
49    string match --regex --all --quiet '\- Name: (?<wpctl_settings_name>.*)\n  Desc: (?<wpctl_settings_desc>.*)' $wpctl_settings
50
51    for i in (seq (count $wpctl_settings_name))
52        set -l name $wpctl_settings_name[$i]
53        set -l desc (string shorten --max 60 $wpctl_settings_desc[$i])
54        echo $name\t$desc
55    end
56end
57
58complete -c wpctl -f
59
60complete -c wpctl -s h -l help -d "Show help options"
61complete -c wpctl -n "__fish_seen_subcommand_from status" -s k -l nick -d "Display device and node nicknames instead of descriptions"
62complete -c wpctl -n "__fish_seen_subcommand_from status" -s n -l name -d "Display device and node names instead of descriptions"
63complete -c wpctl -n "__fish_seen_subcommand_from inspect" -s r -l referenced -d "Show objects that are referenced in properties"
64complete -c wpctl -n "__fish_seen_subcommand_from inspect" -s a -l associated -d "Show associated objects"
65complete -c wpctl -n "__fish_seen_subcommand_from set-volume" -s p -l pid -d "Selects all nodes associated to the given PID"
66complete -c wpctl -n "__fish_seen_subcommand_from set-volume" -s l -l limit -d "Limit volume to below this value"
67complete -c wpctl -n "__fish_seen_subcommand_from set-mute" -s p -l pid -d "Selects all nodes associated to the given PID"
68complete -c wpctl -n "__fish_seen_subcommand_from settings" -s d -l delete -d "Deletes the saved setting value"
69complete -c wpctl -n "__fish_seen_subcommand_from settings" -s s -l save -d "Saves the setting value"
70complete -c wpctl -n "__fish_seen_subcommand_from settings" -s r -l reset -d "Resets the saved setting to its default value"
71
72complete -c wpctl -n __wpctl_command_shape -a "$commands"
73complete -c wpctl -n '__wpctl_command_shape "*"' -n "__fish_seen_subcommand_from get-volume inspect set-volume set-mute set-profile" -a "@DEFAULT_AUDIO_SOURCE@" -d "Default Microphone"
74complete -c wpctl -n '__wpctl_command_shape "*"' -n "__fish_seen_subcommand_from get-volume inspect set-volume set-mute set-profile" -a "@DEFAULT_AUDIO_SINK@" -d "Default Speakers"
75complete -c wpctl -n '__wpctl_command_shape "*"' -n "__fish_seen_subcommand_from inspect set-profile" -a "@DEFAULT_VIDEO_SOURCE@" -d "Default Camera"
76complete -c wpctl -n '__wpctl_command_shape "*"' -n "__fish_seen_subcommand_from get-volume inspect set-volume set-mute set-profile" -a "(__wpctl_get_nodes Audio Sources) (__wpctl_get_nodes Audio Sinks)"
77complete -c wpctl -n '__wpctl_command_shape "*"' -n "__fish_seen_subcommand_from inspect set-profile" -a "(__wpctl_get_nodes Audio Sources) (__wpctl_get_nodes Audio Sinks) (__wpctl_get_nodes Video Source)"
78complete -c wpctl -n '__wpctl_command_shape set-mute "*"' -a "0 1 toggle"
79complete -c wpctl -n '__wpctl_command_shape settings' -a "(__wpctl_get_settings)"