summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss White <russ@riw.us>2017-07-14 07:10:13 -0400
committerGitHub <noreply@github.com>2017-07-14 07:10:13 -0400
commit79af1cb3384c47e374d118281c17a32ea03885df (patch)
tree02537ca38af0d623d43f92df48b048c9237b0cf6
parentac45e83c04c4a5d8846c50726a2cbcf72943b8fb (diff)
parent1a0f614dd29f8666647e005c63ae00b3638234d4 (diff)
Merge pull request #808 from qlyoung/vtysh-termcols
lib, vtysh: pretty-print variable autocompletions
-rw-r--r--lib/command.c35
-rw-r--r--lib/command.h1
-rw-r--r--lib/vty.c14
-rw-r--r--vtysh/vtysh.c17
4 files changed, 49 insertions, 18 deletions
diff --git a/lib/command.c b/lib/command.c
index bf36a43d3d..18a28f57c0 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -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)
{
diff --git a/lib/command.h b/lib/command.h
index 4a261499d4..f712407b7f 100644
--- a/lib/command.h
+++ b/lib/command.h
@@ -406,5 +406,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 */
diff --git a/lib/vty.c b/lib/vty.c
index 00a4e9bf71..2c25bd2fc3 100644
--- 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
diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c
index c20d1f56bf..80200d9c58 100644
--- a/vtysh/vtysh.c
+++ b/vtysh/vtysh.c
@@ -776,7 +776,7 @@ vtysh_rl_describe (void)
rl_on_new_line ();
return 0;
break;
- }
+ }
/* Get width of command string. */
width = 0;
@@ -813,15 +813,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);
}
}