Changeset

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
parents 13043:d4f7118d1531
children 13045:da0b3cb9a2ec
files util/human/io.lua
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
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;
 };