Make sure to check whether the given output channel is a tty before sending ANSI terminal codes to it, instead of always check what stdout is. --- mllib/common_utils.ml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml index 9fcd8dd..83ebd3a 100644 --- a/mllib/common_utils.ml +++ b/mllib/common_utils.ml @@ -205,17 +205,20 @@ let rec combine3 xs ys zs = | x::xs, y::ys, z::zs -> (x, y, z) :: combine3 xs ys zs | _ -> invalid_arg "combine3" +let istty chan = + Unix.isatty (Unix.descr_of_out_channel chan) + (* ANSI terminal colours. *) let ansi_green ?(chan = stdout) () = - if Unix.isatty Unix.stdout then output_string chan "\x1b[0;32m" + if istty chan then output_string chan "\x1b[0;32m" let ansi_red ?(chan = stdout) () = - if Unix.isatty Unix.stdout then output_string chan "\x1b[1;31m" + if istty chan then output_string chan "\x1b[1;31m" let ansi_blue ?(chan = stdout) () = - if Unix.isatty Unix.stdout then output_string chan "\x1b[1;34m" + if istty chan then output_string chan "\x1b[1;34m" let ansi_magenta ?(chan = stdout) () = - if Unix.isatty Unix.stdout then output_string chan "\x1b[1;35m" + if istty chan then output_string chan "\x1b[1;35m" let ansi_restore ?(chan = stdout) () = - if Unix.isatty Unix.stdout then output_string chan "\x1b[0m" + if istty chan then output_string chan "\x1b[0m" (* Timestamped progress messages, used for ordinary messages when not * --quiet. -- 1.9.3 _______________________________________________ Libguestfs mailing list [email protected] https://www.redhat.com/mailman/listinfo/libguestfs
