Source file console_intf.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
open! Core

module type%template
  [@modality (p, c) = ((nonportable, uncontended), (portable, contended))] Io = sig
  type 'a t
  type 'a fmt
  type out_channel : value mod c p

  include Monad.S with type 'a t := 'a t

  val output_string : out_channel -> string -> unit
  val output : out_channel -> buf:bytes -> pos:int -> len:int -> unit
  val stderr : out_channel
  val stdout : out_channel
  val flush : out_channel -> unit t
  val print_string : string -> unit
  val fprintf : attrs:string -> out_channel -> 'a fmt -> 'a
  val fold_left : 'a List.t -> init:'b -> f:('b -> 'a -> 'b t) -> 'b t
  val stdout_isatty : unit -> bool t
  val capable : unit -> bool t
end

module type S = sig
  type 'a io
  type 'a io_fmt
  type out_channel

  (** Handling of ansi codes. *)
  module Ansi : sig
    val kill_line : unit -> unit
    val erase_to_end_of_screen : unit -> unit
    val erase_to_start_of_screen : unit -> unit
    val erase_all : unit -> unit
    val bell : unit -> unit
    val home : unit -> unit
    val home_cursor : unit -> unit
    val cursor_up : unit -> unit
    val cursor_down : unit -> unit
    val cursor_backward : unit -> unit
    val cursor_forward : unit -> unit
    val save_cursor : unit -> unit
    val unsave_cursor : unit -> unit

    type attr = Ansi_kernel.Attr.t

    (** [printf] and [eprintf] do not emit OSC 8 hyperlink escapes: any [`Url _]
        attributes in [style] are silently dropped. Use [string_with_attr] or
        [output_string] if you need hyperlink support. *)

    val printf : attr list -> 'a io_fmt -> 'a
    val eprintf : attr list -> 'a io_fmt -> 'a

    (** [output_string] and [output] honor [`Url _] attrs by wrapping the output in OSC 8
        hyperlink escapes when the terminal is color-capable. *)

    val output_string : attr list -> out_channel -> string -> unit io
    val output : attr list -> out_channel -> Bytes.t -> int -> int -> unit io

    (** Create a string with embedded SGR and OSC 8 formatting codes.

        See {!Ansi_kernel.Attr.t} for the semantics of each attribute; in particular,
        [`Url _] wraps [string] in an OSC 8 hyperlink escape.

        Raises if the URL contains control characters. *)
    val string_with_attr : attr list -> string -> string @@ portable
  end

  val is_color_tty : unit -> bool io

  (** The width in characters of the current output. Returns [`Not_a_tty] if stdout is not
      connected to a tty. *)
  val width : unit -> [ `Cols of int | `Not_a_tty | `Not_available ] io

  (** print a list in a columnize way (like the output of ls) *)
  val print_list : out_channel -> (string * Ansi.attr list) list -> unit io
end

(** Color printing in terminals *)
module type Console = sig @@ portable
  module type Io = Io
  module type S = S

  module%template.portable [@modality p] Make (Io : Io [@modality p]) :
    S
    with type 'a io := 'a Io.t
     and type 'a io_fmt := 'a Io.fmt
     and type out_channel := Io.out_channel

  include
    S
    with type 'a io := 'a
     and type 'a io_fmt := ('a, Out_channel.t, unit) format
     and type out_channel := Out_channel.t
end