linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH BlueZ 0/3] Add zsh completions for bluetoothctl
@ 2019-08-16  7:43 Ronan Pigott
  2019-08-16  7:43 ` [PATCH BlueZ 1/3] client/main: add help option for available args Ronan Pigott
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ronan Pigott @ 2019-08-16  7:43 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

From: Ronan Pigott <rpigott@berkeley.edu>

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, and I thought
to submit them upstream, if you like, so now I've made this patch. If you
are a zsh user, try them out!

I also added the 'help' option in order to help the shell completions
get their value from the same source as the interactive shell completions.

Ronan Pigott (3):
  client/main: add help option for available args
  completion: add bluetoothctl zsh completions
  build: install zsh completions

 Makefile.tools               |   5 ++
 client/main.c                |   8 +++
 completion/zsh/_bluetoothctl | 131 +++++++++++++++++++++++++++++++++++
 configure.ac                 |  12 ++++
 4 files changed, 156 insertions(+)
 create mode 100644 completion/zsh/_bluetoothctl

-- 
2.22.1


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

* [PATCH BlueZ 1/3] client/main: add help option for available args
  2019-08-16  7:43 [PATCH BlueZ 0/3] Add zsh completions for bluetoothctl Ronan Pigott
@ 2019-08-16  7:43 ` Ronan Pigott
  2019-08-16 11:29   ` Luiz Augusto von Dentz
  2019-08-16  7:43 ` [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions Ronan Pigott
  2019-08-16  7:43 ` [PATCH BlueZ 3/3] build: install " Ronan Pigott
  2 siblings, 1 reply; 6+ messages in thread
From: Ronan Pigott @ 2019-08-16  7:43 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

From: Ronan Pigott <rpigott@berkeley.edu>

---
 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 related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions
  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  7:43 ` Ronan Pigott
  2019-08-16 11:36   ` Luiz Augusto von Dentz
  2019-08-16  7:43 ` [PATCH BlueZ 3/3] build: install " Ronan Pigott
  2 siblings, 1 reply; 6+ messages in thread
From: Ronan Pigott @ 2019-08-16  7:43 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ronan Pigott

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


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

* [PATCH BlueZ 3/3] build: install zsh completions
  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  7:43 ` [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions Ronan Pigott
@ 2019-08-16  7:43 ` Ronan Pigott
  2 siblings, 0 replies; 6+ messages in thread
From: Ronan Pigott @ 2019-08-16  7:43 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 related	[flat|nested] 6+ messages in thread

* Re: [PATCH BlueZ 1/3] client/main: add help option for available args
  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
  0 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2019-08-16 11:29 UTC (permalink / raw)
  To: Ronan Pigott; +Cc: linux-bluetooth, Ronan Pigott

Hi Ronan,

On Fri, Aug 16, 2019 at 10:46 AM Ronan Pigott <rpigott314@gmail.com> wrote:
>
> From: Ronan Pigott <rpigott@berkeley.edu>

Can you a description of what the changes does, perhaps a sample when
uses in some command.

> ---
>  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
>


-- 
Luiz Augusto von Dentz

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

* Re: [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions
  2019-08-16  7:43 ` [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions Ronan Pigott
@ 2019-08-16 11:36   ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2019-08-16 11:36 UTC (permalink / raw)
  To: Ronan Pigott; +Cc: linux-bluetooth, Ronan Pigott

Hi Ronan,

On Fri, Aug 16, 2019 at 10:48 AM Ronan Pigott <rpigott314@gmail.com> wrote:
>
> 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

Id love to see this being generated by bt_shell automatically instead
i.e ./bluetoothctl --zsh-complete/--bash-complete, that can then be
used by the auto complete script generate the commands, which seems to
be what you did with help that would print the arguments but done to
the commands as well.


-- 
Luiz Augusto von Dentz

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

end of thread, other threads:[~2019-08-16 11:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH BlueZ 2/3] completion: add bluetoothctl zsh completions Ronan Pigott
2019-08-16 11:36   ` Luiz Augusto von Dentz
2019-08-16  7:43 ` [PATCH BlueZ 3/3] build: install " Ronan Pigott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).