1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-17 20:34:58 +00:00

stats.lua: clip lines with ${term-clip-cc}

Use the property introduced in bf025cd289 to clip the lines of stats.lua
with accurate unicode width detection and considering --msg-module and
--msg-time. This allows removing the term ellipsis functions.

Also use script-opts-append instead of script-opts in the docs so
script-opts in mpv.conf, which could change keybindings, are not
overriden.
This commit is contained in:
Guido Cella 2024-10-18 00:00:22 +02:00 committed by Kacper Michajłow
parent f72f6b0fc4
commit a187110f4a
3 changed files with 14 additions and 63 deletions

View File

@ -0,0 +1,2 @@
remove `stats-term_width_limit` script-opt
add `stats-term_clip` script-opt

View File

@ -115,11 +115,10 @@ Configurable Options
Only show the first specified amount of file tags.
``term_width_limit``
Default: -1
``term_clip``
Default: yes
Sets the terminal width.
A value of 0 means the width is infinite, -1 means it's automatic.
Whether to clip lines to the terminal width.
``term_height_limit``
Default: -1
@ -259,10 +258,10 @@ string, and one should not expect documentation-level grouping accuracy,
however, it should still be reasonably useful.
Using ``--idle --script-opts-append=stats-bindlist=yes`` will print the list to
the terminal and quit immediately. By default long lines are shortened to 79
chars, and terminal escape sequences are enabled. A different length limit can
be set by changing ``yes`` to a number (at least 40), and escape sequences can
be disabled by adding ``-`` before the value, e.g. ``...=-yes`` or ``...=-120``.
the terminal and quit immediately. Long lines are clipped to the terminal width
unless this is disabled with ``--script-opts-append=stats-term_clip=no``. Escape
sequences can be disabled by adding ``-`` before ``yes``, i.e.
``--script-opts-append=stats-bindlist=-yes``.
Like with ``--input-test``, the list includes bindings from ``input.conf`` and
from user scripts. Use ``--no-config`` to list only built-in bindings.

View File

@ -35,7 +35,7 @@ local o = {
file_tag_max_length = 128, -- only show file tags shorter than this length in bytes
file_tag_max_count = 16, -- only show the first x file tags
show_frame_info = false, -- whether to show the current frame info
term_width_limit = -1, -- overwrites the terminal width
term_clip = true,
term_height_limit = -1, -- overwrites the terminal height
debug = false,
@ -92,11 +92,7 @@ local o = {
}
options.read_options(o)
o.term_width_limit = tonumber(o.term_width_limit) or -1
o.term_height_limit = tonumber(o.term_height_limit) or -1
if o.term_width_limit < 0 then
o.term_width_limit = nil
end
if o.term_height_limit < 0 then
o.term_height_limit = nil
end
@ -1093,47 +1089,6 @@ local function eval_ass_formatting()
end
end
-- assumptions:
-- s is composed of SGR escape sequences and/or printable UTF8 sequences
-- printable codepoints occupy one terminal cell (we don't have wcwidth)
-- tabstops are 8, 16, 24..., and the output starts at 0 or a tabstop
-- note: if maxwidth <= 2 and s doesn't fit: the result is "..." (more than 2)
local function term_ellipsis(s, maxwidth)
local TAB, ESC, SGR_END = 9, 27, ("m"):byte()
local width, ellipsis = 0, "..."
local fit_len, in_sgr
for i = 1, #s do
local x = s:byte(i)
if in_sgr then
in_sgr = x ~= SGR_END
elseif x == ESC then
in_sgr = true
ellipsis = "\27[0m..." -- ensure SGR reset
elseif x < 128 or x >= 192 then -- non UTF8-continuation
-- tab adds till the next stop, else add 1
width = width + (x == TAB and 8 - width % 8 or 1)
if fit_len == nil and width > maxwidth - 3 then
fit_len = i - 1 -- adding "..." still fits maxwidth
end
if width > maxwidth then
return s:sub(1, fit_len) .. ellipsis
end
end
end
return s
end
local function term_ellipsis_array(arr, from, to, max_width)
for i = from, to do
arr[i] = term_ellipsis(arr[i], max_width)
end
return arr
end
-- split str into a table
-- example: local t = split(s, "\n")
-- plain: whether pat is a plain string (default false - pat is a pattern)
@ -1156,7 +1111,6 @@ end
-- apply_scroll: scroll the content
local function finalize_page(header, content, apply_scroll)
local term_size = mp.get_property_native("term-size", {})
local term_width = o.term_width_limit or term_size.w or 80
local term_height = o.term_height_limit or term_size.h or 24
local from, to = 1, #content
if apply_scroll and term_height > 0 then
@ -1171,10 +1125,10 @@ local function finalize_page(header, content, apply_scroll)
pages[curr_page].offset = from
end
local output = table.concat(header) .. table.concat(content, "", from, to)
if not o.use_ass and term_width > 0 then
if not o.use_ass and o.term_clip then
local clip = mp.get_property("term-clip-cc")
local t = split(output, "\n", true)
-- limit width for the terminal
output = table.concat(term_ellipsis_array(t, 1, #t, term_width), "\n")
output = clip .. table.concat(t, "\n" .. clip)
end
return output, from
end
@ -1211,7 +1165,7 @@ local function keybinding_info(after_scroll, bindlist)
header = {table.concat(header)}
if not kbinfo_lines or not after_scroll then
kbinfo_lines = get_kbinfo_lines(o.term_width_limit)
kbinfo_lines = get_kbinfo_lines()
end
return finalize_page(header, kbinfo_lines, not bindlist)
@ -1793,7 +1747,6 @@ mp.register_event("video-reconfig",
end
end)
-- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>}
if o.bindlist ~= "no" then
-- This is a special mode to print key bindings to the terminal,
-- Adjust the print format and level to make it print only the key bindings.
@ -1804,14 +1757,11 @@ if o.bindlist ~= "no" then
-- wait for all other scripts to finish init
mp.add_timeout(0, function()
if o.bindlist:sub(1, 1) == "-" then
o.bindlist = o.bindlist:sub(2)
o.no_ass_b0 = ""
o.no_ass_b1 = ""
end
local width = max(40, math.floor(tonumber(o.bindlist) or 79))
o.ass_formatting = false
o.no_ass_indent = " "
o.term_size = { w = width , h = 0}
mp.osd_message(keybinding_info(false, true))
-- wait for next tick to print status line and flush it without clearing
mp.add_timeout(0, function()