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.
Showing the changes to player/lua/stats.lua (1 of 3 files). Show all
Unified · Split
| @@ -35,7 +35,7 @@ local o = { · 34 unmodified lines | ||
| 35 | 35 | file_tag_max_length = 128, -- only show file tags shorter than this length in bytes |
| 36 | 36 | file_tag_max_count = 16, -- only show the first x file tags |
| 37 | 37 | show_frame_info = false, -- whether to show the current frame info |
| 38 | − term_width_limit = -1, -- overwrites the terminal width | |
| 38 | + term_clip = true, | |
| 39 | 39 | term_height_limit = -1, -- overwrites the terminal height |
| 40 | 40 | debug = false, |
| 41 | 41 | |
| @@ -92,11 +92,7 @@ local o = { · 50 unmodified lines | ||
| 92 | 92 | } |
| 93 | 93 | options.read_options(o) |
| 94 | 94 | |
| 95 | −o.term_width_limit = tonumber(o.term_width_limit) or -1 | |
| 96 | 95 | 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 | |
| 100 | 96 | if o.term_height_limit < 0 then |
| 101 | 97 | o.term_height_limit = nil |
| 102 | 98 | end |
| @@ -1093,47 +1089,6 @@ local function eval_ass_formatting() · 990 unmodified lines | ||
| 1093 | 1089 | end |
| 1094 | 1090 | end |
| 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) | |
| 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 | − | |
| 1137 | 1092 | -- split str into a table |
| 1138 | 1093 | -- example: local t = split(s, "\n") |
| 1139 | 1094 | -- plain: whether pat is a plain string (default false - pat is a pattern) |
| @@ -1156,7 +1111,6 @@ end · 16 unmodified lines | ||
| 1156 | 1111 | -- apply_scroll: scroll the content |
| 1157 | 1112 | local function finalize_page(header, content, apply_scroll) |
| 1158 | 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 | 1114 | local term_height = o.term_height_limit or term_size.h or 24 |
| 1161 | 1115 | local from, to = 1, #content |
| 1162 | 1116 | if apply_scroll and term_height > 0 then |
| @@ -1171,10 +1125,10 @@ local function finalize_page(header, content, apply_scroll) · 8 unmodified lines | ||
| 1171 | 1125 | pages[curr_page].offset = from |
| 1172 | 1126 | end |
| 1173 | 1127 | 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") | |
| 1175 | 1130 | 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) | |
| 1178 | 1132 | end |
| 1179 | 1133 | return output, from |
| 1180 | 1134 | end |
| @@ -1211,7 +1165,7 @@ local function keybinding_info(after_scroll, bindlist) · 30 unmodified lines | ||
| 1211 | 1165 | header = {table.concat(header)} |
| 1212 | 1166 | |
| 1213 | 1167 | 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 | 1169 | end |
| 1216 | 1170 | |
| 1217 | 1171 | return finalize_page(header, kbinfo_lines, not bindlist) |
| @@ -1793,7 +1747,6 @@ mp.register_event("video-reconfig", · 575 unmodified lines | ||
| 1793 | 1747 | end |
| 1794 | 1748 | end) |
| 1795 | 1749 | |
| 1796 | −-- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>} | |
| 1797 | 1750 | if o.bindlist ~= "no" then |
| 1798 | 1751 | -- This is a special mode to print key bindings to the terminal, |
| 1799 | 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 | 1757 | -- wait for all other scripts to finish init |
| 1805 | 1758 | mp.add_timeout(0, function() |
| 1806 | 1759 | if o.bindlist:sub(1, 1) == "-" then |
| 1807 | − o.bindlist = o.bindlist:sub(2) | |
| 1808 | 1760 | o.no_ass_b0 = "" |
| 1809 | 1761 | o.no_ass_b1 = "" |
| 1810 | 1762 | end |
| 1811 | − local width = max(40, math.floor(tonumber(o.bindlist) or 79)) | |
| 1812 | 1763 | o.ass_formatting = false |
| 1813 | 1764 | o.no_ass_indent = " " |
| 1814 | − o.term_size = { w = width , h = 0} | |
| 1815 | 1765 | mp.osd_message(keybinding_info(false, true)) |
| 1816 | 1766 | -- wait for next tick to print status line and flush it without clearing |
| 1817 | 1767 | mp.add_timeout(0, function() |