From 81e2d1810008ea62cff5d314df93bb23305c7045 Mon Sep 17 00:00:00 2001 From: Louis Gesbert Date: Thu, 27 Jun 2024 13:59:25 +0200 Subject: [PATCH] Don't try to read screen columns without a tty --- compiler/catala_utils/file.ml | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/compiler/catala_utils/file.ml b/compiler/catala_utils/file.ml index 02fbe6f1..f043b3df 100644 --- a/compiler/catala_utils/file.ml +++ b/compiler/catala_utils/file.ml @@ -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