diff options
author | Kim Alvefur <zash@zash.se> | 2021-11-12 14:21:15 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2021-11-12 14:21:15 +0100 |
commit | d1144a7ede35eb04e0c81c328e3684eb1f7f3f72 (patch) | |
tree | 11210945b10d20eb85dd51a9d7e188753511f70f /util/human/io.lua | |
parent | b9837e2d99bddff368c4b5e2721479093c1f8858 (diff) | |
download | prosody-d1144a7ede35eb04e0c81c328e3684eb1f7f3f72.tar.gz prosody-d1144a7ede35eb04e0c81c328e3684eb1f7f3f72.zip |
util.human.io: Fix cutting of UTF-8 into pieces
Down the rabbit hole we go...
Diffstat (limited to 'util/human/io.lua')
-rw-r--r-- | util/human/io.lua | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/util/human/io.lua b/util/human/io.lua index 09ed2807..83a521c3 100644 --- a/util/human/io.lua +++ b/util/human/io.lua @@ -1,5 +1,9 @@ local array = require "util.array"; -local utf8 = rawget(_G,"utf8") or require"util.encodings".utf8; +local utf8 = rawget(_G, "utf8") or require"util.encodings".utf8; +local len = utf8.len or function(s) + local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", ""); + return count; +end; local function getchar(n) local stty_ret = os.execute("stty raw -echo 2>/dev/null"); @@ -96,11 +100,21 @@ local function padleft(s, width) return string.rep(" ", width-#s)..s; end +local pat = "[%z\001-\127\194-\253][\128-\191]*"; +local function utf8_cut(s, pos) + return s:match("^"..pat:rep(pos)) or s; +end + +if utf8.len and utf8.offset then + function utf8_cut(s, pos) + return s:sub(1, utf8.offset(s, pos+1)-1); + end +end + local function ellipsis(s, width) - if #s <= width then return s; end - s = s:sub(1, width - 1) - while not utf8.len(s) do s = s:sub(1, -2); end - return s .. "…"; + if len(s) <= width then return s; end + if width == 1 then return "…"; end + return utf8_cut(s, width - 1) .. "…"; end local function new_table(col_specs, max_width) @@ -148,13 +162,13 @@ local function new_table(col_specs, max_width) else v = tostring(v); end - if #v < width then + if len(v) < width then if column.align == "right" then v = padleft(v, width); else v = padright(v, width); end - elseif #v > width then + elseif len(v) > width then v = ellipsis(v, width); end table.insert(output, v); |