]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib, vtysh: pretty-print variable autocompletions
authorQuentin Young <qlyoung@cumulusnetworks.com>
Wed, 12 Jul 2017 18:40:18 +0000 (14:40 -0400)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Thu, 13 Jul 2017 14:59:13 +0000 (10:59 -0400)
Pretty-prints variable autocompletions by breaking them up into multiple
lines, indenting them consistently and respecting the column width of
the terminal.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/command.c
lib/command.h
lib/vty.c
vtysh/vtysh.c

index 5ca4a0fda9b42f65c2603f9e63365501ff3b6cff..03918b7217df6841082d1204bcf3023a1415bffb 100644 (file)
@@ -715,6 +715,41 @@ cmd_variable_complete (struct cmd_token *token, const char *arg, vector comps)
   vector_free(tmpcomps);
 }
 
+#define AUTOCOMP_INDENT 5
+
+char *
+cmd_variable_comp2str(vector comps, unsigned short cols, const char nl[])
+{
+  size_t bsz = 16;
+  char *buf = XCALLOC(MTYPE_TMP, bsz);
+  int lc = AUTOCOMP_INDENT;
+  size_t cs = AUTOCOMP_INDENT;
+  size_t nllen = strlen(nl);
+  size_t itemlen;
+  snprintf(buf, bsz, "%*s", AUTOCOMP_INDENT, "");
+  for (size_t j = 0; j < vector_active (comps); j++)
+    {
+      char *item = vector_slot (comps, j);
+      itemlen = strlen(item);
+
+      if (cs + itemlen + nllen + AUTOCOMP_INDENT + 2 >= bsz)
+        buf = XREALLOC(MTYPE_TMP, buf, (bsz *= 2));
+
+      if (lc + itemlen + 1 >= cols)
+        {
+          cs += snprintf(&buf[cs], bsz - cs, "%s%*s", nl, AUTOCOMP_INDENT, "");
+          lc = AUTOCOMP_INDENT;
+        }
+
+      size_t written = snprintf(&buf[cs], bsz - cs, "%s ", item);
+      lc += written;
+      cs += written;
+      XFREE (MTYPE_COMPLETION, item);
+      vector_set_index (comps, j, NULL);
+    }
+  return buf;
+}
+
 void
 cmd_variable_handler_register (const struct cmd_variable_handler *cvh)
 {
index 927c04006c5d6bb36029046e22d90c4e4fb88f75..781387b5e62e5423d90a955783667edf11ac0479 100644 (file)
@@ -404,5 +404,6 @@ struct cmd_variable_handler {
 
 extern void cmd_variable_complete (struct cmd_token *token, const char *arg, vector comps);
 extern void cmd_variable_handler_register (const struct cmd_variable_handler *cvh);
+extern char *cmd_variable_comp2str (vector comps, unsigned short cols, const char nl[]);
 
 #endif /* _ZEBRA_COMMAND_H */
index e6497b31002d0dda84ea0c21dba1a871fd20dfae..dd8dac74b61f6094f036672a482fc82d07869850 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -1134,17 +1134,13 @@ vty_describe_command (struct vty *vty)
             vector varcomps = vector_init (VECTOR_MIN_SIZE);
             cmd_variable_complete (token, ref, varcomps);
 
-            if (vector_active(varcomps) > 0)
+            if (vector_active (varcomps) > 0)
               {
-                vty_out(vty, "     ");
-                for (size_t j = 0; j < vector_active (varcomps); j++)
-                  {
-                    char *item = vector_slot (varcomps, j);
-                    vty_out(vty, " %s", item);
-                    XFREE(MTYPE_COMPLETION, item);
-                  }
-                vty_out (vty, VTYNL);
+                char *ac = cmd_variable_comp2str(varcomps, vty->width, VTYNL);
+                vty_outln(vty, "%s", ac);
+                XFREE(MTYPE_TMP, ac);
               }
+
             vector_free(varcomps);
           }
 #if 0
index f02bd0c8888a9e96396f340bfd24b7970c3b6b4a..7df02e19ceaaafa9a8cb8ae45cca46ac2833553e 100644 (file)
@@ -771,7 +771,7 @@ vtysh_rl_describe (void)
       rl_on_new_line ();
       return 0;
       break;
-    }  
+    }
 
   /* Get width of command string. */
   width = 0;
@@ -808,15 +808,14 @@ vtysh_rl_describe (void)
 
             if (vector_active (varcomps) > 0)
               {
-                fprintf(stdout, "     ");
-                for (size_t j = 0; j < vector_active (varcomps); j++)
-                  {
-                    char *item = vector_slot (varcomps, j);
-                    fprintf (stdout, " %s", item);
-                    XFREE (MTYPE_COMPLETION, item);
-                  }
-                vty_out (vty, VTYNL);
+                int rows, cols;
+                rl_get_screen_size(&rows, &cols);
+
+                char *ac = cmd_variable_comp2str(varcomps, cols, "\n");
+                fprintf(stdout, "%s\n", ac);
+                XFREE(MTYPE_TMP, ac);
               }
+
             vector_free (varcomps);
           }
       }