Skip to content

Commit

Permalink
console.lua: don't highlight the first completion with mp.input.get
Browse files Browse the repository at this point in the history
The first completion is highlighted because it is automatically inserted
when pressing Enter, but this isn't the case when the console is used
with mp.input.get, and autoselecting it can be undesirable if you're
entering arbitrary text, unlike with mpv commands where you're usually
choosing from predefined lists. So just don't highlight the first
completion for mp.input clients.
  • Loading branch information
guidocella authored and kasper93 committed Dec 28, 2024
1 parent 4dbf81c commit 0417218
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ local function calculate_max_log_lines()
- 1.5)
end

local function should_highlight_completion(i)
return i == selected_suggestion_index or
(i == 1 and selected_suggestion_index == 0 and input_caller == nil)
end

-- Takes a list of strings, a max width in characters and
-- optionally a max row count.
-- The result contains at least one column.
Expand Down Expand Up @@ -377,8 +382,7 @@ local function format_table(list, width_max, rows_max)
or '%-' .. math.min(column_widths[column], 99) .. 's'
columns[column] = ass_escape(string.format(format_string, list[i]))

if i == selected_suggestion_index or
(i == 1 and selected_suggestion_index == 0) then
if should_highlight_completion(i) then
columns[column] = styles.selected_suggestion .. columns[column]
.. '{\\b}' .. styles.suggestion
end
Expand Down Expand Up @@ -491,8 +495,7 @@ local function print_to_terminal()

local suggestions = ''
for i, suggestion in ipairs(suggestion_buffer) do
if i == selected_suggestion_index or
(i == 1 and selected_suggestion_index == 0) then
if should_highlight_completion(i) then
suggestions = suggestions .. terminal_styles.selected_suggestion ..
suggestion .. '\027[0m'
else
Expand Down

0 comments on commit 0417218

Please sign in to comment.