From b72c959a5466bf7272c934c1b69162878526ff86 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 22 Apr 2025 20:11:09 +1200 Subject: [PATCH] esc is slow --- src/interactive/interactive.cpp | 51 ++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/interactive/interactive.cpp b/src/interactive/interactive.cpp index 944349e..82bd173 100644 --- a/src/interactive/interactive.cpp +++ b/src/interactive/interactive.cpp @@ -139,26 +139,46 @@ std::string fullscreen_window::set_input_multiple_choice(std::string prompt, std int selected = 0; std::string filter = ""; + std::string last_filter = ""; auto last_key_time = std::chrono::steady_clock::now(); int scroll_offset = 0; - std::vector last_filtered_choices; int last_selected = -1; int last_scroll_offset = -1; + // Pre-compute lowercase versions of choices for case-insensitive matching + std::vector lowercase_choices; + lowercase_choices.reserve(choices.size()); + for (const auto& choice : choices) { + std::string lower; + lower.reserve(choice.size()); + std::transform(choice.begin(), choice.end(), std::back_inserter(lower), ::tolower); + lowercase_choices.push_back(std::move(lower)); + } + + // Initial filtered choices (all choices) + std::vector filtered_choices = choices; + while (true) { - // Filter choices based on input - std::vector filtered_choices; - for (const auto& choice : choices) { - if (filter.empty() || choice.find(filter) == 0) { - filtered_choices.push_back(choice); + // Only recalculate filtered choices if filter changed + if (filter != last_filter) { + if (filter.empty()) { + filtered_choices = choices; + } else { + filtered_choices.clear(); + // Convert filter to lowercase once + std::string lowercase_filter; + lowercase_filter.reserve(filter.size()); + std::transform(filter.begin(), filter.end(), std::back_inserter(lowercase_filter), ::tolower); + + // Filter using pre-computed lowercase versions + for (size_t i = 0; i < choices.size(); ++i) { + if (lowercase_choices[i].find(lowercase_filter) == 0) { + filtered_choices.push_back(choices[i]); + } + } } } - if (filtered_choices.empty()) { - filtered_choices = choices; - filter = ""; - } - // Get window dimensions int max_y, max_x; getmaxyx(input_win, max_y, max_x); @@ -179,11 +199,8 @@ std::string fullscreen_window::set_input_multiple_choice(std::string prompt, std } } - // Only redraw if something changed - if (filtered_choices != last_filtered_choices || - selected != last_selected || - scroll_offset != last_scroll_offset) { - + // Only redraw if selection or scroll changed + if (selected != last_selected || scroll_offset != last_scroll_offset || last_filter != filter) { // Clear the input window except for the prompt werase(input_win); box(input_win, 0, 0); @@ -218,9 +235,9 @@ std::string fullscreen_window::set_input_multiple_choice(std::string prompt, std wrefresh(input_win); // Update last state - last_filtered_choices = filtered_choices; last_selected = selected; last_scroll_offset = scroll_offset; + last_filter = filter; } int ch = wgetch(input_win);