Add zsh completions for bluetoothctl
Subsystems (2)
v2 cover Add zsh completions for bluetoothctl
From: Ronan Pigott <[email protected]>
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
Signature · 1 line
2.22.1
PATCH v2 1/4 client/main: add help option for available args
From: Ronan Pigott <[email protected]>
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(+)
client/main.c +8 -0
@@ -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)
Signature · 1 line
2.22.1
PATCH v2 2/4 shared/shell: add --zsh-complete option
In reply to Ronan PigottFrom: Ronan Pigott <[email protected]>
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(+)
src/shared/shell.c +27 -0
@@ -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 ) {
Signature · 1 line
2.22.1
PATCH v2 3/4 completion: add bluetoothctl zsh completions
In reply to Ronan PigottFrom: Ronan Pigott <[email protected]>
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
completion/zsh/_bluetoothctl new file +98 -0
@@ -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
Signature · 1 line
2.22.1
PATCH v2 4/4 build: install zsh completions
In reply to Ronan PigottFrom: Ronan Pigott <[email protected]>
--- Makefile.tools | 5 +++++ configure.ac | 12 ++++++++++++ 2 files changed, 17 insertions(+)
Makefile.tools +5 -0
@@ -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
configure.ac +12 -0
@@ -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}])
Signature · 1 line
2.22.1
Re: [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions
In reply to Ronan Pigottcompletion/zsh/_bluetoothctl line 68
Quoted 8 lines · Ronan Pigott
completion/zsh/_bluetoothctl new file +8
-0+ _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.
Hi Ronan,
Quoted 25 lines · Ronan Pigott
From: Ronan Pigott <[email protected]>
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 completionsMakefile.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
Quoted 2 lines
--
2.22.1
Applied, thanks.
Signature · 1 line
Luiz Augusto von Dentz