package console
Module Console_ttySource
Colour and UTF-8 on the standard outputs.
Console_tty decides how Fmt.stdout and Fmt.stderr print, from the terminal and the environment, as the OCaml command-line tools do. It decides whether they are in colour, in how many colours, and whether they show UTF-8. A program calls setup once, first, and then prints with Fmt:
let () =
Console_tty.setup ();
Fmt.pr "%a@." Console.View.pp
Console.View.(styled Console.Style.bold (text "ready"))This library is console.tty, which needs Unix but not Eio.
val setup :
?style_renderer:Fmt.style_renderer ->
?utf_8:bool ->
?theme:Console.Theme.t ->
unit ->
unitsetup () sets the style renderer, the Fmt.utf_8, the Console.Color.depth and the Console.Theme of Fmt.stdout and of Fmt.stderr:
- The style renderer is
style_rendererif given, asFmt_cli.style_rendererreads a--coloroption. Otherwise it is`NoneifNO_COLORis set and not empty, or ifTERMis unset, empty ordumb. Else it is`Ansi_ttyiff the output is a terminal, as both are whenCONSOLE_CASTrecords them.CLICOLOR_FORCEandFORCE_COLORare not read:--color=alwaysforces colour. Fmt.utf_8isutf_8if given, elsetrueiff the locale's character set is UTF-8. The locale is the first ofLC_ALL,LC_CTYPEandLANGthat is set and not empty. An unset locale is not UTF-8, and the widgets print ASCII alone on it.- The colour depth is
`True_colorifCOLORTERMistruecoloror24bit, or ifTERMends in-direct. It is`Ansi_256ifTERMcontains256color, and`Ansi_16otherwise. - The theme is
themeif given, asCmdliner.Arg.enum Console.Theme.allreads a--themeoption. Otherwise each output keeps its own.
CONSOLE_CAST, set to a file name, makes setup record both outputs there, as Console_eio.run does, so that a program that prints without a run is recorded too: each write is at time zero, until a run gives the recording its clock.
ensure () is like setup () (setup) but leaves alone each of Fmt.stdout and Fmt.stderr on which the program chose something already. Such a choice is a style renderer or Fmt.utf_8, set with Fmt, Fmt_tty or setup, or a Console.Color.depth. A Console.Theme is not one.
A library that prints in colour on the standard outputs calls ensure, as Console_eio.run does. A program that did not call setup still gets colour, and one that did keeps its choice.
is_terminal fd is true iff fd is a terminal and TERM is set, not empty and not dumb. On such an output a program can move the cursor to redraw what it printed.