Software /
code /
prosody
Comparison
util/human/io.lua @ 13210:8dbe693ded6b
util.human.io: Fix stray 'stty' error by only querying width of real ttys
This adds a dependency on a binary and *nix-specific module but then
stty is probably *nix-specific anyway so maybe that's fine.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 16 Jul 2023 21:21:37 +0200 |
parent | 13199:278920294dfe |
comparison
equal
deleted
inserted
replaced
13209:c8d949cf6b09 | 13210:8dbe693ded6b |
---|---|
1 local array = require "prosody.util.array"; | 1 local array = require "prosody.util.array"; |
2 local pposix = require "prosody.util.pposix"; | |
2 local utf8 = rawget(_G, "utf8") or require"prosody.util.encodings".utf8; | 3 local utf8 = rawget(_G, "utf8") or require"prosody.util.encodings".utf8; |
3 local len = utf8.len or function(s) | 4 local len = utf8.len or function(s) |
4 local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", ""); | 5 local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", ""); |
5 return count; | 6 return count; |
6 end; | 7 end; |
109 end | 110 end |
110 | 111 |
111 local function term_width(default) | 112 local function term_width(default) |
112 local env_cols = tonumber(os.getenv "COLUMNS"); | 113 local env_cols = tonumber(os.getenv "COLUMNS"); |
113 if env_cols then return env_cols; end | 114 if env_cols then return env_cols; end |
115 if not pposix.isatty(io.stdout) then | |
116 return default; | |
117 end | |
114 local stty = io.popen("stty -a"); | 118 local stty = io.popen("stty -a"); |
115 if not stty then return default; end | 119 if not stty then return default; end |
116 local result = stty:read("*a"); | 120 local result = stty:read("*a"); |
117 if result then | 121 if result then |
118 result = result:match("%f[%w]columns[ =]*(%d+)"); | 122 result = result:match("%f[%w]columns[ =]*(%d+)"); |