File: cpdfprinttree.ml

package info (click to toggle)
cpdf 2.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,828 kB
  • sloc: ml: 34,724; makefile: 65; sh: 45
file content (24 lines) | stat: -rw-r--r-- 873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(* Courtesy Martin Jambon, in the public domain. *)
let rec titer f = function
  | [] -> ()
  | [x] -> f true x
  | x :: tl -> f false x; titer f tl

let to_buffer ?(line_prefix = "") ~get_name ~get_children buf x =
  let rec print_root indent x =
    Printf.bprintf buf "%s\n" (get_name x);
    let children = get_children x in
      titer (print_child indent) children
  and print_child indent is_last x =
    let line = if is_last then "└── " else "├── " in
      Printf.bprintf buf "%s%s" indent line;
      let extra_indent = if is_last then "    " else "│   " in
        print_root (indent ^ extra_indent) x
  in
    Buffer.add_string buf line_prefix;
    print_root line_prefix x

let to_string ?line_prefix ~get_name ~get_children x =
  let buf = Buffer.create 1000 in
    to_buffer ?line_prefix ~get_name ~get_children buf x;
    Buffer.contents buf