Source file ezPrintTree.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
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2020 OCamlPro & Origin Labs                               *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

type tree = Branch of string * tree list

let up_down = "│  "
let up_right_down = "├──"
let up_right = "└──"

let print_tree indent tree =

  let rec iter indent ~last = function
    | Branch (s, branches) ->
        Printf.printf "%s%s %s\n"
          indent
          (if last then up_right else up_right_down)
          s;
        iter_branches
          (indent ^ if last then "   " else up_down) branches

  and iter_branches indent = function
    | [] -> ()
    | [ branch ] -> iter ( indent ^ " " ) ~last:true branch
    | branch :: branches ->
      iter ( indent ^ " ") ~last:false branch;
      iter_branches indent branches
  in
  iter indent ~last:true tree