package matrix
Build not yet checkedInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=a92cbbcbe016fb19fb0a749222da0e459a9567aef3f4c7340d9bd6b71c53ed6f
sha512=6a7ab054605538bb31a2b73a1a6d093981a2704c9f0af5852221c05735290d74aca7c7282298c4d0f91e86a800e2ccd94b4918af883cd963449c758eb6b272b3
doc/matrix.cli/Matrix_cli/Tree/index.html
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 ()type guide = {branch : string;(*Before a node with a sibling after it.
*)last : string;(*Before the last of its siblings.
*)pipe : string;(*Under an ancestor with a sibling after it.
*)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.
ascii is the pieces of ASCII characters "|-- ", "`-- ", "| " and " ", as tree --charset=ascii draws them.
val fold :
children:('a -> 'a list) ->
(bool list -> 'a -> 'acc -> 'acc) ->
'a list ->
'acc ->
'accfold ~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 [].
prefix g lasts is the text before the first line of the node of lasts, its pieces in g.