Changeset

11895:d278a4c6da7f

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...
author Kim Alvefur <zash@zash.se>
date Fri, 12 Nov 2021 12:19:01 +0100
parents 11894:57106c61d104
children 11896:93e9f7ae2f9b
files util/human/io.lua
diffstat 1 files changed, 5 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/util/human/io.lua	Fri Nov 12 12:14:27 2021 +0100
+++ b/util/human/io.lua	Fri Nov 12 12:19:01 2021 +0100
@@ -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 @@
 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)