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 2/3] completion: add bluetoothctl zsh completions
Date: Fri, 16 Aug 2019 00:43:32 -0700
Message-ID: <20190816074333.24673-3-rpigott@berkeley.edu> (raw)
In-Reply-To: <20190816074333.24673-1-rpigott@berkeley.edu>

From: Ronan Pigott <rpigott@berkeley.edu>

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

diff --git a/completion/zsh/_bluetoothctl b/completion/zsh/_bluetoothctl
new file mode 100644
index 000000000..c9c177a83
--- /dev/null
+++ b/completion/zsh/_bluetoothctl
@@ -0,0 +1,131 @@
+#compdef bluetoothctl
+
+_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
+}
+
+_bluetoothctl_agent() {
+	local -a agent_options=(${(f)"$(bluetoothctl agent help)"})
+	agent_options+=help
+	compadd -a agent_options
+}
+
+_bluetoothctl_agent_cap() {
+	local -a agent_options=(${(f)"$(bluetoothctl agent help)"})
+	agent_options=( "${(@)agent_options:#(on|off)}" )
+	compadd -a agent_options
+}
+
+_bluetoothctl_advertise() {
+	local -a ad_options=(${(f)"$(bluetoothctl advertise help)"})
+	ad_options+=help
+	compadd -a ad_options
+}
+
+_bluetoothctl() {
+	local curcontext=$curcontext state line ret=1
+
+	local -a simple_commands=(
+		"help:Display help"
+		"version:Dispaly version"
+		"list:List available controllers"
+		"devices:List available devices"
+		"paired-devices:List paired devices"
+		"reset-alias:Reset controller alias"
+		"default-agent:Set agent as the default one"
+		"export:Print environment variables"
+		"system-alias:Set controller alias"
+		"set-alias:Set device alias"
+	)
+
+	local -a toggle_commands=(
+		"power:Set controller power"
+		"pairable:Set controller pairable mode"
+		"discoverable:Set controller discoverable mode"
+		"scan:Scan for devices"
+	)
+
+	local -a controller_commands=(
+		"show:Controller information"
+		"select:Select default controller"
+	)
+
+	local -a device_commands=(
+		"info:Device information"
+		"pair:Pair with device"
+		"trust:Trust device"
+		"untrust:Untrust device"
+		"block:Block device"
+		"unblock:Unblock device"
+		"remove:Remove device"
+		"connect:Connect device"
+		"disconnect:Disconnect device"
+	)
+
+	local -a other_commands=(
+		"agent:Enable/disable advertising with given type"
+		"advertise:Enable/disable advertising with the given type"
+	)
+
+	local -a all_commands=(
+		$simple_commands
+		$device_commands
+		$toggle_commands
+		$controller_commands
+		$other_commands
+	)
+
+	_arguments -C \
+		+ '(info)' \
+		'--help[Show help message and exit]' \
+		'--version[Show version info and exit]' \
+		+ 'mod' \
+		'(info)--timeout[Timeout in seconds for non-interactive mode]' \
+		'(info)--agent=[Register agent handler]:agent:_bluetoothctl_agent_cap' \
+		+ 'command' \
+		'(info):command:->command' \
+		'(info):: :->argument' \
+		': :_message "no more arguments"'
+
+	if [[ $state == "command" ]]; then
+		_describe -t commands 'command' all_commands
+	elif [[ $state == "argument" ]]; then
+		curcontext=${curcontext%:*:*}:bluetoothctl-$line[1]
+		case $line[1] in
+			(${(~j.|.)simple_commands%%:*})
+				_message "no more arguments"
+				;;
+			(${(~j.|.)toggle_commands%%:*})
+				compadd on off
+				;;
+			(${(~j.|.)device_commands%%:*})
+				_bluezcomp_device
+				;;
+			(${(~j.|.)controller_commands%%:*})
+				_bluezcomp_controller
+				;;
+			*)
+				if ! _call_function ret _bluetoothctl_$line[1]; then
+					_message "Unknown bluetoothctl command: $line[1]"
+				fi
+				return ret
+				;;
+		esac
+	fi
+}
+
+_bluetoothctl
-- 
2.22.1


  parent reply index

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-16  7:43 [PATCH BlueZ 0/3] Add zsh completions for bluetoothctl Ronan Pigott
2019-08-16  7:43 ` [PATCH BlueZ 1/3] client/main: add help option for available args Ronan Pigott
2019-08-16 11:29   ` Luiz Augusto von Dentz
2019-08-16  7:43 ` Ronan Pigott [this message]
2019-08-16 11:36   ` [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions Luiz Augusto von Dentz
2019-08-16  7:43 ` [PATCH BlueZ 3/3] build: install " Ronan Pigott

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=20190816074333.24673-3-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


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