Comparison

util/human/io.lua @ 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
parent 11894:57106c61d104
child 11896:93e9f7ae2f9b
comparison
equal deleted inserted replaced
11894:57106c61d104 11895:d278a4c6da7f
1 local array = require "util.array"; 1 local array = require "util.array";
2 local utf8 = rawget(_G,"utf8") or require"util.encodings".utf8;
2 3
3 local function getchar(n) 4 local function getchar(n)
4 local stty_ret = os.execute("stty raw -echo 2>/dev/null"); 5 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
5 local ok, char; 6 local ok, char;
6 if stty_ret == true or stty_ret == 0 then 7 if stty_ret == true or stty_ret == 0 then
94 local function padleft(s, width) 95 local function padleft(s, width)
95 return string.rep(" ", width-#s)..s; 96 return string.rep(" ", width-#s)..s;
96 end 97 end
97 98
98 local function ellipsis(s, width) 99 local function ellipsis(s, width)
99 return s:sub(1, width-1) .. "…"; 100 if #s <= width then return s; end
101 s = s:sub(1, width - 1)
102 while not utf8.len(s) do s = s:sub(1, -2); end
103 return s .. "…";
100 end 104 end
101 105
102 local function new_table(col_specs, max_width) 106 local function new_table(col_specs, max_width)
103 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80; 107 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80;
104 local separator = " | "; 108 local separator = " | ";