Implement bash completion for gsettings

This commit is contained in:
Matthias Clasen 2010-06-27 16:00:20 -04:00
parent 795d2bf8cf
commit 025435329a
3 changed files with 455 additions and 80 deletions

View File

@ -590,7 +590,9 @@ gdbus_LDADD = libgio-2.0.la \
$(top_builddir)/gobject/libgobject-2.0.la $(top_builddir)/gobject/libgobject-2.0.la
completiondir = $(sysconfdir)/bash_completion.d completiondir = $(sysconfdir)/bash_completion.d
completion_SCRIPTS = gdbus-bash-completion.sh completion_SCRIPTS = \
gdbus-bash-completion.sh \
gsettings-bash-completion.sh
EXTRA_DIST += $(completion_SCRIPTS) EXTRA_DIST += $(completion_SCRIPTS)
# ------------------------------------------------------------------------ # ------------------------------------------------------------------------

View File

@ -0,0 +1,33 @@
# Check for bash
[ -z "$BASH_VERSION" ] && return
####################################################################################################
__gsettings() {
local IFS=$'\n'
local cur=`_get_cword :`
local suggestions=$(gsettings complete "${COMP_LINE}" ${COMP_POINT})
COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))
# Remove colon-word prefix from COMPREPLY items
case "$cur" in
*:*)
case "$COMP_WORDBREAKS" in
*:*)
local colon_word=${cur%${cur##*:}}
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
done
;;
esac
;;
esac
}
####################################################################################################
complete -o nospace -F __gsettings gsettings

View File

@ -21,10 +21,48 @@
#include "config.h" #include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <locale.h> #include <locale.h>
#include <gi18n.h> #include <gi18n.h>
#include <gio.h> #include <gio.h>
static gchar *
pick_word_at (const gchar *s,
gint cursor,
gint *out_word_begins_at)
{
gint begin;
gint end;
if (s[0] == '\0')
{
if (out_word_begins_at != NULL)
*out_word_begins_at = -1;
return NULL;
}
if (g_ascii_isspace (s[cursor]) &&
((cursor > 0 && g_ascii_isspace (s[cursor-1])) || cursor == 0))
{
if (out_word_begins_at != NULL)
*out_word_begins_at = cursor;
return g_strdup ("");
}
while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
cursor--;
begin = cursor;
end = begin;
while (!g_ascii_isspace (s[end]) && s[end] != '\0')
end++;
if (out_word_begins_at != NULL)
*out_word_begins_at = begin;
return g_strndup (s + begin, end - begin);
}
static gint static gint
usage (gint *argc, usage (gint *argc,
gchar **argv[], gchar **argv[],
@ -73,6 +111,7 @@ remove_arg (gint num, gint *argc, gchar **argv[])
(*argc) = (*argc) - 1; (*argc) = (*argc) - 1;
} }
static void static void
modify_argv0_for_command (gint *argc, modify_argv0_for_command (gint *argc,
gchar **argv[], gchar **argv[],
@ -87,10 +126,86 @@ modify_argv0_for_command (gint *argc,
(*argv)[0] = s; (*argv)[0] = s;
} }
static gboolean
schema_exists (const gchar *name)
{
const gchar * const *schemas;
gint i;
schemas = g_settings_list_schemas ();
for (i = 0; schemas[i]; i++)
if (g_strcmp0 (name, schemas[i]) == 0)
return TRUE;
return FALSE;
}
static void
list_schemas (const gchar *prefix)
{
const gchar * const *schemas;
gint i;
schemas = g_settings_list_schemas ();
for (i = 0; schemas[i]; i++)
if (prefix == NULL || g_str_has_prefix (schemas[i], prefix))
g_print ("%s \n", schemas[i]);
}
static gboolean
key_exists (GSettings *settings,
const gchar *name)
{
const gchar **keys;
gint i;
gboolean ret;
ret = FALSE;
keys = g_settings_list_keys (settings);
for (i = 0; keys[i]; i++)
if (g_strcmp0 (keys[i], name) == 0)
{
ret = TRUE;
break;
}
g_free (keys);
return ret;
}
static void
list_keys (GSettings *settings,
const gchar *prefix)
{
const gchar **keys;
gint i;
keys = g_settings_list_keys (settings);
for (i = 0; keys[i]; i++)
if (prefix == NULL || g_str_has_prefix (keys[i], prefix))
g_print ("%s \n", keys[i]);
g_free (keys);
}
static void
list_options (GOptionContext *context,
const gchar *prefix)
{
/* FIXME extract options from context */
const gchar *options[] = { "--help", "--path", NULL };
gint i;
for (i = 0; options[i]; i++)
if (g_str_has_prefix (options[i], prefix))
g_print ("%s \n", options[i]);
}
static gint static gint
handle_get (gint *argc, handle_get (gint *argc,
gchar **argv[]) gchar **argv[],
gboolean request_completion,
gchar *completion_cur,
gchar *completion_prev)
{ {
gchar *schema; gchar *schema;
gchar *path; gchar *path;
@ -116,10 +231,15 @@ handle_get (gint *argc,
" KEY The name of the key\n")); " KEY The name of the key\n"));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
settings = NULL;
path = NULL; path = NULL;
schema = NULL;
key = NULL;
error = NULL; error = NULL;
if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3) if (!g_option_context_parse (context, argc, argv, NULL))
{
if (!request_completion)
{ {
gchar *s; gchar *s;
s = g_option_context_get_help (context, FALSE, NULL); s = g_option_context_get_help (context, FALSE, NULL);
@ -128,21 +248,51 @@ handle_get (gint *argc,
goto out; goto out;
} }
}
if (*argc > 1)
schema = (*argv)[1]; schema = (*argv)[1];
if (*argc > 2)
key = (*argv)[2]; key = (*argv)[2];
if (request_completion && completion_cur[0] == '-')
{
list_options (context, completion_cur);
ret = 0;
goto out;
}
if (request_completion && !schema_exists (schema))
{
list_schemas (schema);
ret = 0;
goto out;
}
if (path) if (path)
settings = g_settings_new_with_path (schema, path); settings = g_settings_new_with_path (schema, path);
else else
settings = g_settings_new (schema); settings = g_settings_new (schema);
if (request_completion && !key_exists (settings, key))
{
list_keys (settings, key);
ret = 0;
goto out;
}
if (!request_completion)
{
v = g_settings_get_value (settings, key); v = g_settings_get_value (settings, key);
g_print ("%s\n", g_variant_print (v, FALSE)); g_print ("%s\n", g_variant_print (v, FALSE));
g_variant_unref (v); g_variant_unref (v);
ret = 0; ret = 0;
}
out: out:
if (settings)
g_object_unref (settings);
g_option_context_free (context); g_option_context_free (context);
return ret; return ret;
@ -150,7 +300,10 @@ handle_get (gint *argc,
static gint static gint
handle_set (gint *argc, handle_set (gint *argc,
gchar **argv[]) gchar **argv[],
gboolean request_completion,
gchar *completion_cur,
gchar *completion_prev)
{ {
gchar *schema; gchar *schema;
gchar *path; gchar *path;
@ -179,28 +332,59 @@ handle_set (gint *argc,
" VALUE The value to set key to, as a serialized GVariant\n")); " VALUE The value to set key to, as a serialized GVariant\n"));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
settings = NULL;
path = NULL; path = NULL;
schema = NULL;
key = NULL;
error = NULL; error = NULL;
if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 4) if (!g_option_context_parse (context, argc, argv, NULL))
{
if (!request_completion)
{ {
gchar *s; gchar *s;
s = g_option_context_get_help (context, FALSE, NULL); s = g_option_context_get_help (context, FALSE, NULL);
g_printerr ("%s", s); g_printerr ("%s", s);
g_free (s); g_free (s);
goto out;
}
}
if (*argc > 1)
schema = (*argv)[1];
if (*argc > 2)
key = (*argv)[2];
if (*argc > 3)
value = (*argv)[3];
if (request_completion && completion_cur[0] == '-')
{
list_options (context, completion_cur);
ret = 0;
goto out; goto out;
} }
schema = (*argv)[1]; if (request_completion && !schema_exists (schema))
key = (*argv)[2]; {
value = (*argv)[3]; list_schemas (schema);
ret = 0;
goto out;
}
if (path) if (path)
settings = g_settings_new_with_path (schema, path); settings = g_settings_new_with_path (schema, path);
else else
settings = g_settings_new (schema); settings = g_settings_new (schema);
if (request_completion && !key_exists (settings, key))
{
list_keys (settings, key);
ret = 0;
goto out;
}
if (!request_completion)
{
default_v = g_settings_get_value (settings, key); default_v = g_settings_get_value (settings, key);
type = g_variant_get_type (default_v); type = g_variant_get_type (default_v);
@ -221,8 +405,12 @@ handle_set (gint *argc,
g_settings_sync (); g_settings_sync ();
ret = 0; ret = 0;
}
out: out:
if (settings)
g_object_unref (settings);
g_option_context_free (context); g_option_context_free (context);
return ret; return ret;
@ -230,7 +418,10 @@ handle_set (gint *argc,
static gint static gint
handle_writable (gint *argc, handle_writable (gint *argc,
gchar **argv[]) gchar **argv[],
gboolean request_completion,
gchar *completion_cur,
gchar *completion_prev)
{ {
gchar *schema; gchar *schema;
gchar *path; gchar *path;
@ -255,34 +446,68 @@ handle_writable (gint *argc,
" KEY The name of the key\n")); " KEY The name of the key\n"));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
settings = NULL;
path = NULL; path = NULL;
schema = NULL;
key = NULL;
error = NULL; error = NULL;
if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3) if (!g_option_context_parse (context, argc, argv, NULL))
{
if (!request_completion)
{ {
gchar *s; gchar *s;
s = g_option_context_get_help (context, FALSE, NULL); s = g_option_context_get_help (context, FALSE, NULL);
g_printerr ("%s", s); g_printerr ("%s", s);
g_free (s); g_free (s);
goto out;
}
}
if (*argc > 1)
schema = (*argv)[1];
if (*argc > 2)
key = (*argv)[2];
if (request_completion && completion_cur[0] == '-')
{
list_options (context, completion_cur);
ret = 0;
goto out; goto out;
} }
schema = (*argv)[1]; if (request_completion && !schema_exists (schema))
key = (*argv)[2]; {
list_schemas (schema);
ret = 0;
goto out;
}
if (path) if (path)
settings = g_settings_new_with_path (schema, path); settings = g_settings_new_with_path (schema, path);
else else
settings = g_settings_new (schema); settings = g_settings_new (schema);
if (request_completion && !key_exists (settings, key))
{
list_keys (settings, key);
ret = 0;
goto out;
}
if (!request_completion)
{
if (g_settings_is_writable (settings, key)) if (g_settings_is_writable (settings, key))
g_print ("true\n"); g_print ("true\n");
else else
g_print ("false\n"); g_print ("false\n");
ret = 0; ret = 0;
}
out: out:
if (settings)
g_object_unref (settings);
g_option_context_free (context); g_option_context_free (context);
return ret; return ret;
@ -304,7 +529,10 @@ key_changed (GSettings *settings,
static gint static gint
handle_monitor (gint *argc, handle_monitor (gint *argc,
gchar **argv[]) gchar **argv[],
gboolean request_completion,
gchar *completion_cur,
gchar *completion_prev)
{ {
gchar *schema; gchar *schema;
gchar *path; gchar *path;
@ -334,27 +562,57 @@ handle_monitor (gint *argc,
" KEY The name of the key\n")); " KEY The name of the key\n"));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
settings = NULL;
path = NULL; path = NULL;
schema = NULL;
key = NULL;
error = NULL; error = NULL;
if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3) if (!g_option_context_parse (context, argc, argv, NULL))
{
if (!request_completion)
{ {
gchar *s; gchar *s;
s = g_option_context_get_help (context, FALSE, NULL); s = g_option_context_get_help (context, FALSE, NULL);
g_printerr ("%s", s); g_printerr ("%s", s);
g_free (s); g_free (s);
goto out;
}
}
if (*argc > 1)
schema = (*argv)[1];
if (*argc > 2)
key = (*argv)[2];
if (request_completion && completion_cur[0] == '-')
{
list_options (context, completion_cur);
ret = 0;
goto out; goto out;
} }
schema = (*argv)[1]; if (request_completion && !schema_exists (schema))
key = (*argv)[2]; {
list_schemas (schema);
ret = 0;
goto out;
}
if (path) if (path)
settings = g_settings_new_with_path (schema, path); settings = g_settings_new_with_path (schema, path);
else else
settings = g_settings_new (schema); settings = g_settings_new (schema);
if (request_completion && !key_exists (settings, key))
{
list_keys (settings, key);
ret = 0;
goto out;
}
if (!request_completion)
{
detailed_signal = g_strdup_printf ("changed::%s", key); detailed_signal = g_strdup_printf ("changed::%s", key);
g_signal_connect (settings, detailed_signal, g_signal_connect (settings, detailed_signal,
G_CALLBACK (key_changed), NULL); G_CALLBACK (key_changed), NULL);
@ -362,8 +620,13 @@ handle_monitor (gint *argc,
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop); g_main_loop_run (loop);
g_main_loop_unref (loop); g_main_loop_unref (loop);
ret = 0;
}
out: out:
if (settings)
g_object_unref (settings);
g_option_context_free (context); g_option_context_free (context);
return ret; return ret;
@ -371,29 +634,106 @@ handle_monitor (gint *argc,
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
gboolean ret = 1; gboolean ret;
gchar *command;
gboolean request_completion;
gchar *completion_cur;
gchar *completion_prev;
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
g_type_init (); g_type_init ();
ret = 1;
completion_cur = NULL;
completion_prev = NULL;
request_completion = FALSE;
if (argc < 2) if (argc < 2)
{
ret = usage (&argc, &argv, FALSE); ret = usage (&argc, &argv, FALSE);
else if (g_strcmp0 (argv[1], "help") == 0) goto out;
}
again:
command = argv[1];
if (g_strcmp0 (command, "help") == 0)
{
if (!request_completion)
ret = usage (&argc, &argv, TRUE); ret = usage (&argc, &argv, TRUE);
else if (g_strcmp0 (argv[1], "get") == 0) }
ret = handle_get (&argc, &argv); else if (g_strcmp0 (command, "get") == 0)
else if (g_strcmp0 (argv[1], "set") == 0) ret = handle_get (&argc, &argv, request_completion, completion_cur, completion_prev);
ret = handle_set (&argc, &argv); else if (g_strcmp0 (command, "set") == 0)
else if (g_strcmp0 (argv[1], "monitor") == 0) ret = handle_set (&argc, &argv, request_completion, completion_cur, completion_prev);
ret = handle_monitor (&argc, &argv); else if (g_strcmp0 (command, "monitor") == 0)
else if (g_strcmp0 (argv[1], "writable") == 0) ret = handle_monitor (&argc, &argv, request_completion, completion_cur, completion_prev);
ret = handle_writable (&argc, &argv); else if (g_strcmp0 (command, "writable") == 0)
ret = handle_writable (&argc, &argv, request_completion, completion_cur, completion_prev);
else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
{
gchar *completion_line;
gint completion_point;
gchar *endp;
gchar **completion_argv;
gint completion_argc;
gint cur_begin;
request_completion = TRUE;
completion_line = argv[2];
completion_point = strtol (argv[3], &endp, 10);
if (endp == argv[3] || *endp != '\0')
goto out;
if (!g_shell_parse_argv (completion_line,
&completion_argc,
&completion_argv,
NULL))
{
/* can't parse partical cmdline, don't attempt completion */
goto out;
}
completion_prev = NULL;
completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
if (cur_begin > 0)
{
gint prev_end;
for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
{
if (!g_ascii_isspace (completion_line[prev_end]))
{
completion_prev = pick_word_at (completion_line, prev_end, NULL);
break;
}
}
}
argc = completion_argc;
argv = completion_argv;
ret = 0;
goto again;
}
else
{
if (request_completion)
{
g_print ("help \nget \nmonitor \nwritable \nset \n");
ret = 0;
}
else else
{ {
g_printerr (_("Unknown command '%s'\n"), argv[1]); g_printerr (_("Unknown command '%s'\n"), argv[1]);
ret = usage (&argc, &argv, FALSE); ret = usage (&argc, &argv, FALSE);
} }
}
out:
g_free (completion_cur);
g_free (completion_prev);
return ret; return ret;
} }