From 9cdc8540fa3f7d36d48b44f9e955652b6e17dd91 Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Fri, 17 Oct 2025 22:55:48 -0400 Subject: item filtering --- README.md | 2 ++ graphics.c | 5 +++-- input_field.c | 26 ++++++++++++++++++++++---- input_field.h | 1 + main.c | 2 ++ state.h | 1 + 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0894a7a..f21a1da 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,7 @@ To build: `make` ## Todo - - [ ] "Selection" - [ ] Output results and exit code +- [ ] Cursor - [ ] Scrolling - [ ] Unicode +- [ ] Kerning diff --git a/graphics.c b/graphics.c index 05af57f..3032d68 100644 --- a/graphics.c +++ b/graphics.c @@ -158,7 +158,7 @@ void render_frame(client_state *state) { glUniform2f(state->offset_uniform, pen_x, pen_y); glDrawElements(GL_TRIANGLES, state->input_buffer_grafix.num_elements, GL_UNSIGNED_INT, 0); - // set up drawing for options + // set up starting pen and scissor for options if (state->lines > 0) { glScissor(0, 0, state->width, pen_y); pen_y -= state->line_height; @@ -170,7 +170,8 @@ void render_frame(client_state *state) { } // draw options - array_for_all(item_t, item, state->items) { + array_for_all(uint32_t, i, state->filtered_items) { + item_t* item = &state->items[*i]; glBindVertexArray(item->text_buffer.vao); glUniform2f(state->offset_uniform, pen_x, pen_y); glDrawElements(GL_TRIANGLES, item->text_buffer.num_elements, GL_UNSIGNED_INT, 0); diff --git a/input_field.c b/input_field.c index 14c34f7..75a0cbc 100644 --- a/input_field.c +++ b/input_field.c @@ -4,7 +4,25 @@ #include "array.h" #include "graphics.h" -void regenerate_text_buffer(client_state* state) { +void filter_items(client_state* state) { + array_clear(state->filtered_items); + + // get null terminated input buffer + size_t len = array_size(state->input_buffer); + char input_buffer_string[len + 1]; + memcpy(input_buffer_string, state->input_buffer, len); + input_buffer_string[len] = '\0'; + + // add all strings with substring + for (uint32_t i = 0; i < array_size(state->items); ++i) { + if (strstr(state->items[i].text, input_buffer_string) != NULL) { + array_add(state->filtered_items, i); + } + } +} + +void update_text_buffer(client_state* state) { + filter_items(state); destroy_text_buffer(&state->input_buffer_grafix); init_text_buffer(state, &state->input_buffer_grafix, state->input_buffer, array_size(state->input_buffer)); } @@ -24,7 +42,7 @@ bool type_key(client_state* state, xkb_keysym_t keysym) { // submit line if (keysym == XKB_KEY_Return || keysym == XKB_KEY_KP_Enter) { submit_line(state); - regenerate_text_buffer(state); + update_text_buffer(state); return false; } @@ -35,7 +53,7 @@ bool type_key(client_state* state, xkb_keysym_t keysym) { } else { array_pop(state->input_buffer); } - regenerate_text_buffer(state); + update_text_buffer(state); return true; } @@ -46,7 +64,7 @@ bool type_key(client_state* state, xkb_keysym_t keysym) { for (int i = 0; i < strlen(buf); i++) { array_add(state->input_buffer, buf[i]); } - regenerate_text_buffer(state); + update_text_buffer(state); return true; } diff --git a/input_field.h b/input_field.h index f3379d4..ab583e5 100644 --- a/input_field.h +++ b/input_field.h @@ -5,6 +5,7 @@ // returns true if the key should repeat bool type_key(client_state* state, xkb_keysym_t keysym); +void filter_items(client_state* state); void submit_line(client_state* state); #endif diff --git a/main.c b/main.c index 0d079a7..a85a74a 100644 --- a/main.c +++ b/main.c @@ -35,10 +35,12 @@ int main(int argc, char* argv[]) { client_state state = {0}; state.running = true; state.input_buffer = array_new(char, 0); + state.filtered_items = array_new(uint32_t, 0); // read input parse_args(&state, argc, argv); read_stdin(&state); + filter_items(&state); // find font char* font = get_font("Monospace"); diff --git a/state.h b/state.h index cada269..8371000 100644 --- a/state.h +++ b/state.h @@ -98,6 +98,7 @@ typedef struct { char* input_buffer; uint32_t width, height; item_t* items; + uint32_t* filtered_items; bool running; } client_state; -- cgit v1.2.3