Merge pull request #808 from qlyoung/vtysh-termcols

lib, vtysh: pretty-print variable autocompletions
This commit is contained in:
Russ White 2017-07-14 07:10:13 -04:00 committed by GitHub
commit 79af1cb338
4 changed files with 49 additions and 18 deletions

View File

@ -715,6 +715,41 @@ cmd_variable_complete (struct cmd_token *token, const char *arg, vector comps)
vector_free(tmpcomps); 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 void
cmd_variable_handler_register (const struct cmd_variable_handler *cvh) cmd_variable_handler_register (const struct cmd_variable_handler *cvh)
{ {

View File

@ -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_complete (struct cmd_token *token, const char *arg, vector comps);
extern void cmd_variable_handler_register (const struct cmd_variable_handler *cvh); 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 */ #endif /* _ZEBRA_COMMAND_H */

View File

@ -1134,17 +1134,13 @@ vty_describe_command (struct vty *vty)
vector varcomps = vector_init (VECTOR_MIN_SIZE); vector varcomps = vector_init (VECTOR_MIN_SIZE);
cmd_variable_complete (token, ref, varcomps); cmd_variable_complete (token, ref, varcomps);
if (vector_active(varcomps) > 0) if (vector_active (varcomps) > 0)
{ {
vty_out(vty, " "); char *ac = cmd_variable_comp2str(varcomps, vty->width, VTYNL);
for (size_t j = 0; j < vector_active (varcomps); j++) vty_outln(vty, "%s", ac);
{ XFREE(MTYPE_TMP, ac);
char *item = vector_slot (varcomps, j);
vty_out(vty, " %s", item);
XFREE(MTYPE_COMPLETION, item);
}
vty_out (vty, VTYNL);
} }
vector_free(varcomps); vector_free(varcomps);
} }
#if 0 #if 0

View File

@ -776,7 +776,7 @@ vtysh_rl_describe (void)
rl_on_new_line (); rl_on_new_line ();
return 0; return 0;
break; break;
} }
/* Get width of command string. */ /* Get width of command string. */
width = 0; width = 0;
@ -813,15 +813,14 @@ vtysh_rl_describe (void)
if (vector_active (varcomps) > 0) if (vector_active (varcomps) > 0)
{ {
fprintf(stdout, " "); int rows, cols;
for (size_t j = 0; j < vector_active (varcomps); j++) rl_get_screen_size(&rows, &cols);
{
char *item = vector_slot (varcomps, j); char *ac = cmd_variable_comp2str(varcomps, cols, "\n");
fprintf (stdout, " %s", item); fprintf(stdout, "%s\n", ac);
XFREE (MTYPE_COMPLETION, item); XFREE(MTYPE_TMP, ac);
}
vty_out (vty, VTYNL);
} }
vector_free (varcomps); vector_free (varcomps);
} }
} }