Don't try to read screen columns without a tty

This commit is contained in:
Louis Gesbert 2024-06-27 13:59:25 +02:00
parent 2d756698fb
commit 81e2d18100

View File

@ -185,7 +185,12 @@ let process_out ?check_exit cmd args =
let () =
let default = 80 in
let get_terminal_cols () =
let from_env () =
try int_of_string (Sys.getenv "COLUMNS") with Not_found | Failure _ -> 0
in
let count =
if not Unix.(isatty stdin) then from_env ()
else
try
(* terminfo *)
process_out "tput" ["cols"] |> String.trim |> int_of_string
@ -197,9 +202,7 @@ let () =
|> fun s ->
let i = String.rindex s ' ' + 1 in
String.sub s i (String.length s - i) |> int_of_string
with Failure _ | Not_found | Invalid_argument _ -> (
try int_of_string (Sys.getenv "COLUMNS")
with Not_found | Failure _ -> 0))
with Failure _ | Not_found | Invalid_argument _ -> from_env ())
in
if count > 0 then count else default
in