aboutsummaryrefslogtreecommitdiffstats
path: root/util/human
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-11-12 12:19:01 +0100
committerKim Alvefur <zash@zash.se>2021-11-12 12:19:01 +0100
commitb9837e2d99bddff368c4b5e2721479093c1f8858 (patch)
tree802c9a546ce56bf7f8b473b23f2d42c446116824 /util/human
parentf8afa0807b38482f361eaab228193b53dbebc962 (diff)
downloadprosody-b9837e2d99bddff368c4b5e2721479093c1f8858.tar.gz
prosody-b9837e2d99bddff368c4b5e2721479093c1f8858.zip
util.human.io: Trim any broken UTF-8 from ellipsis
This should fix basic problems with multi-byte UTF-8 sequences getting cut in the middle. Down the rabbit hole we go...
Diffstat (limited to 'util/human')
-rw-r--r--util/human/io.lua6
1 files changed, 5 insertions, 1 deletions
diff --git a/util/human/io.lua b/util/human/io.lua
index 2e59ea85..09ed2807 100644
--- a/util/human/io.lua
+++ b/util/human/io.lua
@@ -1,4 +1,5 @@
local array = require "util.array";
+local utf8 = rawget(_G,"utf8") or require"util.encodings".utf8;
local function getchar(n)
local stty_ret = os.execute("stty raw -echo 2>/dev/null");
@@ -96,7 +97,10 @@ local function padleft(s, width)
end
local function ellipsis(s, width)
- return s:sub(1, width-1) .. "…";
+ 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 .. "…";
end
local function new_table(col_specs, max_width)