aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-06 08:41:00 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-06 08:41:00 +0200
commit6f11bf1dd634e39a2f17fc63f156575f3b10358c (patch)
treea61be807e29b8d2a699634da8f44c515f5d4cda2
parent561971b194dad6d9d7f607eb75ae33e7b42f009e (diff)
create word completion list function
-rw-r--r--client/cmd.cpp15
-rw-r--r--client/rl.cpp23
-rw-r--r--client/rl.h1
3 files changed, 25 insertions, 14 deletions
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[]);