From 7c31982244f179cb2f51bb1d81b21ad4efd649ba Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Fri, 10 Jun 2022 19:36:29 +0100 Subject: Various small changes. - Split the compgen and history sorting parts of compgen(), for future dmenu-like work. - Add a separate tofi-compgen executable. - Remove harfbuzz-glib usage, as we shouldn't be doing any complicated unicode stuff. --- src/compgen.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/compgen.c') diff --git a/src/compgen.c b/src/compgen.c index 3ab228a..b0ea44d 100644 --- a/src/compgen.c +++ b/src/compgen.c @@ -9,7 +9,7 @@ #include "string_vec.h" #include "xmalloc.h" -struct string_vec compgen(struct history *history) +struct string_vec compgen() { log_debug("Retrieving PATH.\n"); const char *env_path = getenv("PATH"); @@ -54,6 +54,11 @@ struct string_vec compgen(struct history *history) log_debug("Making unique.\n"); string_vec_uniq(&programs); + return programs; +} + +void compgen_history_sort(struct string_vec *programs, struct history *history) +{ log_debug("Moving already known programs to the front.\n"); /* * Remove any programs in our history from the generated list, and @@ -63,7 +68,7 @@ struct string_vec compgen(struct history *history) */ struct string_vec to_add = string_vec_create(); for (size_t i = 0; i < history->count; i++) { - char **res = string_vec_find(&programs, history->buf[i].name); + char **res = string_vec_find(programs, history->buf[i].name); if (res == NULL) { continue; } @@ -73,7 +78,7 @@ struct string_vec compgen(struct history *history) } /* Sort the vector to push the removed entries to the end. */ - string_vec_sort(&programs); + string_vec_sort(programs); /* * Move the results down by the number of items we want to add. There's @@ -81,14 +86,13 @@ struct string_vec compgen(struct history *history) * many items. */ memmove( - &programs.buf[to_add.count], - programs.buf, - (programs.count - to_add.count) * sizeof(programs.buf[0])); + &programs->buf[to_add.count], + programs->buf, + (programs->count - to_add.count) * sizeof(programs->buf[0])); /* Add our history to the front in order. */ for (size_t i = 0; i < to_add.count; i++) { - programs.buf[i] = xstrdup(to_add.buf[i]); + programs->buf[i] = xstrdup(to_add.buf[i]); } string_vec_destroy(&to_add); - return programs; } -- cgit v1.2.3