# HG changeset patch # User Matthew Wild # Date 1680867197 -3600 # Node ID 5bd2720953886bfd22b82fdab6a9a24af1ca56c2 # Parent d4f7118d15315933a38c1be4a3822b250c2dd752 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 diff -r d4f7118d1531 -r 5bd272095388 util/human/io.lua --- a/util/human/io.lua Fri Apr 07 13:09:00 2023 +0200 +++ b/util/human/io.lua Fri Apr 07 12:33:17 2023 +0100 @@ -108,6 +108,17 @@ end end +local function term_width(default) + local stty = io.popen("stty -a"); + if not stty then return default; end + local result = stty:read("*a"); + if result then + result = result:match("%f[%w]columns[ =]*(%d+)"); + end + stty:close(); + return tonumber(result or default); +end + local function ellipsis(s, width) if len(s) <= width then return s; end if width == 1 then return "…"; end @@ -194,6 +205,7 @@ printf = printf; padleft = padleft; padright = padright; + term_width = term_width; ellipsis = ellipsis; table = new_table; };