History

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.

Guido Cella committed 2 years ago

Commit a1871101 parent f72f6b03 files · +14 −63Browse files

Showing the changes to player/lua/stats.lua (1 of 3 files). Show all

Unified · Split

player/lua/stats.lua+5−55
@@ -35,7 +35,7 @@ local o = { · 34 unmodified lines
3535 file_tag_max_length = 128, -- only show file tags shorter than this length in bytes
3636 file_tag_max_count = 16, -- only show the first x file tags
3737 show_frame_info = false, -- whether to show the current frame info
38− term_width_limit = -1, -- overwrites the terminal width
38+ term_clip = true,
3939 term_height_limit = -1, -- overwrites the terminal height
4040 debug = false,
4141
@@ -92,11 +92,7 @@ local o = { · 50 unmodified lines
9292 }
9393 options.read_options(o)
9494
95−o.term_width_limit = tonumber(o.term_width_limit) or -1
9695 o.term_height_limit = tonumber(o.term_height_limit) or -1
97−if o.term_width_limit < 0 then
98− o.term_width_limit = nil
99−end
10096 if o.term_height_limit < 0 then
10197 o.term_height_limit = nil
10298 end
@@ -1093,47 +1089,6 @@ local function eval_ass_formatting() · 990 unmodified lines
10931089 end
10941090 end
10951091
1096−-- assumptions:
1097−-- s is composed of SGR escape sequences and/or printable UTF8 sequences
1098−-- printable codepoints occupy one terminal cell (we don't have wcwidth)
1099−-- tabstops are 8, 16, 24..., and the output starts at 0 or a tabstop
1100−-- note: if maxwidth <= 2 and s doesn't fit: the result is "..." (more than 2)
1101−local function term_ellipsis(s, maxwidth)
1102− local TAB, ESC, SGR_END = 9, 27, ("m"):byte()
1103− local width, ellipsis = 0, "..."
1104− local fit_len, in_sgr
1105−
1106− for i = 1, #s do
1107− local x = s:byte(i)
1108−
1109− if in_sgr then
1110− in_sgr = x ~= SGR_END
1111− elseif x == ESC then
1112− in_sgr = true
1113− ellipsis = "\27[0m..." -- ensure SGR reset
1114− elseif x < 128 or x >= 192 then -- non UTF8-continuation
1115− -- tab adds till the next stop, else add 1
1116− width = width + (x == TAB and 8 - width % 8 or 1)
1117−
1118− if fit_len == nil and width > maxwidth - 3 then
1119− fit_len = i - 1 -- adding "..." still fits maxwidth
1120− end
1121− if width > maxwidth then
1122− return s:sub(1, fit_len) .. ellipsis
1123− end
1124− end
1125− end
1126−
1127− return s
1128−end
1129−
1130−local function term_ellipsis_array(arr, from, to, max_width)
1131− for i = from, to do
1132− arr[i] = term_ellipsis(arr[i], max_width)
1133− end
1134− return arr
1135−end
1136−
11371092 -- split str into a table
11381093 -- example: local t = split(s, "\n")
11391094 -- plain: whether pat is a plain string (default false - pat is a pattern)
@@ -1156,7 +1111,6 @@ end · 16 unmodified lines
11561111 -- apply_scroll: scroll the content
11571112 local function finalize_page(header, content, apply_scroll)
11581113 local term_size = mp.get_property_native("term-size", {})
1159− local term_width = o.term_width_limit or term_size.w or 80
11601114 local term_height = o.term_height_limit or term_size.h or 24
11611115 local from, to = 1, #content
11621116 if apply_scroll and term_height > 0 then
@@ -1171,10 +1125,10 @@ local function finalize_page(header, content, apply_scroll) · 8 unmodified lines
11711125 pages[curr_page].offset = from
11721126 end
11731127 local output = table.concat(header) .. table.concat(content, "", from, to)
1174− if not o.use_ass and term_width > 0 then
1128+ if not o.use_ass and o.term_clip then
1129+ local clip = mp.get_property("term-clip-cc")
11751130 local t = split(output, "\n", true)
1176− -- limit width for the terminal
1177− output = table.concat(term_ellipsis_array(t, 1, #t, term_width), "\n")
1131+ output = clip .. table.concat(t, "\n" .. clip)
11781132 end
11791133 return output, from
11801134 end
@@ -1211,7 +1165,7 @@ local function keybinding_info(after_scroll, bindlist) · 30 unmodified lines
12111165 header = {table.concat(header)}
12121166
12131167 if not kbinfo_lines or not after_scroll then
1214− kbinfo_lines = get_kbinfo_lines(o.term_width_limit)
1168+ kbinfo_lines = get_kbinfo_lines()
12151169 end
12161170
12171171 return finalize_page(header, kbinfo_lines, not bindlist)
@@ -1793,7 +1747,6 @@ mp.register_event("video-reconfig", · 575 unmodified lines
17931747 end
17941748 end)
17951749
1796−-- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>}
17971750 if o.bindlist ~= "no" then
17981751 -- This is a special mode to print key bindings to the terminal,
17991752 -- Adjust the print format and level to make it print only the key bindings.
@@ -1804,14 +1757,11 @@ if o.bindlist ~= "no" then · 4 unmodified lines
18041757 -- wait for all other scripts to finish init
18051758 mp.add_timeout(0, function()
18061759 if o.bindlist:sub(1, 1) == "-" then
1807− o.bindlist = o.bindlist:sub(2)
18081760 o.no_ass_b0 = ""
18091761 o.no_ass_b1 = ""
18101762 end
1811− local width = max(40, math.floor(tonumber(o.bindlist) or 79))
18121763 o.ass_formatting = false
18131764 o.no_ass_indent = " "
1814− o.term_size = { w = width , h = 0}
18151765 mp.osd_message(keybinding_info(false, true))
18161766 -- wait for next tick to print status line and flush it without clearing
18171767 mp.add_timeout(0, function()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t