Skip to content

Commit d54ffb1

Browse files
committed
stats: new func ellipsize
Takes a string and an optional length and returns string of that length with '...' in the middle. If no length is provided it defaults to the good old 80 chars. If the string is shorter than target length the input string is returned unchanged. This, in conjunction with mpv-player#17021, is intended to eventually fix mpv-player#10975.
1 parent 28ef6b7 commit d54ffb1

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

player/lua/stats.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ local function text_style()
207207
end
208208

209209

210+
local function ellipsize(text, max_len)
211+
assert(text, "no text")
212+
max_len = max_len or 80
213+
if max_len < 6 or text:len() <= max_len then
214+
return text end
215+
216+
local middle = max_len/2 + max_len%2
217+
local ellipsized = ("%s...%s"):format(text:sub(1, middle-1), text:sub(-middle+2))
218+
219+
return assert(ellipsized:len() == max_len, "lengths don't match") and ellipsized
220+
end
221+
222+
210223
local function has_vo_window()
211224
return mp.get_property_native("vo-configured") and mp.get_property_native("video-osd")
212225
end

0 commit comments

Comments
 (0)