package matrix

  1. Overview
  2. Docs

Module Matrix_cli.TreeSource

Trees, the guides that draw them one node per line.

A tree is drawn one node per line, depth first, as tree(1) lists a directory. A node below the roots stands behind one guide piece for each of its ancestors below the roots, outermost first, and one for itself. An ancestor's piece is a pipe when the ancestor has a sibling after it, since the line to that sibling passes this node, and a space otherwise. The node's own piece is a branch when it has a sibling after it and a last when it is the last of its siblings. A root stands behind no piece. The node's other lines, such as the rest of a label that wraps, stand behind its rails, the pieces of its ancestors followed by a pipe or a space for itself, so the lines to its later siblings and to its children go on beside them.

A node is told by its lasts, whether it and each of its ancestors below the roots is the last of its siblings, innermost first, [] for a root. Tree.fold gives every node its lasts, and Tree.prefix and Tree.rails turn them into text.

This prints a tree of strings:

type node = N of string * node list

let print roots =
  let open Matrix_cli in
  Tree.fold
    ~children:(fun (N (_, c)) -> c)
    (fun lasts (N (label, _)) () ->
      print_endline (Tree.prefix Tree.single lasts ^ label))
    roots ()
Sourcetype guide = {
  1. branch : string;
    (*

    Before a node with a sibling after it.

    *)
  2. last : string;
    (*

    Before the last of its siblings.

    *)
  3. pipe : string;
    (*

    Under an ancestor with a sibling after it.

    *)
  4. space : string;
    (*

    Under an ancestor without one.

    *)
}

The type for guide pieces. The four pieces must be equally wide, or the nodes of one depth do not line up.

Sourceval single : guide

single is the pieces of light lines "├── ", "└── ", "│ " and " ".

Sourceval ascii : guide

ascii is the pieces of ASCII characters "|-- ", "`-- ", "| " and " ", as tree --charset=ascii draws them.

Sourceval fold : children:('a -> 'a list) -> (bool list -> 'a -> 'acc -> 'acc) -> 'a list -> 'acc -> 'acc

fold ~children f roots acc folds f over roots and their descendants, depth first, each node with its lasts. A node's children are children node, so a collapsed node is one whose children is [].

Sourceval prefix : guide -> bool list -> string

prefix g lasts is the text before the first line of the node of lasts, its pieces in g.

Sourceval rails : guide -> bool list -> string

rails g lasts is the text before the other lines of the node of lasts, its rails in g.