[term] do not apply terminal colour codes if the output is not a tty

Summary:
Wrap ANSITerminal so that it doesn't apply control codes unless they can be
interpreted, and so that clients are unable to call ANSITerminal directly.

Reviewed By: mbouaziz

Differential Revision: D5953733

fbshipit-source-id: 6b3602a
master
Jules Villard 7 years ago committed by Facebook Github Bot
parent 67b08f666f
commit b8a4147744

@ -101,3 +101,19 @@ let invalid_argf _ : [`use_Logging_die_instead] = assert false
and running certain functions that may in turn invoke exit, and you want to handle the execution and running certain functions that may in turn invoke exit, and you want to handle the execution
flow differently - like invoking certain callbacks before exiting, or not exiting at all. *) flow differently - like invoking certain callbacks before exiting, or not exiting at all. *)
let exit = `In_general_prefer_using_Logging_exit_over_Pervasives_exit let exit = `In_general_prefer_using_Logging_exit_over_Pervasives_exit
module ANSITerminal : module type of ANSITerminal = struct
include ANSITerminal
let print_string =
if Unix.(isatty stdout) then print_string else fun _ -> Pervasives.print_string
let prerr_string =
if Unix.(isatty stderr) then prerr_string else fun _ -> Pervasives.prerr_string
let printf styles fmt = Format.ksprintf (fun s -> print_string styles s) fmt
let eprintf styles fmt = Format.ksprintf (fun s -> prerr_string styles s) fmt
let sprintf = if Unix.(isatty stderr) then sprintf else fun _ -> Printf.sprintf
end

Loading…
Cancel
Save