Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
From: Ronan Pigott <rpigott314@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Ronan Pigott <rpigott@berkeley.edu>
Subject: [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions
Date: Fri, 16 Aug 2019 23:34:51 -0700
Message-ID: <20190817063452.23273-4-rpigott@berkeley.edu> (raw)
In-Reply-To: <20190817063452.23273-1-rpigott@berkeley.edu>

From: Ronan Pigott <rpigott@berkeley.edu>

This zsh compdef provides completions for the bluetoothctl command.

It has been modified from version 1 of this patch to get the command
names and descriptions from the newly introduced --zsh-complete.

It utilizes the output of `bluetoothctl devices` or
`bluetoothctl controllers` to provide a descriptive completion menu
in those contexts.

e.g.

$ bluetoothctl connect <TAB>
04:52:C7:0C:D4:A7  -- Bose QuietComfort 35
40:4E:36:D9:8F:28  -- Pixel 2
A4:38:CC:20:5D:E0  -- Pro Controller

---
 completion/zsh/_bluetoothctl | 98 ++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 completion/zsh/_bluetoothctl

diff --git a/completion/zsh/_bluetoothctl b/completion/zsh/_bluetoothctl
new file mode 100644
index 000000000..bf35e503e
--- /dev/null
+++ b/completion/zsh/_bluetoothctl
@@ -0,0 +1,98 @@
+#compdef bluetoothctl
+
+__bluetoothctl() {
+	bluetoothctl "$@" 2>/dev/null
+}
+
+_bluezcomp_controller() {
+	local -a controllers
+	bluetoothctl list |
+	while read _ MAC NAME; do
+		controllers+="${MAC//:/\\:}:${NAME//:/\\:}"
+	done
+	_describe -t controllers 'controller' controllers
+}
+
+_bluezcomp_device() {
+	local -a devices
+	bluetoothctl devices |
+	while read _ MAC NAME; do
+		devices+="${MAC//:/\\:}:${NAME//:/\\:}"
+	done
+	_describe -t devices 'device' devices
+}
+
+_bluezcomp_agentcap() {
+	local -a agent_options=(${(f)"$(__bluetoothctl agent help)"})
+	agent_options=( "${(@)agent_options:#(on|off)}" )
+	compadd -a agent_options
+}
+
+_bluetoothctl_agent() {
+	local -a agent_options=(${(f)"$(__bluetoothctl agent help)"})
+	agent_options+=help
+	compadd -a agent_options
+}
+
+_bluetoothctl_advertise() {
+	local -a ad_options=(${(f)"$(__bluetoothctl advertise help)"})
+	ad_options+=help
+	compadd -a ad_options
+}
+
+_bluetoothctl() {
+	local -a toggle_commands=(
+		"discoverable" "pairable" "power" "scan"
+	)
+
+	local -a controller_commands=(
+		"select" "show"
+	)
+
+	local -a device_commands=(
+		"block" "connect" "disconnect" "info"
+		"pair" "remove" "trust" "unblock" "untrust"
+	)
+
+	# Other commands may be handled by _bluetoothctl_$command
+	local -a all_commands=( "${(@f)$(__bluetoothctl --zsh-complete help)}" )
+
+	local curcontext=$curcontext state line ret=1
+	_arguments -C \
+		+ '(info)' \
+		{-h,--help}'[Show help message and exit]' \
+		{-v,--version}'--version[Show version info and exit]' \
+		+ 'mod' \
+		'(info)'{-a+,--agent=}'[Register agent handler]:agent:_bluezcomp_agentcap' \
+		'(info)'{-t,--timeout}'[Timeout in seconds for non-interactive mode]' \
+		'(info)'{-m,--monitor}'[Enable monitor output]' \
+		+ 'command' \
+		'(info):command:->command' \
+		'(info):: :->argument' \
+		': :_message "no more arguments"'
+
+	if [[ $state == "command" ]]; then
+		_describe -t commands 'command' all_commands
+	elif [[ $state == "argument" ]]; then
+		if (( ! ${"${(@)all_commands%%:*}"[(I)${line[1]}]} )); then
+			_message "Unknown bluetoothctl command: $line[1]"
+			return 1;
+		fi
+
+		curcontext="${curcontext%:*:*}:bluetoothctl-$line[1]:"
+		if ! _call_function ret _bluetoothctl_$line[1]; then
+			case $line[1] in
+				(${(~j.|.)toggle_commands})
+					compadd on off
+					;;
+				(${(~j.|.)device_commands})
+					_bluezcomp_device
+					;;
+				(${(~j.|.)controller_commands})
+					_bluezcomp_controller
+					;;
+			esac
+		fi
+		return ret
+	fi
+} && _bluetoothctl
-- 
2.22.1


  parent reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-17  6:34 [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Ronan Pigott
2019-08-17  6:34 ` [PATCH BlueZ v2 1/4] client/main: add help option for available args Ronan Pigott
2019-08-17  6:34 ` [PATCH BlueZ v2 2/4] shared/shell: add --zsh-complete option Ronan Pigott
2019-08-17  6:34 ` Ronan Pigott [this message]
2019-08-18  0:57   ` [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions Ronan Pigott
2019-08-17  6:34 ` [PATCH BlueZ v2 4/4] build: install " Ronan Pigott
2019-09-06  8:13 ` [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Luiz Augusto von Dentz

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190817063452.23273-4-rpigott@berkeley.edu \
    --to=rpigott314@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=rpigott@berkeley.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org linux-bluetooth@archiver.kernel.org
	public-inbox-index linux-bluetooth

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/ public-inbox