Skip to content

Commit

Permalink
console.lua: highlight the select menu's preselected item
Browse files Browse the repository at this point in the history
Requested in
mpv-player#14087 (comment) and
mpv-player#15031 (comment)

This is mainly useful to keep highlighting the current playlist entry
after moving the mouse.
  • Loading branch information
guidocella committed Oct 9, 2024
1 parent d92c518 commit 81c5635
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ local terminal_styles = {
error = '\027[31m',
fatal = '\027[91m',
selected_suggestion = '\027[7m',
default_item = '\027[1m',
disabled = '\027[38;5;8m',
}

Expand Down Expand Up @@ -109,6 +110,7 @@ local selectable_items
local matches = {}
local selected_match = 1
local first_match_to_print = 1
local default_item

local set_active

Expand Down Expand Up @@ -453,11 +455,23 @@ local function populate_log_with_matches(max_width)
end

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 i == default_item then
style = style .. styles.warn
terminal_style = terminal_style .. terminal_styles.default_item
end

log[#log + 1] = {
text = (max_width and truncate_utf8(matches[i].text, max_width)
or matches[i].text),
style = i == selected_match and styles.selected_suggestion or '',
terminal_style = i == selected_match and terminal_styles.selected_suggestion or '',
style = style,
terminal_style = terminal_style,
}
end

Expand Down Expand Up @@ -1732,6 +1746,7 @@ set_active = function (active)
line = ''
cursor = 1
selectable_items = nil
default_item = nil
dont_bind_up_down = false
unbind_mouse()
end
Expand Down Expand Up @@ -1805,6 +1820,7 @@ mp.register_script_message('get-input', function (script_name, args)
if selectable_items then
matches = {}
selected_match = args.default_item or 1
default_item = args.default_item
first_match_to_print = 1
for i, item in ipairs(selectable_items) do
matches[i] = { index = i, text = item }
Expand Down

0 comments on commit 81c5635

Please sign in to comment.