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

Unified · Split

3 files
  1. DOCS/interface-changes/stats-term-clip.txt +2 −0
  2. DOCS/man/stats.rst +7 −8
  3. player/lua/stats.lua +5 −55
DOCS/interface-changes/stats-term-clip.txt+2−0
@@ -0,0 +1,2 @@
1remove `stats-term_width_limit` script-opt
2add `stats-term_clip` script-opt
DOCS/man/stats.rst+7−8
@@ -115,11 +115,10 @@ Configurable Options · 114 unmodified lines
115 115
116 Only show the first specified amount of file tags.116 Only show the first specified amount of file tags.
117 117
118``term_width_limit``118``term_clip``
119 Default: -1119 Default: yes
120 120
121 Sets the terminal width.121 Whether to clip lines to the terminal width.
122 A value of 0 means the width is infinite, -1 means it's automatic.
123 122
124``term_height_limit``123``term_height_limit``
125 Default: -1124 Default: -1
@@ -259,10 +258,10 @@ string, and one should not expect documentation-level grouping accuracy, · 133 unmodified lines
259however, it should still be reasonably useful.258however, it should still be reasonably useful.
260 259
261Using ``--idle --script-opts-append=stats-bindlist=yes`` will print the list to260Using ``--idle --script-opts-append=stats-bindlist=yes`` will print the list to
262the terminal and quit immediately. By default long lines are shortened to 79261the terminal and quit immediately. Long lines are clipped to the terminal width
263chars, and terminal escape sequences are enabled. A different length limit can262unless this is disabled with ``--script-opts-append=stats-term_clip=no``. Escape
264be set by changing ``yes`` to a number (at least 40), and escape sequences can263sequences can be disabled by adding ``-`` before ``yes``, i.e.
265be disabled by adding ``-`` before the value, e.g. ``...=-yes`` or ``...=-120``.264``--script-opts-append=stats-bindlist=-yes``.
266 265
267Like with ``--input-test``, the list includes bindings from ``input.conf`` and266Like with ``--input-test``, the list includes bindings from ``input.conf`` and
268from user scripts. Use ``--no-config`` to list only built-in bindings.267from user scripts. Use ``--no-config`` to list only built-in bindings.
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