Skip to content

Commit

Permalink
console.lua: use ${term-clip-cc} to clip lines
Browse files Browse the repository at this point in the history
Instead of considering every non-ASCII character 2-cell wide, use this
newly introduced property to let msg.c clip the select menu using all
available with.
  • Loading branch information
guidocella authored and kasper93 committed Oct 11, 2024
1 parent bf025cd commit ba3b4ed
Showing 1 changed file with 5 additions and 41 deletions.
46 changes: 5 additions & 41 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,6 @@ local function len_utf8(str)
return len
end

local function truncate_utf8(str, max_length)
local len = 0
local pos = 1
while pos <= #str do
local last_pos = pos
pos = next_utf8(str, pos)
len = len + 1
if pos > last_pos + 1 then
if len == max_length - 1 then
pos = last_pos
else
len = len + 1
end
end
if len == max_length - 1 then
return str:sub(1, pos - 1) .. ''
end
end
return str
end


-- Functions to calculate the font width.
local width_length_ratio = 0.5
Expand Down Expand Up @@ -371,22 +350,7 @@ local function fuzzy_find(needle, haystacks)
return result
end

local function calculate_max_terminal_width()
local max_width = mp.get_property_native('term-size/w', 80)

-- The longest module name is vo/gpu-next/libplacebo.
if mp.get_property_native('msg-module') then
max_width = max_width - 24
end

if mp.get_property_native('msg-time') then
max_width = max_width - 13
end

return max_width
end

local function populate_log_with_matches(max_width)
local function populate_log_with_matches()
if not selectable_items or selected_match == 0 then
return
end
Expand Down Expand Up @@ -454,8 +418,7 @@ local function populate_log_with_matches(max_width)

for i = first_match_to_print, last_match_to_print do
log[#log + 1] = {
text = (max_width and truncate_utf8(matches[i].text, max_width)
or matches[i].text),
text = matches[i].text,
style = i == selected_match and styles.selected_suggestion or '',
terminal_style = i == selected_match and terminal_styles.selected_suggestion or '',
}
Expand All @@ -478,11 +441,12 @@ local function print_to_terminal()
return
end

populate_log_with_matches(calculate_max_terminal_width())
populate_log_with_matches()

local log = ''
local clip = selectable_items and mp.get_property('term-clip-cc') or ''
for _, log_line in ipairs(log_buffers[id]) do
log = log .. log_line.terminal_style .. log_line.text .. '\027[0m\n'
log = log .. clip .. log_line.terminal_style .. log_line.text .. '\027[0m\n'
end

local suggestions = ''
Expand Down

0 comments on commit ba3b4ed

Please sign in to comment.