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 ++++++++++++-------- src/compgen.h | 3 ++- src/entry.c | 1 - src/entry_backend/harfbuzz.c | 2 -- src/entry_backend/pango.c | 9 +++------ src/main.c | 3 ++- src/main_compgen.c | 12 ++++++++++++ 7 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 src/main_compgen.c (limited to 'src') 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; } diff --git a/src/compgen.h b/src/compgen.h index 502bcb5..c79f548 100644 --- a/src/compgen.h +++ b/src/compgen.h @@ -4,6 +4,7 @@ #include "history.h" #include "string_vec.h" -struct string_vec compgen(struct history *history); +struct string_vec compgen(void); +void compgen_history_sort(struct string_vec *programs, struct history *history); #endif /* COMPGEN_H */ diff --git a/src/entry.c b/src/entry.c index 453f3c5..252e3e1 100644 --- a/src/entry.c +++ b/src/entry.c @@ -1,5 +1,4 @@ #include -#include #include #include "entry.h" #include "log.h" diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c index 78d0757..89ed13b 100644 --- a/src/entry_backend/harfbuzz.c +++ b/src/entry_backend/harfbuzz.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include "harfbuzz.h" @@ -162,7 +161,6 @@ void entry_backend_init( log_debug("Creating Harfbuzz buffer.\n"); hb_buffer_t *buffer = hb_buffer_create(); entry->backend.hb_buffer = buffer; - hb_buffer_set_unicode_funcs(buffer, hb_glib_get_unicode_funcs()); setup_hb_buffer(buffer); /* Draw the prompt now, as this only needs to be done once */ diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c index 787dfd8..6aafaef 100644 --- a/src/entry_backend/pango.c +++ b/src/entry_backend/pango.c @@ -27,10 +27,6 @@ void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height, entry->backend.layout = pango_layout_new(context); log_debug("Setting Pango text.\n"); pango_layout_set_text(entry->backend.layout, "run: ", -1); - int prompt_width; - int prompt_height; - log_debug("Get Pango pixel size.\n"); - pango_layout_get_pixel_size(entry->backend.layout, &prompt_width, &prompt_height); log_debug("First Pango draw.\n"); pango_cairo_update_layout(cr, entry->backend.layout); @@ -40,6 +36,8 @@ void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height, pango_cairo_show_layout(cr, entry->backend.layout); /* Move and clip so we don't draw over the prompt */ + int prompt_width; + pango_layout_get_pixel_size(entry->backend.layout, &prompt_width, NULL); cairo_translate(cr, prompt_width, 0); *width -= prompt_width; cairo_rectangle(cr, 0, 0, *width, *height); @@ -63,9 +61,8 @@ void entry_backend_update(struct entry *entry) pango_cairo_update_layout(cr, layout); pango_cairo_show_layout(cr, layout); - int width; int height; - pango_layout_get_size(entry->backend.layout, &width, &height); + pango_layout_get_size(layout, NULL, &height); for (size_t i = 0; i < 5; i++) { cairo_translate(cr, 0, (int)(height / PANGO_SCALE)); diff --git a/src/main.c b/src/main.c index 63d8252..806ad1c 100644 --- a/src/main.c +++ b/src/main.c @@ -584,7 +584,8 @@ int main(int argc, char *argv[]) log_debug("Generating command list.\n"); log_indent(); tofi.window.entry.history = history_load(); - tofi.window.entry.commands = compgen(&tofi.window.entry.history); + tofi.window.entry.commands = compgen(); + compgen_history_sort(&tofi.window.entry.commands, &tofi.window.entry.history); tofi.window.entry.results = string_vec_copy(&tofi.window.entry.commands); log_unindent(); log_debug("Command list generated.\n"); diff --git a/src/main_compgen.c b/src/main_compgen.c new file mode 100644 index 0000000..a62bc11 --- /dev/null +++ b/src/main_compgen.c @@ -0,0 +1,12 @@ +#include +#include "compgen.h" +#include "string_vec.h" + +int main() +{ + struct string_vec commands = compgen(); + for (size_t i = 0; i < commands.count; i++) { + printf("%s\n", commands.buf[i]); + } + string_vec_destroy(&commands); +} -- cgit v1.2.3