Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion player/lua/stats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ local function text_style()
end


local function ellipsize(text, max_len)
assert(text, "no text")
max_len = max_len or 80
if max_len < 6 or text:len() <= max_len then
return text end

local middle = max_len/2 + max_len%2
local ellipsized = ("%s...%s"):format(text:sub(1, middle-1), text:sub(-middle+2))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will split multi-byte characters and grapheme clusters and so won't work as expected or produce invalid UTF-8 with non-ASCII values.

Best you can do in Lua here is to at least operate on codepoints and avoid the invalid UTF-8 part (it may still split things like emoji sequences or modifier characters and produce weird results).

Copy link
Author

@WhitePeter WhitePeter Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I was hoping for Schrödinger's UTF-8 support cat to be alive in Lua. So there doesn't seem to be any way to get this on the cheap. I'll explore bstr support for UTF-8 then.


return assert(ellipsized:len() == max_len, "lengths don't match") and ellipsized
end


local function has_vo_window()
return mp.get_property_native("vo-configured") and mp.get_property_native("video-osd")
end
Expand Down Expand Up @@ -278,6 +291,9 @@ local function append(s, str, attr)
attr.no_prefix_markup = attr.no_prefix_markup or false
attr.prefix = attr.no_prefix_markup and attr.prefix or bold(attr.prefix)

-- squeeze str to fit the desired length
str = attr.max_len and ellipsize(str, attr.max_len) or str

local index = #s + (attr.nl == "" and 0 or 1)
s[index] = s[index] or ""
s[index] = s[index] .. format("%s%s%s%s%s%s", attr.nl, attr.indent,
Expand Down Expand Up @@ -674,7 +690,7 @@ end

local function add_file(s, print_cache, print_tags)
append(s, "", {prefix="File:", nl="", indent=""})
append_property(s, "filename", {prefix_sep="", nl="", indent=""})
append_property(s, "filename", {prefix_sep="", nl="", indent="", max_len=32})
if mp.get_property_osd("filename") ~= mp.get_property_osd("media-title") then
append_property(s, "media-title", {prefix="Title:"})
end
Expand Down