2019-08-17 06:39:31

by Ronan Pigott

[permalink] [raw]
Subject: [PATCH BlueZ v2 0/4] 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

--
2.22.1


2019-08-17 06:41:29

by Ronan Pigott

[permalink] [raw]
Subject: [PATCH BlueZ v2 2/4] shared/shell: add --zsh-complete option

From: 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(+)

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

2019-08-17 06:41:29

by Ronan Pigott

[permalink] [raw]
Subject: [PATCH BlueZ v2 4/4] build: install zsh completions

From: Ronan Pigott <[email protected]>

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

2019-09-06 11:09:35

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl

Hi Ronan,

On Sat, Aug 17, 2019 at 9:41 AM Ronan Pigott <[email protected]> wrote:
>
> 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
>
> --
> 2.22.1

Applied, thanks.

--
Luiz Augusto von Dentz