Created
September 26, 2012 16:52
-
-
Save hcarty/3789149 to your computer and use it in GitHub Desktop.
BatEnum, BatArray, BatList pretty printers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pp_enum ?(flush = false) ?(first = "") ?(last = "") ?(sep = " ") ?(indent = String.length first) pp f e = | |
let open Format in | |
pp_open_box f indent; | |
pp_print_cut f (); | |
pp_print_string f first; | |
pp_print_cut f (); | |
match Enum.get e with | |
| None -> | |
pp_print_string f last; | |
pp_close_box f (); | |
if flush then pp_print_flush f () | |
| Some x -> | |
pp_open_box f indent; | |
pp f x; | |
let rec aux () = | |
match Enum.get e with | |
| None -> | |
pp_close_box f (); | |
pp_print_cut f (); | |
pp_print_string f last; | |
pp_close_box f (); | |
if flush then pp_print_flush f () | |
| Some x -> | |
pp_print_string f sep; | |
pp_close_box f (); | |
pp_print_cut f (); | |
pp_open_box f indent; | |
pp f x; | |
aux () | |
in | |
aux () | |
let pp_array ?(flush = false) ?(first = "[|") ?(last = "|]") ?(sep = "; ") ?(indent = String.length first) pp f a = | |
let open Format in | |
pp_open_box f indent; | |
pp_print_cut f (); | |
pp_print_string f first; | |
pp_print_cut f (); | |
for i = 0 to Array.length a - 2 do | |
pp_open_box f indent; | |
pp f a.(i); | |
pp_print_string f sep; | |
pp_close_box f (); | |
pp_print_cut f (); | |
done; | |
if Array.length a > 0 then ( | |
(* Print the last element without a trailing separator *) | |
pp_open_box f indent; | |
pp f a.(Array.length a - 1); | |
pp_close_box f (); | |
); | |
pp_print_cut f (); | |
pp_print_string f last; | |
pp_close_box f (); | |
if flush then pp_print_flush f () | |
let pp_list ?(flush = false) ?(first = "[") ?(last = "]") ?(sep = "; ") ?(indent = String.length first) pp f l = | |
let open Format in | |
pp_open_box f indent; | |
pp_print_cut f (); | |
pp_print_string f first; | |
pp_print_cut f (); | |
match l with | |
| [] -> | |
pp_print_string f last; | |
pp_close_box f (); | |
if flush then pp_print_flush f () | |
| hd :: tl -> | |
pp_open_box f indent; | |
pp f hd; | |
let rec aux rem = | |
match rem with | |
| [] -> | |
pp_close_box f (); | |
pp_print_cut f (); | |
pp_print_string f last; | |
pp_close_box f (); | |
if flush then pp_print_flush f () | |
| hd :: tl -> | |
pp_print_string f sep; | |
pp_close_box f (); | |
pp_print_cut f (); | |
pp_open_box f indent; | |
pp f hd; | |
aux tl | |
in | |
aux tl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment