From e475e1abc06c5ee5fd480ef4cea5c32491c6ab68 Mon Sep 17 00:00:00 2001 From: Christoph Heinrich Date: Thu, 3 Nov 2022 18:14:53 +0100 Subject: console: support utf8 for table formatting --- player/lua/console.lua | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'player/lua/console.lua') 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 -- cgit v1.2.3