From f3ed94b74d280ab9d8e839f1b435ae78f6c0f8cf Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Thu, 2 Jan 2025 10:30:50 +0100 Subject: [PATCH] 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 881fa114ca..8dcfff0f6e 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