summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Heinrich <christoph.heinrich@student.tugraz.at>2022-11-03 18:14:53 +0100
committerDudemanguy <random342@airmail.cc>2023-10-07 22:45:50 +0000
commite475e1abc06c5ee5fd480ef4cea5c32491c6ab68 (patch)
tree62ed8d318ee38d132069668fd6b5d8543b7c4269
parentd2c92f76532dbd4dfc03b21dc9fbbfb6618bcfa1 (diff)
downloadmpv-e475e1abc06c5ee5fd480ef4cea5c32491c6ab68.tar.bz2
mpv-e475e1abc06c5ee5fd480ef4cea5c32491c6ab68.tar.xz
console: support utf8 for table formatting
-rw-r--r--player/lua/console.lua21
1 files changed, 18 insertions, 3 deletions
diff --git a/player/lua/console.lua b/player/lua/console.lua
index 76b8be527a..c1958ef7dd 100644
--- a/player/lua/console.lua
+++ b/player/lua/console.lua
@@ -158,6 +158,11 @@ function format_table(list, width_max, rows_max)
-- total width without spacing
local width_total = 0
+ local list_widths = {}
+ for i, item in ipairs(list) do
+ list_widths[i] = len_utf8(item)
+ end
+
-- use as many columns as possible
for rows = 1, list_size do
local columns = math.ceil(list_size / rows)
@@ -170,9 +175,9 @@ function format_table(list, width_max, rows_max)
for row = 1, rows do
local i = row + (column - 1) * rows
if i > #list then break end
- local len = list[i]:len()
- if width < len then
- width = len
+ local item_width = list_widths[i]
+ if width < item_width then
+ width = item_width
end
end
column_widths[column] = width
@@ -360,6 +365,16 @@ function prev_utf8(str, pos)
return pos
end
+function len_utf8(str)
+ local len = 0
+ local pos = 1
+ while pos <= str:len() do
+ pos = next_utf8(str, pos)
+ len = len + 1
+ end
+ return len
+end
+
-- Insert a character at the current cursor position (any_unicode)
function handle_char_input(c)
if insert_mode then