From 8a4984e0e15fcfb0be6db242ab3f60325b80abd8 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 18 Jan 2021 20:54:07 -0500 Subject: bug fixes, narrow file list based on search term --- buffer.c | 9 +++++++++ command.c | 6 ++---- main.c | 2 +- menu.c | 17 +++++++++++++++++ string32.c | 15 +++++++-------- ted.h | 6 +++--- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/buffer.c b/buffer.c index 235a59f..477a1c9 100644 --- a/buffer.c +++ b/buffer.c @@ -137,6 +137,15 @@ static inline Settings *buffer_settings(TextBuffer *buffer) { return &buffer->ted->settings; } +// NOTE: this string will be invalidated when the line is edited!!! +// only use it briefly!! +static String32 buffer_get_line(TextBuffer *buffer, u32 line_number) { + Line *line = &buffer->lines[line_number]; + return (String32) { + .str = line->str, .len = line->len + }; +} + // Returns a simple checksum of the buffer. // This is only used for testing, and shouldn't be relied on. static u64 buffer_checksum(TextBuffer *buffer) { diff --git a/command.c b/command.c index 684d30a..9bd58bc 100644 --- a/command.c +++ b/command.c @@ -157,14 +157,12 @@ void command_execute(Ted *ted, Command c, i64 argument) { break; case MENU_OPEN: { TextBuffer *open_to = &ted->main_buffer; - String32 filename32 = {.str = buffer->lines[0].str, .len = buffer->lines[0].len}; - - char *filename_cstr = str32_to_utf8_cstr(filename32); + char *filename_cstr = str32_to_utf8_cstr(buffer_get_line(&ted->line_buffer, 0)); if (filename_cstr) { buffer_load_file(open_to, filename_cstr); buffer = open_to; if (buffer_haserr(open_to)) { - // @TODO: something better + // @TODO: something } free(filename_cstr); menu_close(ted, true); diff --git a/main.c b/main.c index 3a42e87..3c05744 100644 --- a/main.c +++ b/main.c @@ -321,7 +321,7 @@ int main(int argc, char **argv) { case SDL_TEXTINPUT: { char *text = event.text.text; if (buffer - && key_modifier == 0) // unfortunately, some key combinations like ctrl+minus still register as a "-" text input event + && (key_modifier & ~KEY_MODIFIER_SHIFT) == 0) // unfortunately, some key combinations like ctrl+minus still register as a "-" text input event buffer_insert_utf8_at_cursor(buffer, text); } break; } diff --git a/menu.c b/menu.c index 9ef2245..7c09d53 100644 --- a/menu.c +++ b/menu.c @@ -31,8 +31,10 @@ static void menu_render(Ted *ted, Menu menu) { gl_color_rgba(colors[COLOR_MENU_BACKDROP]); rect_render(rect(V2(0, 0), V2(window_width, window_height))); glEnd(); + if (menu == MENU_OPEN) { + char *search_term = str32_to_utf8_cstr(buffer_get_line(&ted->line_buffer, 0)); char const *directory = "."; float padding = 20; float menu_x1 = window_width * 0.5f - 300; @@ -62,8 +64,21 @@ static void menu_render(Ted *ted, Menu menu) { char **files = fs_list_directory(directory); if (files) { + u32 nfiles = 0; for (char **p = files; *p; ++p) ++nfiles; + + if (search_term && *search_term) { + // filter files based on search term + u32 in, out = 0; + for (in = 0; in < nfiles; ++in) { + if (stristr(files[in], search_term)) { + files[out++] = files[in]; + } + } + nfiles = out; + } + qsort(files, nfiles, sizeof *files, str_qsort_case_insensitive_cmp); { // render file names @@ -79,5 +94,7 @@ static void menu_render(Ted *ted, Menu menu) { for (u32 i = 0; i < nfiles; ++i) free(files[i]); free(files); } + free(search_term); } + } diff --git a/string32.c b/string32.c index 35aa1a2..4d033c0 100644 --- a/string32.c +++ b/string32.c @@ -91,18 +91,17 @@ size_t str32_count_char(String32 s, char32_t c) { // returns number of characters deleted from s size_t str32_remove_all_instances_of_char(String32 *s, char32_t c) { - bool increment = true; char32_t *str = s->str; size_t ndeleted = 0; - for (size_t i = 0; i < s->len; i += increment, increment = true) { - if (str[i] == c) { - --s->len; - if (i < s->len) { - str[i] = str[i+1]; - } + size_t len = s->len; + size_t out = 0; + for (size_t in = 0; in < len; ++in) { + if (str[in] == c) { ++ndeleted; - increment = false; + } else { + str[out++] = str[in]; } } + s->len = out; return ndeleted; } diff --git a/ted.h b/ted.h index b0e325b..36602cf 100644 --- a/ted.h +++ b/ted.h @@ -19,9 +19,9 @@ typedef struct { #define KEY_MODIFIER_CTRL_BIT 0 #define KEY_MODIFIER_SHIFT_BIT 1 #define KEY_MODIFIER_ALT_BIT 2 -#define KEY_MODIFIER_CTRL (1<