[PATCH 12/18] vsh: Prune string list returned by completer

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

 



Instead of having completers prune returned string list based on
user's input we can do that right after the callback is called.
Only strings matching the prefix will be presented to the user
then.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tools/vsh.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index 10a4ef69c..49e8033bd 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -2735,6 +2735,40 @@ vshReadlineCommandFindOpt(const vshCmd *partial,
 }
 
 
+static int
+vshCompleterFilter(char ***list,
+                   const char *text)
+{
+    char **newList = NULL;
+    size_t newList_len = 0;
+    size_t list_len;
+    size_t i;
+
+    if (!list || !*list)
+        return -1;
+
+    list_len = virStringListLength((const char **) *list);
+
+    if (VIR_ALLOC_N(newList, list_len + 1) < 0)
+        return -1;
+
+    for (i = 0; i < list_len; i++) {
+        if (!STRPREFIX((*list)[i], text)) {
+            VIR_FREE((*list)[i]);
+            continue;
+        }
+
+        VIR_STEAL_PTR(newList[newList_len], (*list)[i]);
+        newList_len++;
+    }
+
+    ignore_value(VIR_REALLOC_N_QUIET(newList, newList_len + 1));
+    VIR_FREE(*list);
+    *list = newList;
+    return 0;
+}
+
+
 static char *
 vshReadlineParse(const char *text, int state)
 {
@@ -2790,7 +2824,14 @@ vshReadlineParse(const char *text, int state)
                 char **completer_list = opt->completer(autoCompleteOpaque,
                                                        partial,
                                                        opt->completer_flags);
-                if (virStringListMerge(&list, &completer_list) < 0) {
+
+                /* For string list returned by completer we have to do
+                 * filtering based on @text because completer returns all
+                 * possible strings. */
+
+                if (completer_list &&
+                    (vshCompleterFilter(&completer_list, text) < 0 ||
+                     virStringListMerge(&list, &completer_list) < 0)) {
                     virStringListFree(completer_list);
                     goto cleanup;
                 }
-- 
2.13.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux