Skip to content

console.lua: say focused instead of selected #16181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DOCS/interface-changes/console-focused-color.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rename `console-selected_color` script-opt to `console-focused_color`
rename `console-selected_back_color` script-opt to `console-focused_back_color`
8 changes: 4 additions & 4 deletions DOCS/man/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ Configurable Options
Whether to scale the console with the window height. Can be ``yes``, ``no``,
or ``auto``, which follows the value of ``--osd-scale-by-window``.

``selected_color``
``focused_color``
Default: ``#222222``

The color of the selected item.
The color of the focused item.

``selected_back_color``
``focused_back_color``
Default: ``#FFFFFF``

The background color of the selected item.
The background color of the focused item.

``match_color``
Default: ``#0088FF``
Expand Down
10 changes: 5 additions & 5 deletions DOCS/man/select.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ of the presented items, and key bindings listed in `CONSOLE`_ are extended with
the following:

ENTER, Ctrl+j and Ctrl+m
Confirm the selection of the highlighted item.
Select the focused item.

UP and Ctrl+p
Select the item above, or the last one when the first item is selected.
Focus the item above, or the last one when the first item is selected.

DOWN and Ctrl+n
Select the item below, or the first one when the last item is selected.
Focus the item below, or the first one when the last item is selected.

PGUP and Ctrl+b
Scroll up one page.
Expand All @@ -30,8 +30,8 @@ PGDN and Ctrl+f
Scroll down one page.

MBTN_LEFT
Confirm the selection of the highlighted item, or close the console if
clicking outside of the menu rectangle.
Select the item under the cursor, or close the console if clicking outside
of the menu rectangle.

WHEEL_UP
Scroll up.
Expand Down
84 changes: 48 additions & 36 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ local opts = {
margin_x = -1,
margin_y = -1,
scale_with_window = "auto",
selected_color = "#222222",
selected_back_color = "#FFFFFF",
focused_color = "#222222",
focused_back_color = "#FFFFFF",
match_color = "#0088FF",
case_sensitive = platform ~= "windows" and true or false,
history_dedup = true,
font_hw_ratio = "auto",
selected_color = "",
selected_back_color = "",
}

local styles = {
Expand Down Expand Up @@ -102,7 +104,7 @@ local has_completions

local selectable_items
local matches = {}
local selected_match = 1
local focused_match = 1
local first_match_to_print = 1
local default_item
local item_positions = {}
Expand Down Expand Up @@ -465,7 +467,7 @@ local function fuzzy_find(needle, haystacks, case_sensitive)
end

local function get_matches_to_print(terminal)
if not selectable_items or selected_match == 0 then
if not selectable_items or focused_match == 0 then
return {}
end

Expand All @@ -475,10 +477,10 @@ local function get_matches_to_print(terminal)
local highlight = terminal and terminal_styles.matched_position or
"{\\1c&H" .. option_color_to_ass(opts.match_color) .. "}"

if selected_match < first_match_to_print then
first_match_to_print = selected_match
elseif selected_match > first_match_to_print + max_lines - 1 then
first_match_to_print = selected_match - max_lines + 1
if focused_match < first_match_to_print then
first_match_to_print = focused_match
elseif focused_match > first_match_to_print + max_lines - 1 then
first_match_to_print = focused_match - max_lines + 1
end

local last_match_to_print = math.min(first_match_to_print + max_lines - 1,
Expand All @@ -492,16 +494,16 @@ local function get_matches_to_print(terminal)
if matches[i].index == default_item then
item = terminal_styles.default_item
end
if i == selected_match then
if i == focused_match then
item = item .. terminal_styles.selected_completion
end
else
if i == selected_match then
if i == focused_match then
if searching_history and
mp.get_property("osd-border-style") == "outline-and-shadow" then
item = get_selected_ass()
else
item = "{\\1c&H" .. option_color_to_ass(opts.selected_color) .. "&}"
item = "{\\1c&H" .. option_color_to_ass(opts.focused_color) .. "&}"
end
end_highlight = item
end
Expand Down Expand Up @@ -583,7 +585,7 @@ local function print_to_terminal()
if #selectable_items > calculate_max_lines() then
local digits = math.ceil(math.log(#selectable_items, 10))
counter = terminal_styles.disabled ..
"[" .. string.format("%0" .. digits .. "d", selected_match) ..
"[" .. string.format("%0" .. digits .. "d", focused_match) ..
"/" .. string.format("%0" .. digits .. "d", #matches) ..
"]\027[0m "
end
Expand Down Expand Up @@ -751,15 +753,15 @@ local function render()
and y + (1 + i) * line_height
or y - (1.5 + #items - i) * line_height

if (first_match_to_print - 1 + i == selected_match or
if (first_match_to_print - 1 + i == focused_match or
matches[first_match_to_print - 1 + i].index == default_item)
and (not searching_history or border_style == "background-box") then
ass:new_event()
ass:an(4)
ass:pos(x, item_y)
ass:append("{\\blur0\\bord0\\4aH&ff&\\1c&H" ..
option_color_to_ass(opts.selected_back_color) .. "&}")
if first_match_to_print - 1 + i ~= selected_match then
option_color_to_ass(opts.focused_back_color) .. "&}")
if first_match_to_print - 1 + i ~= focused_match then
ass:append("{\\1aH&cc&}")
end
ass:draw_start()
Expand All @@ -785,7 +787,7 @@ local function render()
if not searching_history or border_style == "background-box" then
ass:append("{\\bord0\\4a&Hff&\\blur0}")
end
ass:append(selected_match .. "/" .. #matches)
ass:append(focused_match .. "/" .. #matches)

local start_percentage = (first_match_to_print - 1) / #matches
local end_percentage = (first_match_to_print - 1 + max_lines) / #matches
Expand Down Expand Up @@ -895,15 +897,15 @@ local function handle_edit()
end

if line == "" and default_item then
selected_match = default_item
focused_match = default_item

local max_lines = calculate_max_lines()
first_match_to_print = math.max(1, selected_match + 1 - math.ceil(max_lines / 2))
first_match_to_print = math.max(1, focused_match + 1 - math.ceil(max_lines / 2))
if first_match_to_print > #selectable_items - max_lines + 1 then
first_match_to_print = math.max(1, #selectable_items - max_lines + 1)
end
else
selected_match = 1
focused_match = 1
end

render()
Expand Down Expand Up @@ -982,7 +984,7 @@ local function submit()
if searching_history then
searching_history = false
selectable_items = nil
line = #matches > 0 and matches[selected_match].text or ""
line = #matches > 0 and matches[focused_match].text or ""
cursor = #line + 1
handle_edit()
unbind_mouse()
Expand All @@ -992,7 +994,7 @@ local function submit()
if selectable_items then
if #matches > 0 then
mp.commandv("script-message-to", input_caller, "input-event", "submit",
utils.format_json({matches[selected_match].index}))
utils.format_json({matches[focused_match].index}))
end
else
if selected_completion_index == 0 and autoselect_completion then
Expand Down Expand Up @@ -1035,16 +1037,16 @@ end
local function bind_mouse()
mp.add_forced_key_binding("MOUSE_MOVE", "_console_mouse_move", function()
local item = determine_hovered_item()
if item and item ~= selected_match then
selected_match = item
if item and item ~= focused_match then
focused_match = item
render()
end
end)

mp.add_forced_key_binding("MBTN_LEFT", "_console_mbtn_left", function()
local item = determine_hovered_item()
if item then
selected_match = item
focused_match = item
submit()
else
set_active(false)
Expand Down Expand Up @@ -1097,13 +1099,13 @@ local function move_history(amount, is_wheel)
if is_wheel then
local max_lines = calculate_max_lines()

-- Update selected_match only if it's the first or last printed item and
-- Update focused_match only if it's the first or last printed item and
-- there are hidden items.
if (amount > 0 and selected_match == first_match_to_print
if (amount > 0 and focused_match == first_match_to_print
and first_match_to_print - 1 + max_lines < #matches)
or (amount < 0 and selected_match == first_match_to_print - 1 + max_lines
or (amount < 0 and focused_match == first_match_to_print - 1 + max_lines
and first_match_to_print > 1) then
selected_match = selected_match + amount
focused_match = focused_match + amount
end

if amount > 0 and first_match_to_print < #matches - max_lines + 1
Expand All @@ -1116,26 +1118,26 @@ local function move_history(amount, is_wheel)

local item = determine_hovered_item()
if item then
selected_match = item
focused_match = item
end

render()
return
end

selected_match = selected_match + amount
if selected_match > #matches then
selected_match = 1
elseif selected_match < 1 then
selected_match = #matches
focused_match = focused_match + amount
if focused_match > #matches then
focused_match = 1
elseif focused_match < 1 then
focused_match = #matches
end
render()
end

-- Go to the first command in the command history (PgUp)
local function handle_pgup()
if selectable_items then
selected_match = math.max(selected_match - calculate_max_lines() + 1, 1)
focused_match = math.max(focused_match - calculate_max_lines() + 1, 1)
render()
return
end
Expand All @@ -1146,7 +1148,7 @@ end
-- Stop browsing history and start editing a blank line (PgDown)
local function handle_pgdown()
if selectable_items then
selected_match = math.min(selected_match + calculate_max_lines() - 1, #matches)
focused_match = math.min(focused_match + calculate_max_lines() - 1, #matches)
render()
return
end
Expand Down Expand Up @@ -1731,4 +1733,14 @@ end)

require "mp.options".read_options(opts, nil, render)

if opts.selected_color ~= "" then
opts.focused_color = opts.selected_color
mp.msg.warn("selected_color has been replaced by focused_color")
end

if opts.selected_back_color ~= "" then
opts.focused_back_color = opts.selected_back_color
mp.msg.warn("selected_back_color has been replaced by focused_back_color")
end

collectgarbage()
Loading