Comparison

util/human/io.lua @ 13044:5bd272095388

util.human.io: Add term_width() method to discover the terminal width This is not standard POSIX, but apparently very widely supported. For reference: https://www.austingroupbugs.net/view.php?id=1053
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Apr 2023 12:33:17 +0100
parent 13040:0cbe400ebab4
child 13045:da0b3cb9a2ec
comparison
equal deleted inserted replaced
13043:d4f7118d1531 13044:5bd272095388
104 104
105 if utf8.len and utf8.offset then 105 if utf8.len and utf8.offset then
106 function utf8_cut(s, pos) 106 function utf8_cut(s, pos)
107 return s:sub(1, utf8.offset(s, pos+1)-1); 107 return s:sub(1, utf8.offset(s, pos+1)-1);
108 end 108 end
109 end
110
111 local function term_width(default)
112 local stty = io.popen("stty -a");
113 if not stty then return default; end
114 local result = stty:read("*a");
115 if result then
116 result = result:match("%f[%w]columns[ =]*(%d+)");
117 end
118 stty:close();
119 return tonumber(result or default);
109 end 120 end
110 121
111 local function ellipsis(s, width) 122 local function ellipsis(s, width)
112 if len(s) <= width then return s; end 123 if len(s) <= width then return s; end
113 if width == 1 then return "…"; end 124 if width == 1 then return "…"; end
192 read_password = read_password; 203 read_password = read_password;
193 show_prompt = show_prompt; 204 show_prompt = show_prompt;
194 printf = printf; 205 printf = printf;
195 padleft = padleft; 206 padleft = padleft;
196 padright = padright; 207 padright = padright;
208 term_width = term_width;
197 ellipsis = ellipsis; 209 ellipsis = ellipsis;
198 table = new_table; 210 table = new_table;
199 }; 211 };