package console

All checked builds pass
  1. Overview
  2. Docs
Colours, tables, trees and progress rows for the terminal

Install

dune-project
 Dependency

Authors

Maintainers

Sources

console-0.9.0.tbz
sha256=1c6851dbc38514bf717da9bbe9cddda3c2958b215f934dee69324f1ec243873f
sha512=71bd51d2cbaf945ae153e863c2de189b32f3049c750131cc32e96e43759e4d8b62f9bf8863ff2e80429d56d32209573b85d3da9e0d67ad52bb1106b4c763070f

doc/src/console.tty/console_tty.ml.html

Source file console_tty.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(*---------------------------------------------------------------------------
  Copyright (c) 2026 Thomas Gazagnaire. All rights reserved.
  SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

let getenv = Sys.getenv_opt
let outputs = [ (Fmt.stdout, Unix.stdout); (Fmt.stderr, Unix.stderr) ]

let set ?style_renderer ?utf_8 ?theme (ppf, fd) =
  let renderer =
    match style_renderer with
    | Some r -> r
    | None ->
        let is_tty = Unix.isatty fd || Option.is_some (Cast.file ()) in
        Colour.renderer ~getenv ~is_tty
  in
  Fmt.set_style_renderer ppf renderer;
  Fmt.set_utf_8 ppf (Option.value utf_8 ~default:(Colour.utf_8 ~getenv));
  Console.Color.set_depth ppf (Colour.depth ~getenv);
  Option.iter (Console.Theme.set ppf) theme

let setup ?style_renderer ?utf_8 ?theme () =
  List.iter (set ?style_renderer ?utf_8 ?theme) outputs;
  ignore (Recording.start ~time:(fun () -> 0.) Fmt.stdout : bool)

(* Fmt keeps what a program chose for a formatter in tag functions it
   installs on the first choice, so a formatter whose tag functions are still
   Format's own, but for its theme, has had nothing chosen for it. *)
let ensure () =
  List.iter (fun out -> if Slot.unset (fst out) then set out) outputs

let is_terminal fd = Unix.isatty fd && not (Colour.dumb ~getenv)