Comparison

util/human/io.lua @ 11894:57106c61d104

util.human.io: Factor out ellipsis function Could be useful elsewhere
author Kim Alvefur <zash@zash.se>
date Fri, 12 Nov 2021 12:14:27 +0100
parent 11893:afef1e170de7
child 11895:d278a4c6da7f
comparison
equal deleted inserted replaced
11893:afef1e170de7 11894:57106c61d104
93 93
94 local function padleft(s, width) 94 local function padleft(s, width)
95 return string.rep(" ", width-#s)..s; 95 return string.rep(" ", width-#s)..s;
96 end 96 end
97 97
98 local function ellipsis(s, width)
99 return s:sub(1, width-1) .. "…";
100 end
101
98 local function new_table(col_specs, max_width) 102 local function new_table(col_specs, max_width)
99 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80; 103 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80;
100 local separator = " | "; 104 local separator = " | ";
101 105
102 local widths = {}; 106 local widths = {};
145 v = padleft(v, width); 149 v = padleft(v, width);
146 else 150 else
147 v = padright(v, width); 151 v = padright(v, width);
148 end 152 end
149 elseif #v > width then 153 elseif #v > width then
150 v = v:sub(1, width-1) .. "…"; 154 v = ellipsis(v, width);
151 end 155 end
152 table.insert(output, v); 156 table.insert(output, v);
153 end 157 end
154 return table.concat(output, separator); 158 return table.concat(output, separator);
155 end; 159 end;
163 read_password = read_password; 167 read_password = read_password;
164 show_prompt = show_prompt; 168 show_prompt = show_prompt;
165 printf = printf; 169 printf = printf;
166 padleft = padleft; 170 padleft = padleft;
167 padright = padright; 171 padright = padright;
172 ellipsis = ellipsis;
168 table = new_table; 173 table = new_table;
169 }; 174 };