fork/fish-shell

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

ccoVeilleFix typos in completions scripts0df6fa791

master
6.4 KiB149 linesraw
1# Fish completions for systemd's "busctl" dbus tool
2# TODO:
3# One issue is that sometimes these will come to a dead-end e.g. when a particular interface has no properties
4# Another is that some busnames aren't accessible by the current user
5# but this can't be predicted via the user that owns that name, e.g. `org.freedesktop.login1`
6# is usually owned by a root-owned process, yet accessible (at least in part) by normal users
7
8# A simple wrapper to call busctl with the correct mode and output
9function __fish_busctl
10    # TODO: If there's a "--address" argument we need to pass that
11    # We also need to pass the _last_ of these (`busctl --user --system` operates on the system bus)
12    set -l mode
13    if __fish_contains_opt user
14        set mode --user
15    else
16        set mode --system
17    end
18    command busctl $mode $argv --no-legend --no-pager 2>/dev/null
19end
20
21function _fish_busctl
22    set -l args a-address= s-show-machine u-unique A-acquired ä-activatable \
23        m-match= S-size= l-list q-quiet v-verbose e-expect-reply= Ä-auto-start= \
24        1-allow-interactive-authorization= t-timeout= 2-augment-creds= U-user 3-system \
25        H/host= M/machine= n-no-pager N-no-legend h/help V-version
26    set -l cmdline (commandline -xpc) (commandline -ct)
27    set -e cmdline[1]
28    argparse $args -- $cmdline 2>/dev/null
29    or return
30    set -l cmd $argv[1]
31    set -e argv[1]
32    switch "$cmd"
33        case list help
34            # Accepts nothing
35            return
36        case status
37            # A service
38            if not set -q argv[2]
39                __fish_busctl_busnames
40            end
41        case monitor capture tree
42            # Services
43            __fish_busctl_busnames
44        case introspect
45            # Service Object Interface
46            if not set -q argv[2]
47                __fish_busctl_busnames
48            else if not set -q argv[3]
49                __fish_busctl_objects $argv[1]
50            else if not set -q argv[4]
51                __fish_busctl_interfaces $argv[1..2]
52            end
53        case call
54            # SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]
55            if not set -q argv[2]
56                __fish_busctl_busnames
57            else if not set -q argv[3]
58                __fish_busctl_objects $argv[1]
59            else if not set -q argv[4]
60                __fish_busctl_interfaces $argv[1..2]
61            else if not set -q argv[5]
62                __fish_busctl_members method $argv[1..3]
63            else if not set -q argv[6]
64                __fish_busctl_signature $argv[1..4]
65            end
66        case get-property
67            # SERVICE OBJECT INTERFACE PROPERTY...
68            if not set -q argv[2]
69                __fish_busctl_busnames
70            else if not set -q argv[3]
71                __fish_busctl_objects $argv[1]
72            else if not set -q argv[4]
73                __fish_busctl_interfaces $argv[1..2]
74            else
75                __fish_busctl_members property $argv[1..3]
76            end
77        case set-property
78            # SERVICE OBJECT INTERFACE PROPERTY SIGNATURE ARGUMENT...
79            if not set -q argv[2]
80                __fish_busctl_busnames
81            else if not set -q argv[3]
82                __fish_busctl_objects $argv[1]
83            else if not set -q argv[4]
84                __fish_busctl_interfaces $argv[1..2]
85            else if not set -q argv[5]
86                __fish_busctl_members property $argv[1..3]
87            else if not set -q argv[6]
88                __fish_busctl_members signature $argv[1..4]
89            end
90    end
91end
92
93function __fish_busctl_busnames
94    __fish_busctl list --acquired | string replace -r '\s+.*$' ''
95    # Describe unique names (":1.32") with their process (e.g. `:1.32\tsteam`)
96    __fish_busctl list --unique | string replace -r '\s+\S+\s+(\S+)\s+.*$' '\t$1'
97end
98
99function __fish_busctl_objects -a busname
100    __fish_busctl tree --list $busname | string replace -r '\s+.*$' ''
101end
102
103function __fish_busctl_interfaces -a busname -a object
104    __fish_busctl introspect --list $busname $object | string replace -r '\s+.*$' ''
105end
106
107function __fish_busctl_members -a type -a busname -a object -a interface
108    __fish_busctl introspect --list $busname $object $interface \
109        | string match -- "* $type *" | string replace -r '.(\S+) .*' '$1'
110end
111
112function __fish_busctl_signature -a busname -a object -a interface -a member
113    __fish_busctl introspect --list $busname $object $interface \
114        | string match ".$member *" | while read -l a b c d
115        echo $c
116    end
117end
118
119### Commands
120set -l commands list status monitor capture tree introspect call get-property set-property help
121
122complete -f -c busctl
123complete -f -c busctl -n "not __fish_seen_subcommand_from $commands" -a "$commands"
124
125### Arguments to commands
126complete -f -c busctl -a '(_fish_busctl)'
127
128# Flags
129# These are incomplete as I have no idea how to complete --address= or --match=
130complete -f -c busctl -n "__fish_seen_subcommand_from call" -l quiet -d 'Suppress message payload display'
131complete -f -c busctl -n "__fish_seen_subcommand_from call get-property" -l verbose
132complete -f -c busctl -n "__fish_seen_subcommand_from tree" -l list -d 'Show a flat list instead of a tree'
133complete -x -c busctl -n "__fish_seen_subcommand_from call" -l expect-reply -a "yes no"
134complete -x -c busctl -n "__fish_seen_subcommand_from call" -l auto-start -a "yes no" -d 'Activate the peer if necessary'
135complete -x -c busctl -n "__fish_seen_subcommand_from call" -l allow-interactive-authorization -a "yes no"
136complete -x -c busctl -n "__fish_seen_subcommand_from call" -l timeout -a "(seq 0 100)"
137complete -f -c busctl -n "__fish_seen_subcommand_from list" -l unique -d 'Only show unique (:X.Y) names'
138complete -f -c busctl -n "__fish_seen_subcommand_from list" -l acquired -d 'Only show well-known names'
139complete -f -c busctl -n "__fish_seen_subcommand_from list" -l activatable -d 'Only show peers that have not been activated yet but can be'
140complete -f -c busctl -n "__fish_seen_subcommand_from list" -l show-machine -d 'Show the machine the peers belong to'
141complete -x -c busctl -n "__fish_seen_subcommand_from list status" -l augment-creds -a "yes no"
142complete -f -c busctl -l user
143complete -f -c busctl -l system
144complete -f -c busctl -s H -l host -a "(__fish_print_hostnames)"
145complete -f -c busctl -s M -l machine -a "(__fish_systemd_machines)"
146complete -f -c busctl -l no-pager
147complete -f -c busctl -l no-legend
148complete -f -c busctl -s h -l help
149complete -f -c busctl -l version