From 6f11bf1dd634e39a2f17fc63f156575f3b10358c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 6 Jun 2024 08:41:00 +0200 Subject: create word completion list function --- client/cmd.cpp | 15 +-------------- client/rl.cpp | 23 +++++++++++++++++++++++ client/rl.h | 1 + 3 files changed, 25 insertions(+), 14 deletions(-) (limited to 'client') diff --git a/client/cmd.cpp b/client/cmd.cpp index e7d42d0..10d53e3 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -101,19 +101,6 @@ void cmd_dump(char * mode) { } char** cmd_dump_complete(const char * text, int begin, int end) { int word = rl_word(rl_line_buffer, begin); - if (word != 1) return NULL; - - return rl_completion_matches(text, [](const char * text, int state) -> char * { - static size_t i = 0; - if (state == 0) i = 0; - - while (dump_modes[i] != NULL) { - const char * mode = dump_modes[i++]; - if (strncmp(text, mode, strlen(text)) == 0) - return strdup(mode); - } - return NULL; - }); - + if (word == 1) return rl_complete_list(text, dump_modes); return NULL; } diff --git a/client/rl.cpp b/client/rl.cpp index b8113aa..fa44bf4 100644 --- a/client/rl.cpp +++ b/client/rl.cpp @@ -119,3 +119,26 @@ int rl_word(const char * line, int cursor) { return word; } +typedef struct { + const char * word; + const char ** suggestions; +} __rl_complete_list_data_t; +char** rl_complete_list(const char * word, const char ** suggestions) { + __rl_complete_list_data_t data = { + .word = word, + .suggestions = suggestions, + }; + return rl_completion_matches((char *) &data, [](const char * text, int state) -> char * { + __rl_complete_list_data_t data = *(__rl_complete_list_data_t *) text; + static size_t i = 0; + if (state == 0) i = 0; + + while (data.suggestions[i] != NULL) { + const char * suggestion = data.suggestions[i++]; + if (strncmp(data.word, suggestion, strlen(data.word)) == 0) + return strdup(suggestion); + } + return NULL; + }); +} + diff --git a/client/rl.h b/client/rl.h index c3bf2c7..ab31ddb 100644 --- a/client/rl.h +++ b/client/rl.h @@ -8,4 +8,5 @@ int cli_main(); void rl_printf(const char * fmt, ...); int rl_word(const char * line, int cursor); +char ** rl_complete_list(const char * word, const char * suggestions[]); -- cgit v1.2.3