[dconf] dconf cli: commandline completion



commit a7de856e9754ad539cfb37c622513e48e6b67d54
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu May 5 14:00:30 2011 +0200

    dconf cli: commandline completion

 bin/Makefile.am              |    4 +++
 bin/dconf-bash-completion.sh |   33 ++++++++++++++++++++++++++++++
 bin/dconf.vala               |   46 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 75 insertions(+), 8 deletions(-)
---
diff --git a/bin/Makefile.am b/bin/Makefile.am
index f830052..f88de24 100644
--- a/bin/Makefile.am
+++ b/bin/Makefile.am
@@ -7,3 +7,7 @@ bin_PROGRAMS = dconf
 dconf_VALAFLAGS = --vapidir ../client --pkg=gio-2.0 --pkg=posix --pkg=dconf
 dconf_LDADD = $(gio_LIBS) ../client/libdconf.so.0
 dconf_SOURCES = dconf.vala dconf-update.vala ../gvdb/gvdb-builder.c gvdb.vapi fixes.vapi
+
+completiondir = $(sysconfdir)/bash_completion.d
+completion_SCRIPTS = dconf-bash-completion.sh
+EXTRA_DIST = $(completion_SCRIPTS)
diff --git a/bin/dconf-bash-completion.sh b/bin/dconf-bash-completion.sh
new file mode 100644
index 0000000..4d10310
--- /dev/null
+++ b/bin/dconf-bash-completion.sh
@@ -0,0 +1,33 @@
+
+# Check for bash
+[ -z "$BASH_VERSION" ] && return
+
+####################################################################################################
+
+__dconf() {
+  local choices
+
+  case "${COMP_CWORD}" in
+    1)
+      choices=$'help \nread \nlist \nwrite \nupdate \nlock \nunlock \nwatch '
+      ;;
+
+    2)
+      case "${COMP_WORDS[1]}" in
+        help)
+          choices=$'help \nread \nlist \nwrite \nupdate \nlock \nunlock \nwatch '
+          ;;
+        read|list|write|lock|unlock|watch)
+          choices="$(dconf _complete "${COMP_WORDS[2]}")"
+          ;;
+      esac
+      ;;
+  esac
+
+  local IFS=$'\n'
+  COMPREPLY=($(compgen -W "${choices}" "${COMP_WORDS[$COMP_CWORD]}"))
+}
+
+####################################################################################################
+
+complete -o nospace -F __dconf dconf
diff --git a/bin/dconf.vala b/bin/dconf.vala
index 47adf0b..630ff9c 100644
--- a/bin/dconf.vala
+++ b/bin/dconf.vala
@@ -198,6 +198,35 @@ void dconf_watch (string?[] args) throws Error {
 	new MainLoop (null, false).run ();
 }
 
+void dconf_complete (string[] args) throws Error {
+	var path = args[2];
+
+	if (path == "") {
+		print ("/\n");
+		return;
+	}
+
+	if (path[0] == '/') {
+		var client = new DConf.Client ();
+		var last = 0;
+
+		for (var i = 1; path[i] != '\0'; i++) {
+			if (path[i] == '/') {
+				last = i;
+			}
+		}
+
+		var dir = path.substring (0, last + 1);
+		foreach (var item in client.list (dir)) {
+			var full_item = dir + item;
+
+			if (full_item.has_prefix (path)) {
+				print ("%s\n", full_item);
+			}
+		}
+	}
+}
+
 delegate void Command (string[] args) throws Error;
 
 struct CommandMapping {
@@ -215,14 +244,15 @@ int main (string[] args) {
 	Environment.set_prgname (args[0]);
 
 	var map = new CommandMapping[] {
-		CommandMapping ("help",   dconf_help),
-		CommandMapping ("read",   dconf_read),
-		CommandMapping ("list",   dconf_list),
-		CommandMapping ("write",  dconf_write),
-		CommandMapping ("update", dconf_update),
-		CommandMapping ("lock",   dconf_lock),
-		CommandMapping ("unlock", dconf_unlock),
-		CommandMapping ("watch",  dconf_watch)
+		CommandMapping ("help",      dconf_help),
+		CommandMapping ("read",      dconf_read),
+		CommandMapping ("list",      dconf_list),
+		CommandMapping ("write",     dconf_write),
+		CommandMapping ("update",    dconf_update),
+		CommandMapping ("lock",      dconf_lock),
+		CommandMapping ("unlock",    dconf_unlock),
+		CommandMapping ("watch",     dconf_watch),
+		CommandMapping ("_complete", dconf_complete)
 	};
 
 	try {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]