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,21 +185,24 @@ 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 =
try
(* terminfo *)
process_out "tput" ["cols"] |> String.trim |> int_of_string
with Failure _ -> (
if not Unix.(isatty stdin) then from_env ()
else
try
(* stty *)
process_out "stty" ["size"]
|> String.trim
|> 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))
(* terminfo *)
process_out "tput" ["cols"] |> String.trim |> int_of_string
with Failure _ -> (
try
(* stty *)
process_out "stty" ["size"]
|> String.trim
|> 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 _ -> from_env ())
in
if count > 0 then count else default
in