Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl
@ 2019-08-17  6:34 Ronan Pigott
  2019-08-17  6:34 ` [PATCH BlueZ v2 1/4] client/main: add help option for available args Ronan Pigott
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Ronan Pigott @ 2019-08-17  6:34 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

From: Ronan Pigott <rpigott@berkeley.edu>

V2 of zsh completions for bluetoothctl

bluetoothctl has a nice interactive interface, but some of its functions
can be accessed quicker non-interactively, straight from the command
line. I made these zsh completions to facilitate that.

If you are a zsh user, try them out!

Ronan Pigott (4):
  client/main: add help option for available args
  shared/shell: add --zsh-complete option
  completion: add bluetoothctl zsh completions
  build: install zsh completions

 Makefile.tools               |  5 ++
 client/main.c                |  8 +++
 completion/zsh/_bluetoothctl | 98 ++++++++++++++++++++++++++++++++++++
 configure.ac                 | 12 +++++
 src/shared/shell.c           | 27 ++++++++++
 5 files changed, 150 insertions(+)
 create mode 100644 completion/zsh/_bluetoothctl

-- 
2.22.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH BlueZ v2 1/4] client/main: add help option for available args
  2019-08-17  6:34 [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Ronan Pigott
@ 2019-08-17  6:34 ` Ronan Pigott
  2019-08-17  6:34 ` [PATCH BlueZ v2 2/4] shared/shell: add --zsh-complete option Ronan Pigott
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ronan Pigott @ 2019-08-17  6:34 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

From: Ronan Pigott <rpigott@berkeley.edu>

This option exists to facilitate external completion of commands that
have specially defined arguments. It effectively adds two new commands
to bluetoothctl with the following output:

$ bluetoothctl agent help
on
off
DisplayOnly
DisplayYesNo
KeyboardDisplay
KeyboardOnly
NoInputNoOutput

$ bluetoothctl advertise help
on
off
peripheral
broadcast

Shell completion scripts can then parse this output to provide
completions for those commands.

---
 client/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/client/main.c b/client/main.c
index 578b3c7c3..0f810901a 100644
--- a/client/main.c
+++ b/client/main.c
@@ -800,6 +800,14 @@ static gboolean parse_argument(int argc, char *argv[], const char **arg_table,
 {
 	const char **opt;
 
+	if (!strcmp(argv[1], "help")) {
+		for (opt = arg_table; opt && *opt; opt++) {
+			bt_shell_printf("%s\n", *opt);
+		}
+		bt_shell_noninteractive_quit(EXIT_SUCCESS);
+		return FALSE;
+	}
+
 	if (!strcmp(argv[1], "on") || !strcmp(argv[1], "yes")) {
 		*value = TRUE;
 		if (option)
-- 
2.22.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH BlueZ v2 2/4] shared/shell: add --zsh-complete option
  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 ` Ronan Pigott
  2019-08-17  6:34 ` [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions Ronan Pigott
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ronan Pigott @ 2019-08-17  6:34 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

From: Ronan Pigott <rpigott@berkeley.edu>

This adds a new long form option --zsh-complete to provide all available
commands in an output format suitable for parsing by zsh or other shell
completion scripts.

Invoke like: `bluetoothctl --zsh-complete help`

There is no corresponding short form option.

---
 src/shared/shell.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index eac654f40..bbf9f9e7a 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -79,6 +79,7 @@ static struct {
 	int argc;
 	char **argv;
 	bool mode;
+	bool zsh;
 	bool monitor;
 	int timeout;
 	struct io *input;
@@ -98,6 +99,7 @@ static struct {
 } data;
 
 static void shell_print_menu(void);
+static void shell_print_menu_zsh_complete(void);
 
 static void cmd_version(int argc, char *argv[])
 {
@@ -288,6 +290,11 @@ static void shell_print_menu(void)
 	if (!data.menu)
 		return;
 
+	if (data.zsh) {
+		shell_print_menu_zsh_complete();
+		return;
+	}
+
 	print_text(COLOR_HIGHLIGHT, "Menu %s:", data.menu->name);
 	print_text(COLOR_HIGHLIGHT, "Available commands:");
 	print_text(COLOR_HIGHLIGHT, "-------------------");
@@ -314,6 +321,22 @@ static void shell_print_menu(void)
 	}
 }
 
+static void shell_print_menu_zsh_complete(void)
+{
+	const struct bt_shell_menu_entry *entry;
+
+	for (entry = data.menu->entries; entry->cmd; entry++) {
+		printf("%s:%s\n", entry->cmd, entry->desc ? : "");
+	}
+
+	for (entry = default_menu; entry->cmd; entry++) {
+		if (entry->exists && !entry->exists(data.menu))
+			continue;
+
+		printf("%s:%s\n", entry->cmd, entry->desc ? : "");
+	}
+}
+
 static int parse_args(char *arg, wordexp_t *w, char *del, int flags)
 {
 	char *str;
@@ -1015,6 +1038,7 @@ static const struct option main_options[] = {
 	{ "help",	no_argument, 0, 'h' },
 	{ "timeout",	required_argument, 0, 't' },
 	{ "monitor",	no_argument, 0, 'm' },
+	{ "zsh-complete",	no_argument, 0, 'z' },
 };
 
 static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
@@ -1075,6 +1099,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 		case 't':
 			data.timeout = atoi(optarg);
 			break;
+		case 'z':
+			data.zsh = 1;
+			break;
 		case 'm':
 			data.monitor = true;
 			if (bt_log_open() < 0) {
-- 
2.22.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions
  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
  2019-08-18  0:57   ` 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
  4 siblings, 1 reply; 7+ messages in thread
From: Ronan Pigott @ 2019-08-17  6:34 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH BlueZ v2 4/4] build: install zsh completions
  2019-08-17  6:34 [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Ronan Pigott
                   ` (2 preceding siblings ...)
  2019-08-17  6:34 ` [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions Ronan Pigott
@ 2019-08-17  6:34 ` " Ronan Pigott
  2019-09-06  8:13 ` [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Luiz Augusto von Dentz
  4 siblings, 0 replies; 7+ messages in thread
From: Ronan Pigott @ 2019-08-17  6:34 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

From: Ronan Pigott <rpigott@berkeley.edu>

---
 Makefile.tools |  5 +++++
 configure.ac   | 12 ++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/Makefile.tools b/Makefile.tools
index b6b99d216..81ed2e30d 100644
--- a/Makefile.tools
+++ b/Makefile.tools
@@ -12,6 +12,11 @@ client_bluetoothctl_LDADD = gdbus/libgdbus-internal.la src/libshared-glib.la \
 				$(GLIB_LIBS) $(DBUS_LIBS) -lreadline
 endif
 
+if ZSH_COMPLETIONS
+zshcompletiondir=$(ZSH_COMPLETIONDIR)
+dist_zshcompletion_DATA = completion/zsh/_bluetoothctl
+endif
+
 if MONITOR
 bin_PROGRAMS += monitor/btmon
 
diff --git a/configure.ac b/configure.ac
index 0afe1e6db..76612ff07 100644
--- a/configure.ac
+++ b/configure.ac
@@ -119,6 +119,18 @@ if (test -z "${path_dbussessionbusdir}"); then
 fi
 AC_SUBST(DBUS_SESSIONBUSDIR, [${path_dbussessionbusdir}])
 
+AC_ARG_WITH([zsh-completion-dir], AC_HELP_STRING([--with-zsh-completion-dir=DIR],
+				[path to install zsh completions]),
+					[path_zshcompletiondir=${withval}],
+						[path_zshcompletiondir="yes"])
+
+if (test "${path_zshcompletiondir}" = "yes"); then
+	path_zshcompletiondir="$datarootdir/zsh/site-functions"
+	AC_MSG_RESULT([${path_zshcompletiondir}])
+fi
+AC_SUBST(ZSH_COMPLETIONDIR, [${path_zshcompletiondir}])
+AM_CONDITIONAL(ZSH_COMPLETIONS, test "${path_zshcompletiondir}" != "no")
+
 AC_ARG_ENABLE(backtrace, AC_HELP_STRING([--enable-backtrace],
 		[compile backtrace support]), [enable_backtrace=${enableval}])
 
-- 
2.22.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions
  2019-08-17  6:34 ` [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions Ronan Pigott
@ 2019-08-18  0:57   ` Ronan Pigott
  0 siblings, 0 replies; 7+ messages in thread
From: Ronan Pigott @ 2019-08-18  0:57 UTC (permalink / raw)
  To: Ronan Pigott, linux-bluetooth

On Fri Aug 16, 2019 at 11:34 PM Ronan Pigott wrote:
> +	_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]' \

An error: the -t,--timeout option is missing the required parameter.
There are no interesting completions to offer here, but it should stop
completing command words where an argument is required.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl
  2019-08-17  6:34 [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Ronan Pigott
                   ` (3 preceding siblings ...)
  2019-08-17  6:34 ` [PATCH BlueZ v2 4/4] build: install " Ronan Pigott
@ 2019-09-06  8:13 ` Luiz Augusto von Dentz
  4 siblings, 0 replies; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2019-09-06  8:13 UTC (permalink / raw)
  To: Ronan Pigott; +Cc: linux-bluetooth, Ronan Pigott

Hi Ronan,

On Sat, Aug 17, 2019 at 9:41 AM Ronan Pigott <rpigott314@gmail.com> wrote:
>
> From: Ronan Pigott <rpigott@berkeley.edu>
>
> V2 of zsh completions for bluetoothctl
>
> bluetoothctl has a nice interactive interface, but some of its functions
> can be accessed quicker non-interactively, straight from the command
> line. I made these zsh completions to facilitate that.
>
> If you are a zsh user, try them out!
>
> Ronan Pigott (4):
>   client/main: add help option for available args
>   shared/shell: add --zsh-complete option
>   completion: add bluetoothctl zsh completions
>   build: install zsh completions
>
>  Makefile.tools               |  5 ++
>  client/main.c                |  8 +++
>  completion/zsh/_bluetoothctl | 98 ++++++++++++++++++++++++++++++++++++
>  configure.ac                 | 12 +++++
>  src/shared/shell.c           | 27 ++++++++++
>  5 files changed, 150 insertions(+)
>  create mode 100644 completion/zsh/_bluetoothctl
>
> --
> 2.22.1

Applied, thanks.

-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, back to index

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions Ronan Pigott
2019-08-18  0:57   ` 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

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