esc is slow

This commit is contained in:
Your Name 2025-04-22 20:11:09 +12:00
parent 6a381b539c
commit b72c959a54

View File

@ -139,24 +139,44 @@ std::string fullscreen_window::set_input_multiple_choice(std::string prompt, std
int selected = 0; int selected = 0;
std::string filter = ""; std::string filter = "";
std::string last_filter = "";
auto last_key_time = std::chrono::steady_clock::now(); auto last_key_time = std::chrono::steady_clock::now();
int scroll_offset = 0; int scroll_offset = 0;
std::vector<std::string> last_filtered_choices;
int last_selected = -1; int last_selected = -1;
int last_scroll_offset = -1; int last_scroll_offset = -1;
while (true) { // Pre-compute lowercase versions of choices for case-insensitive matching
// Filter choices based on input std::vector<std::string> lowercase_choices;
std::vector<std::string> filtered_choices; lowercase_choices.reserve(choices.size());
for (const auto& choice : choices) { for (const auto& choice : choices) {
if (filter.empty() || choice.find(filter) == 0) { std::string lower;
filtered_choices.push_back(choice); lower.reserve(choice.size());
} std::transform(choice.begin(), choice.end(), std::back_inserter(lower), ::tolower);
} lowercase_choices.push_back(std::move(lower));
}
if (filtered_choices.empty()) { // Initial filtered choices (all choices)
filtered_choices = choices; std::vector<std::string> filtered_choices = choices;
filter = "";
while (true) {
// 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]);
}
}
}
} }
// Get window dimensions // Get window dimensions
@ -179,11 +199,8 @@ std::string fullscreen_window::set_input_multiple_choice(std::string prompt, std
} }
} }
// Only redraw if something changed // Only redraw if selection or scroll changed
if (filtered_choices != last_filtered_choices || if (selected != last_selected || scroll_offset != last_scroll_offset || last_filter != filter) {
selected != last_selected ||
scroll_offset != last_scroll_offset) {
// Clear the input window except for the prompt // Clear the input window except for the prompt
werase(input_win); werase(input_win);
box(input_win, 0, 0); box(input_win, 0, 0);
@ -218,9 +235,9 @@ std::string fullscreen_window::set_input_multiple_choice(std::string prompt, std
wrefresh(input_win); wrefresh(input_win);
// Update last state // Update last state
last_filtered_choices = filtered_choices;
last_selected = selected; last_selected = selected;
last_scroll_offset = scroll_offset; last_scroll_offset = scroll_offset;
last_filter = filter;
} }
int ch = wgetch(input_win); int ch = wgetch(input_win);