package console

Build not yet checked
  1. Overview
  2. Docs
Colours, tables, trees and progress rows for the terminal

Install

dune-project
 Dependency

Authors

Maintainers

Sources

console-0.10.1.tbz
sha256=37ad65f6833c65814b0673f009f6b656c5194608ca03519da2e63e1051d54f6e
sha512=7677067f1a3244779cdbc5c90a8b0e1eba30c3fd58281584ea3d4c46c3e219eef0668588ca0e2938799c61a3fa831ce2146eac73717214d7baf96b497bfcd892

doc/src/console/slot.ml.html

Source file slot.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
(*---------------------------------------------------------------------------
  Copyright (c) 2026 Thomas Gazagnaire. All rights reserved.
  SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

type 'a op = Get of 'a option ref | Set of 'a

type 'a t = {
  wrap : 'a op -> Format.stag;
  unwrap : Format.stag -> 'a op option;
  setting : bool;
  watched : bool;
}

let v (type a) ?(setting = true) ?(watched = false) () : a t =
  let module M = struct
    type Format.stag += Op of a op
  end in
  {
    wrap = (fun op -> M.Op op);
    unwrap = (function M.Op op -> Some op | _ -> None);
    setting;
    watched;
  }

type mark = Format.stag -> string

(* A slot's tag function answers [Probe r] by storing itself in [r], and
   whether only non-setting slots lie below it. [unset] uses this to look
   through such slots. *)
type Format.stag += Probe of (mark * bool) option ref

let format_marks =
  (Format.pp_get_formatter_stag_functions
     (Format.formatter_of_buffer (Buffer.create 0))
     ())
    .mark_open_stag

let unset_marks (f : mark) =
  f == format_marks
  ||
  let r = ref None in
  ignore (f (Probe r));
  match !r with Some (g, unset) -> g == f && unset | None -> false

let unset ppf =
  unset_marks (Format.pp_get_formatter_stag_functions ppf ()).mark_open_stag

(* The string that a slot's tag function returns for its own tags. No other
   tag function returns it, so [ask] can tell whether a slot answered. *)
let ack = "\000console.slot"

let ask s ppf op =
  let funs = Format.pp_get_formatter_stag_functions ppf () in
  String.equal (funs.mark_open_stag (s.wrap op)) ack

let find s ppf =
  let r = ref None in
  ignore (ask s ppf (Get r));
  !r

let sets = Atomic.make 0
let generation () = Atomic.get sets
let changed () = Atomic.incr sets

let set s ppf v =
  if s.watched then Atomic.incr sets;
  if not (ask s ppf (Set v)) then begin
    let current = ref v in
    let funs = Format.pp_get_formatter_stag_functions ppf () in
    let other = funs.mark_open_stag in
    let unset = (not s.setting) && unset_marks other in
    let rec mark_open_stag stag =
      match stag with
      | Probe r ->
          r := Some (mark_open_stag, unset);
          ack
      | stag -> (
          match s.unwrap stag with
          | Some (Get r) ->
              r := Some !current;
              ack
          | Some (Set v) ->
              current := v;
              ack
          | None -> other stag)
    in
    Format.pp_set_formatter_stag_functions ppf { funs with mark_open_stag }
  end