aboutsummaryrefslogtreecommitdiffstats
path: root/util/human/io.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2023-04-07 12:33:17 +0100
committerMatthew Wild <mwild1@gmail.com>2023-04-07 12:33:17 +0100
commit403acbb836688f57e184b57b77f073a51b0dc4eb (patch)
treef204e2fd4461a0be8f81016495c312ca86f0480c /util/human/io.lua
parentf937942519d15530d2b5201e5460620bdb9a67dc (diff)
downloadprosody-403acbb836688f57e184b57b77f073a51b0dc4eb.tar.gz
prosody-403acbb836688f57e184b57b77f073a51b0dc4eb.zip
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
Diffstat (limited to 'util/human/io.lua')
-rw-r--r--util/human/io.lua12
1 files changed, 12 insertions, 0 deletions
diff --git a/util/human/io.lua b/util/human/io.lua
index bfbabafb..8a162059 100644
--- a/util/human/io.lua
+++ b/util/human/io.lua
@@ -108,6 +108,17 @@ if utf8.len and utf8.offset then
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 @@ return {
printf = printf;
padleft = padleft;
padright = padright;
+ term_width = term_width;
ellipsis = ellipsis;
table = new_table;
};