console.lua: highlight the select menu's preselected item

Requested in
https://github.com/mpv-player/mpv/pull/14087#issuecomment-2110889436 and
https://github.com/mpv-player/mpv/pull/15031#issuecomment-2401328081

This is mainly useful to keep highlighting the current playlist entry
after moving the mouse.
This commit is contained in:
Guido Cella 2024-10-09 21:35:03 +02:00 committed by Kacper Michajłow
parent ad0c29e5ac
commit 3e63c97677
1 changed files with 19 additions and 2 deletions

View File

@ -64,6 +64,7 @@ local styles = {
fatal = '{\\1c&H5791f9&}', fatal = '{\\1c&H5791f9&}',
suggestion = '{\\1c&Hcc99cc&}', suggestion = '{\\1c&Hcc99cc&}',
selected_suggestion = '{\\1c&H2fbdfa&\\b1}', selected_suggestion = '{\\1c&H2fbdfa&\\b1}',
default_item = '{\\1c&H2fbdfa&}',
disabled = '{\\1c&Hcccccc&}', disabled = '{\\1c&Hcccccc&}',
} }
@ -74,6 +75,7 @@ local terminal_styles = {
error = '\027[31m', error = '\027[31m',
fatal = '\027[91m', fatal = '\027[91m',
selected_suggestion = '\027[7m', selected_suggestion = '\027[7m',
default_item = '\027[1m',
disabled = '\027[38;5;8m', disabled = '\027[38;5;8m',
} }
@ -109,6 +111,7 @@ local selectable_items
local matches = {} local matches = {}
local selected_match = 1 local selected_match = 1
local first_match_to_print = 1 local first_match_to_print = 1
local default_item
local set_active local set_active
@ -417,10 +420,22 @@ local function populate_log_with_matches()
end end
for i = first_match_to_print, last_match_to_print do for i = first_match_to_print, last_match_to_print do
local style = ''
local terminal_style = ''
if i == selected_match then
style = styles.selected_suggestion
terminal_style = terminal_styles.selected_suggestion
end
if matches[i].index == default_item then
style = style .. styles.default_item
terminal_style = terminal_style .. terminal_styles.default_item
end
log[#log + 1] = { log[#log + 1] = {
text = matches[i].text, text = matches[i].text,
style = i == selected_match and styles.selected_suggestion or '', style = style,
terminal_style = i == selected_match and terminal_styles.selected_suggestion or '', terminal_style = terminal_style,
} }
end end
@ -1697,6 +1712,7 @@ set_active = function (active)
line = '' line = ''
cursor = 1 cursor = 1
selectable_items = nil selectable_items = nil
default_item = nil
dont_bind_up_down = false dont_bind_up_down = false
unbind_mouse() unbind_mouse()
end end
@ -1770,6 +1786,7 @@ mp.register_script_message('get-input', function (script_name, args)
if selectable_items then if selectable_items then
matches = {} matches = {}
selected_match = args.default_item or 1 selected_match = args.default_item or 1
default_item = args.default_item
first_match_to_print = 1 first_match_to_print = 1
for i, item in ipairs(selectable_items) do for i, item in ipairs(selectable_items) do
matches[i] = { index = i, text = item } matches[i] = { index = i, text = item }