From 492ad79f0137f083f92fc0e8e871e924788d4331 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Thu, 2 Jan 2025 10:30:50 +0100 Subject: [PATCH 1/2] console.lua: focus the default item after emptying the input line When console is opened with selectable items and a default item, the default item is placed at the center. But if you type something and then clear the line, the first item becomes the selected one. Make it select the default item and place it in the center in this case too. --- player/lua/console.lua | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/player/lua/console.lua b/player/lua/console.lua index 881fa114ca35c..8dcfff0f6eb85 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -689,12 +689,22 @@ end local function handle_edit() if selectable_items then matches = {} - selected_match = 1 - for i, match in ipairs(fuzzy_find(line, selectable_items)) do matches[i] = { index = match, text = selectable_items[match] } end + if line == '' and default_item then + selected_match = default_item + + local max_lines = calculate_max_log_lines() + first_match_to_print = math.max(1, selected_match - math.floor(max_lines / 2) + 1) + if first_match_to_print > #selectable_items - max_lines + 2 then + first_match_to_print = math.max(1, #selectable_items - max_lines + 1) + end + else + selected_match = 1 + end + update() return @@ -1820,16 +1830,8 @@ mp.register_script_message('get-input', function (script_name, args) for i, item in ipairs(args.items) do selectable_items[i] = item:gsub("[\r\n].*", "⋯"):sub(1, 300) end - handle_edit() - selected_match = args.default_item or 1 default_item = args.default_item - - local max_lines = calculate_max_log_lines() - first_match_to_print = math.max(1, selected_match - math.floor(max_lines / 2) + 1) - if first_match_to_print > #selectable_items - max_lines + 2 then - first_match_to_print = math.max(1, #selectable_items - max_lines + 1) - end - + handle_edit() bind_mouse() end From 1eadc6c37ed80f2c11afbda462d0e68b670bf87f Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Thu, 2 Jan 2025 11:03:53 +0100 Subject: [PATCH 2/2] console.lua: remove useless assignment The lines in populate_log_with_matches() if selected_match < first_match_to_print then first_match_to_print = selected_match will set first_match_to_print to 1 even if it is not set here since handle_edit() sets selected_match = 1. --- player/lua/console.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/player/lua/console.lua b/player/lua/console.lua index 8dcfff0f6eb85..b69ea23e179e2 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -1033,7 +1033,6 @@ local function search_history() searching_history = true suggestion_buffer = {} selectable_items = {} - first_match_to_print = 1 for i = 1, #history do selectable_items[i] = history[#history + 1 - i]