From 6011c1b213fa8e08126e33852403845a39881072 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Thu, 6 Oct 2016 01:15:48 +0000 Subject: [PATCH] lib: Make appear first in completions When a command is complete and appears in tab- or ?-completions, make sure it appears first Signed-off-by: Quentin Young --- lib/command.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/command.c b/lib/command.c index 6368ebffe1..f51b2c69ee 100644 --- a/lib/command.c +++ b/lib/command.c @@ -498,7 +498,10 @@ compare_completions (const void *fst, const void *snd) /** * Takes a list of completions returned by command_complete, * dedeuplicates them based on both text and description, - * and returns them as a vector. + * sorts them, and returns them as a vector. + * + * @param completions linked list of cmd_token + * @return deduplicated and sorted vector with */ static vector completions_to_vec (struct list *completions) @@ -506,10 +509,13 @@ completions_to_vec (struct list *completions) vector comps = vector_init (VECTOR_MIN_SIZE); struct listnode *ln; - struct cmd_token *token; + struct cmd_token *token, *cr = NULL; unsigned int i, exists; for (ALL_LIST_ELEMENTS_RO(completions,ln,token)) { + if (token->type == END_TKN && (cr = token)) + continue; + // linear search for token in completions vector exists = 0; for (i = 0; i < vector_active (comps) && !exists; i++) @@ -529,6 +535,13 @@ completions_to_vec (struct list *completions) sizeof (void *), &compare_completions); + if (cr) + { + vector_set_index (comps, vector_active (comps), NULL); + memmove (comps->index + 1, comps->index, (comps->alloced - 1) * sizeof (void *)); + vector_set_index (comps, 0, copy_cmd_token (cr)); + } + return comps; } /** -- 2.39.5