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
35 file_tag_max_length = 128, -- only show file tags shorter than this length in bytes35 file_tag_max_length = 128, -- only show file tags shorter than this length in bytes
36 file_tag_max_count = 16, -- only show the first x file tags36 file_tag_max_count = 16, -- only show the first x file tags
37 show_frame_info = false, -- whether to show the current frame info37 show_frame_info = false, -- whether to show the current frame info
38 term_width_limit = -1, -- overwrites the terminal width38 term_clip = true,
39 term_height_limit = -1, -- overwrites the terminal height39 term_height_limit = -1, -- overwrites the terminal height
40 debug = false,40 debug = false,
41 41
@@ -92,11 +92,7 @@ local o = { · 50 unmodified lines
92}92}
93options.read_options(o)93options.read_options(o)
94 94
95o.term_width_limit = tonumber(o.term_width_limit) or -1
96o.term_height_limit = tonumber(o.term_height_limit) or -195o.term_height_limit = tonumber(o.term_height_limit) or -1
97if o.term_width_limit < 0 then
98 o.term_width_limit = nil
99end
100if o.term_height_limit < 0 then96if o.term_height_limit < 0 then
101 o.term_height_limit = nil97 o.term_height_limit = nil
102end98end
@@ -1093,47 +1089,6 @@ local function eval_ass_formatting() · 990 unmodified lines
1093 end1089 end
1094end1090end
1095 1091
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)
1101local 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
1128end
1129
1130local 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
1135end
1136
1137-- split str into a table1092-- split str into a table
1138-- example: local t = split(s, "\n")1093-- example: local t = split(s, "\n")
1139-- plain: whether pat is a plain string (default false - pat is a pattern)1094-- plain: whether pat is a plain string (default false - pat is a pattern)
@@ -1156,7 +1111,6 @@ end · 16 unmodified lines
1156-- apply_scroll: scroll the content1111-- apply_scroll: scroll the content
1157local function finalize_page(header, content, apply_scroll)1112local function finalize_page(header, content, apply_scroll)
1158 local term_size = mp.get_property_native("term-size", {})1113 local term_size = mp.get_property_native("term-size", {})
1159 local term_width = o.term_width_limit or term_size.w or 80
1160 local term_height = o.term_height_limit or term_size.h or 241114 local term_height = o.term_height_limit or term_size.h or 24
1161 local from, to = 1, #content1115 local from, to = 1, #content
1162 if apply_scroll and term_height > 0 then1116 if apply_scroll and term_height > 0 then
@@ -1171,10 +1125,10 @@ local function finalize_page(header, content, apply_scroll) · 8 unmodified lines
1171 pages[curr_page].offset = from1125 pages[curr_page].offset = from
1172 end1126 end
1173 local output = table.concat(header) .. table.concat(content, "", from, to)1127 local output = table.concat(header) .. table.concat(content, "", from, to)
1174 if not o.use_ass and term_width > 0 then1128 if not o.use_ass and o.term_clip then
1129 local clip = mp.get_property("term-clip-cc")
1175 local t = split(output, "\n", true)1130 local t = split(output, "\n", true)
1176 -- limit width for the terminal1131 output = clip .. table.concat(t, "\n" .. clip)
1177 output = table.concat(term_ellipsis_array(t, 1, #t, term_width), "\n")
1178 end1132 end
1179 return output, from1133 return output, from
1180end1134end
@@ -1211,7 +1165,7 @@ local function keybinding_info(after_scroll, bindlist) · 30 unmodified lines
1211 header = {table.concat(header)}1165 header = {table.concat(header)}
1212 1166
1213 if not kbinfo_lines or not after_scroll then1167 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()
1215 end1169 end
1216 1170
1217 return finalize_page(header, kbinfo_lines, not bindlist)1171 return finalize_page(header, kbinfo_lines, not bindlist)
@@ -1793,7 +1747,6 @@ mp.register_event("video-reconfig", · 575 unmodified lines
1793 end1747 end
1794 end)1748 end)
1795 1749
1796-- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>}
1797if o.bindlist ~= "no" then1750if o.bindlist ~= "no" then
1798 -- This is a special mode to print key bindings to the terminal,1751 -- This is a special mode to print key bindings to the terminal,
1799 -- Adjust the print format and level to make it print only the key bindings.1752 -- 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
1804 -- wait for all other scripts to finish init1757 -- wait for all other scripts to finish init
1805 mp.add_timeout(0, function()1758 mp.add_timeout(0, function()
1806 if o.bindlist:sub(1, 1) == "-" then1759 if o.bindlist:sub(1, 1) == "-" then
1807 o.bindlist = o.bindlist:sub(2)
1808 o.no_ass_b0 = ""1760 o.no_ass_b0 = ""
1809 o.no_ass_b1 = ""1761 o.no_ass_b1 = ""
1810 end1762 end
1811 local width = max(40, math.floor(tonumber(o.bindlist) or 79))
1812 o.ass_formatting = false1763 o.ass_formatting = false
1813 o.no_ass_indent = " "1764 o.no_ass_indent = " "
1814 o.term_size = { w = width , h = 0}
1815 mp.osd_message(keybinding_info(false, true))1765 mp.osd_message(keybinding_info(false, true))
1816 -- wait for next tick to print status line and flush it without clearing1766 -- wait for next tick to print status line and flush it without clearing
1817 mp.add_timeout(0, function()1767 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