| 1 |
#compdef bluetoothctl |
| 2 |
|
| 3 |
__bluetoothctl() { |
| 4 |
bluetoothctl "$@" 2>/dev/null |
| 5 |
} |
| 6 |
|
| 7 |
_bluezcomp_controller() { |
| 8 |
local -a controllers |
| 9 |
bluetoothctl list | |
| 10 |
while read _ MAC NAME; do |
| 11 |
controllers+="${MAC//:/\\:}:${NAME//:/\\:}" |
| 12 |
done |
| 13 |
_describe -t controllers 'controller' controllers |
| 14 |
} |
| 15 |
|
| 16 |
_bluezcomp_device() { |
| 17 |
local -a devices |
| 18 |
bluetoothctl devices | |
| 19 |
while read _ MAC NAME; do |
| 20 |
devices+="${MAC//:/\\:}:${NAME//:/\\:}" |
| 21 |
done |
| 22 |
_describe -t devices 'device' devices |
| 23 |
} |
| 24 |
|
| 25 |
_bluezcomp_agentcap() { |
| 26 |
local -a agent_options=(${(f)"$(__bluetoothctl agent help)"}) |
| 27 |
agent_options=( "${(@)agent_options:#(on|off)}" ) |
| 28 |
compadd -a agent_options |
| 29 |
} |
| 30 |
|
| 31 |
_bluetoothctl_agent() { |
| 32 |
local -a agent_options=(${(f)"$(__bluetoothctl agent help)"}) |
| 33 |
agent_options+=help |
| 34 |
compadd -a agent_options |
| 35 |
} |
| 36 |
|
| 37 |
_bluetoothctl_advertise() { |
| 38 |
local -a ad_options=(${(f)"$(__bluetoothctl advertise help)"}) |
| 39 |
ad_options+=help |
| 40 |
compadd -a ad_options |
| 41 |
} |
| 42 |
|
| 43 |
_bluetoothctl() { |
| 44 |
local -a toggle_commands=( |
| 45 |
"discoverable" "pairable" "power" "scan" |
| 46 |
) |
| 47 |
|
| 48 |
local -a controller_commands=( |
| 49 |
"select" "show" |
| 50 |
) |
| 51 |
|
| 52 |
local -a device_commands=( |
| 53 |
"block" "connect" "disconnect" "info" |
| 54 |
"pair" "remove" "trust" "unblock" "untrust" |
| 55 |
) |
| 56 |
|
| 57 |
# Other commands may be handled by _bluetoothctl_$command |
| 58 |
local -a all_commands=( "${(@f)$(__bluetoothctl --zsh-complete help)}" ) |
| 59 |
|
| 60 |
local curcontext=$curcontext state line ret=1 |
| 61 |
_arguments -C \ |
| 62 |
+ '(info)' \ |
| 63 |
{-h,--help}'[Show help message and exit]' \ |
| 64 |
{-v,--version}'--version[Show version info and exit]' \ |
| 65 |
+ 'mod' \ |
| 66 |
'(info)'{-a+,--agent=}'[Register agent handler]:agent:_bluezcomp_agentcap' \ |
| 67 |
'(info)'{-t,--timeout}'[Timeout in seconds for non-interactive mode]' \ |
| 68 |
'(info)'{-m,--monitor}'[Enable monitor output]' \ |
| 69 |
+ 'command' \ |
| 70 |
'(info):command:->command' \ |
| 71 |
'(info):: :->argument' |
| 72 |
|
| 73 |
if [[ $state == "command" ]]; then |
| 74 |
_describe -t commands 'command' all_commands |
| 75 |
elif [[ $state == "argument" ]]; then |
| 76 |
if (( ! ${"${(@)all_commands%%:*}"[(I)${line[1]}]} )); then |
| 77 |
_message "Unknown bluetoothctl command: $line[1]" |
| 78 |
return 1; |
| 79 |
fi |
| 80 |
|
| 81 |
curcontext="${curcontext%:*:*}:bluetoothctl-$line[1]:" |
| 82 |
if ! _call_function ret _bluetoothctl_$line[1]; then |
| 83 |
case $line[1] in |
| 84 |
(${(~j.|.)toggle_commands}) |
| 85 |
compadd on off |
| 86 |
;; |
| 87 |
(${(~j.|.)device_commands}) |
| 88 |
_bluezcomp_device |
| 89 |
;; |
| 90 |
(${(~j.|.)controller_commands}) |
| 91 |
_bluezcomp_controller |
| 92 |
;; |
| 93 |
esac |
| 94 |
fi |
| 95 |
return ret |
| 96 |
fi |
| 97 |
} && _bluetoothctl |