diff options
author | Michael Vetter <jubalh@iodoru.org> | 2021-03-11 22:08:04 +0100 |
---|---|---|
committer | Michael Vetter <jubalh@iodoru.org> | 2021-03-11 22:08:04 +0100 |
commit | c1ccaee58fe576e4e8f232f7e20ebb912ffb2a1a (patch) | |
tree | dbee648d63f33dad1e8ec92afbbd5397b9c5882a | |
parent | 7e6b9f4e0a1639a640cc1355ef3dd0dc2c1c7cd8 (diff) | |
download | profani-tty-c1ccaee58fe576e4e8f232f7e20ebb912ffb2a1a.tar.gz |
Remove duplicate code in autocomplete_param*
autocomplete_param_with_func and -autocomplete_param_with_ac had lots of duplicate code.
-rw-r--r-- | src/tools/autocomplete.c | 51 |
1 files changed, 22 insertions, 29 deletions
diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index bd766e52..67cc6cb0 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -298,24 +298,34 @@ autocomplete_complete(Autocomplete ac, const gchar* search_str, gboolean quote, } } -char* -autocomplete_param_with_func(const char* const input, char* command, autocomplete_func func, gboolean previous, void* context) +// autocomplete_func func is used -> autocomplete_param_with_func +// Autocomplete ac, gboolean quote are used -> autocomplete_param_with_ac +static char* +_autocomplete_param_common(const char* const input, char* command, autocomplete_func func, Autocomplete ac, gboolean quote, gboolean previous, void* context) { GString* auto_msg = NULL; char* result = NULL; - char command_cpy[strlen(command) + 2]; + char* command_cpy = malloc(strlen(command) + 2); sprintf(command_cpy, "%s ", command); int len = strlen(command_cpy); if (strncmp(input, command_cpy, len) == 0) { int inp_len = strlen(input); char prefix[inp_len]; + char *found; + for (int i = len; i < inp_len; i++) { prefix[i - len] = input[i]; } + prefix[inp_len - len] = '\0'; - char* found = func(prefix, previous, context); + if (func) { + found = func(prefix, previous, context); + } else { + found = autocomplete_complete(ac, prefix, quote, previous); + } + if (found) { auto_msg = g_string_new(command_cpy); g_string_append(auto_msg, found); @@ -324,38 +334,21 @@ autocomplete_param_with_func(const char* const input, char* command, autocomplet g_string_free(auto_msg, FALSE); } } + free(command_cpy); return result; } char* -autocomplete_param_with_ac(const char* const input, char* command, Autocomplete ac, gboolean quote, gboolean previous) +autocomplete_param_with_func(const char* const input, char* command, autocomplete_func func, gboolean previous, void* context) { - GString* auto_msg = NULL; - char* result = NULL; - char* command_cpy = malloc(strlen(command) + 2); - sprintf(command_cpy, "%s ", command); - int len = strlen(command_cpy); - int inp_len = strlen(input); - if (strncmp(input, command_cpy, len) == 0) { - char prefix[inp_len]; - for (int i = len; i < inp_len; i++) { - prefix[i - len] = input[i]; - } - prefix[inp_len - len] = '\0'; - - char* found = autocomplete_complete(ac, prefix, quote, previous); - if (found) { - auto_msg = g_string_new(command_cpy); - g_string_append(auto_msg, found); - free(found); - result = auto_msg->str; - g_string_free(auto_msg, FALSE); - } - } - free(command_cpy); + return _autocomplete_param_common(input, command, func, NULL, FALSE, previous, context); +} - return result; +char* +autocomplete_param_with_ac(const char* const input, char* command, Autocomplete ac, gboolean quote, gboolean previous) +{ + return _autocomplete_param_common(input, command, NULL, ac, quote, previous, NULL); } char* |