console.lua: print position/count instead of n hidden items

This makes it consistent with the new changes to show-text ${playlist}
and greatly simplifies the logic, making it sane to implement scrolling
the view with the wheel in the commit.
This commit is contained in:
Guido Cella 2024-10-11 21:58:41 +02:00 committed by Kacper Michajłow
parent 77239b87f1
commit e88a1a7de7
1 changed files with 22 additions and 63 deletions

View File

@ -362,60 +362,31 @@ local function populate_log_with_matches()
local log = log_buffers[id] local log = log_buffers[id]
local max_log_lines = calculate_max_log_lines() local max_log_lines = calculate_max_log_lines()
local print_counter = false
if selected_match < first_match_to_print then if #matches > max_log_lines then
first_match_to_print = selected_match print_counter = true
end
if first_match_to_print > 1 then
-- Reserve the first line for "n hidden items".
max_log_lines = max_log_lines - 1 max_log_lines = max_log_lines - 1
end end
if selected_match > first_match_to_print + max_log_lines - 1 then if selected_match < first_match_to_print then
-- Reserve the first line for "n hidden items" if it wasn't already. first_match_to_print = selected_match
if first_match_to_print == 1 then elseif selected_match > first_match_to_print + max_log_lines - 1 then
max_log_lines = max_log_lines - 1
end
first_match_to_print = selected_match - max_log_lines + 1 first_match_to_print = selected_match - max_log_lines + 1
end end
local last_match_to_print = math.min(first_match_to_print + max_log_lines - 1, local last_match_to_print = math.min(first_match_to_print + max_log_lines - 1,
#matches) #matches)
if last_match_to_print < #matches then if print_counter then
-- Reserve the last line for "n hidden items".
last_match_to_print = last_match_to_print - 1
-- After decrementing the last match to print, we need to check if the
-- selected match is beyond the last match to print again, and shift
-- both the first and last match to print when it is.
if selected_match > last_match_to_print then
if first_match_to_print == 1 then
-- Reserve the first line for "2 hidden items".
first_match_to_print = first_match_to_print + 1
end
first_match_to_print = first_match_to_print + 1
last_match_to_print = last_match_to_print + 1
end
end
-- When there is only 1 hidden item, print it in the previously reserved
-- line instead of printing "1 hidden items".
if first_match_to_print == 2 then
first_match_to_print = 1
end
if last_match_to_print == #matches - 1 then
last_match_to_print = #matches
end
if first_match_to_print > 1 then
log[1] = { log[1] = {
text = '↑ (' .. (first_match_to_print - 1) .. ' hidden items)', text = '',
style = styles.disabled, style = styles.disabled .. selected_match .. '/' .. #matches ..
terminal_style = terminal_styles.disabled, ' {\\fs' .. opts.font_size * 0.75 .. '}[' ..
first_match_to_print .. '-' .. last_match_to_print .. ']',
terminal_style = terminal_styles.disabled .. selected_match .. '/' ..
#matches .. ' [' .. first_match_to_print .. '-' ..
last_match_to_print .. ']',
} }
end end
@ -438,14 +409,6 @@ local function populate_log_with_matches()
terminal_style = terminal_style, terminal_style = terminal_style,
} }
end end
if last_match_to_print < #matches then
log[#log + 1] = {
text = '↓ (' .. (#matches - last_match_to_print) .. ' hidden items)',
style = styles.disabled,
terminal_style = terminal_styles.disabled,
}
end
end end
local function print_to_terminal() local function print_to_terminal()
@ -831,16 +794,17 @@ local function highlight_hovered_line()
/ opts.font_size / opts.font_size
local clicked_line = math.floor(y / height * max_lines + .5) local clicked_line = math.floor(y / height * max_lines + .5)
-- Subtract 1 line for "n hidden items" when necessary. local offset = first_match_to_print - 1
local offset = first_match_to_print == 1 and 0 or first_match_to_print - 2
max_lines = calculate_max_log_lines() max_lines = calculate_max_log_lines()
-- Subtract 1 line for the position counter.
if first_match_to_print > 1 or offset + max_lines < #matches then
offset = offset - 1
end
if #matches < max_lines then if #matches < max_lines then
clicked_line = clicked_line - (max_lines - #matches) clicked_line = clicked_line - (max_lines - #matches)
max_lines = #matches max_lines = #matches
elseif offset + max_lines < #matches then
-- Subtract 1 line for "n hidden items".
max_lines = max_lines - 1
end end
if selected_match ~= offset + clicked_line if selected_match ~= offset + clicked_line
@ -913,11 +877,6 @@ end
-- Go to the first command in the command history (PgUp) -- Go to the first command in the command history (PgUp)
local function handle_pgup() local function handle_pgup()
if selectable_items then if selectable_items then
-- We don't know whether to count the "n hidden items" lines here; an
-- offset of 2 is better with 1 extra line because it scrolls from the
-- last to the first visible match, while with both extra lines that is
-- done with +3. When there are no "n hidden items" lines selected_match
-- becomes 1 with any offset >= 1.
selected_match = math.max(selected_match - calculate_max_log_lines() + 2, 1) selected_match = math.max(selected_match - calculate_max_log_lines() + 2, 1)
update() update()
return return
@ -1789,9 +1748,9 @@ mp.register_script_message('get-input', function (script_name, args)
default_item = args.default_item default_item = args.default_item
local max_lines = calculate_max_log_lines() local max_lines = calculate_max_log_lines()
first_match_to_print = math.max(1, selected_match - math.floor(max_lines / 2) + 2) 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 if first_match_to_print > #selectable_items - max_lines + 2 then
first_match_to_print = math.max(1, #selectable_items - max_lines + 2) first_match_to_print = math.max(1, #selectable_items - max_lines + 1)
end end
for i, item in ipairs(selectable_items) do for i, item in ipairs(selectable_items) do