Software /
code /
prosody
Diff
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 |
line wrap: on
line diff
--- 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; };