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/tree.ml.html

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

type t = { label : View_core.t; children : t list }

let v label children = { label; children }
let label t = t.label
let children t = t.children

(* The first line of a label uses the room beside the node's branch. Its
   other lines use the room beside its rails. A line wider than its room
   wraps, and the next lines start under its text. *)
let label_lines ~first ~rest label =
  List.concat
    (List.mapi
       (fun i line ->
         let room = if i = 0 then first else rest in
         if room <= 0 then [ View_core.empty ]
         else if View_core.width line <= room then [ line ]
         else View_core.wrap_hanging room line)
       (View_core.lines label))

let rec labels t = t.label :: List.concat_map labels t.children

let lines c ~guide ~indent t =
  let look = Ctx.look c in
  let guide = Widget.guide look guide in
  let style = Guide.style guide in
  let margin = Option.value (Ctx.width c) ~default:max_int in
  let pad = String.make indent ' ' in
  let acc = ref [] in
  (* A tree has few distinct guide strings, so each is measured once. *)
  let measured = Hashtbl.create 8 in
  let cells s =
    match Hashtbl.find_opt measured s with
    | Some n -> n
    | None ->
        let n = Widget.cells s in
        Hashtbl.add measured s n;
        n
  in
  (* A label line already fits its room, so a line is cut only when its
     guides alone are wider than the margin. *)
  let emit (prefix, cells) line =
    let line = View_core.(ink style prefix ++ line) in
    let line =
      if cells > margin then Widget.truncate look margin line else line
    in
    acc := line :: !acc
  in
  (* [lasts] has one flag for the node and for each ancestor below the root,
     innermost first. A flag says whether that node is the last of its
     siblings. *)
  let rec go lasts node =
    let lead = if lasts = [] then "" else pad in
    let first = lead ^ Widget.branch guide lasts
    and after = pad ^ Widget.rails guide lasts in
    let first_cells = cells first and after_cells = cells after in
    let room = margin - first_cells and rest = margin - after_cells in
    let label =
      View_core.resolve
        (Ctx.with_width (Option.map (fun _ -> room) (Ctx.width c)) c)
        node.label
    in
    (match label_lines ~first:room ~rest label with
    | [] -> ()
    | l :: ls ->
        emit (first, first_cells) l;
        List.iter (emit (after, after_cells)) ls);
    let n = List.length node.children in
    List.iteri (fun i child -> go ((i = n - 1) :: lasts) child) node.children
  in
  go [] t;
  List.rev !acc

let view ?guide ?(indent = 0) t =
  if indent < 0 then invalid_arg "Console.Tree.view: negative indent";
  let parts = labels t in
  View_core.deferred ~kind:(View_core.kinds parts) (fun c ->
      View_core.unlines (lines c ~guide ~indent t))