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/rl.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'client/rl.cpp') 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; + }); +} + -- cgit v1.2.3