console: support utf8 for table formatting

This commit is contained in:
Christoph Heinrich authored and Dudemanguy committed 2023-10-07 22:45:50 +00:00
1 parent d2c92f7653
commit e475e1abc0
1 file changed
+18 -3
+18 -3
View File
@@ -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