Skip to content

Commit

Permalink
Merge pull request #292 from hcarty/format-printers
Browse files Browse the repository at this point in the history
Add some Format-based container pretty printers to BatIO.Incubator
  • Loading branch information
thelema committed Sep 30, 2012
2 parents f4dff0e + 3a0dee8 commit 3c6b01e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/batIO.ml
Expand Up @@ -723,3 +723,102 @@ let string_of_t_printer p x =

let to_format printer =
fun fmt t -> Format.pp_print_string fmt (to_string printer t)

module Incubator = struct
module Array = struct
let pp ?(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 ()
end

module Enum = struct
let pp ?(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 BatEnum.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 BatEnum.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 ()
end

module List = struct
let pp ?(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
end
end
67 changes: 67 additions & 0 deletions src/batIO.mli
Expand Up @@ -929,3 +929,70 @@ val make_enum : (input -> 'a) -> input -> 'a BatEnum.t

val get_output_id : _ output -> int
val get_input_id : input -> int

module Incubator : sig
(** {6 Format-based pretty-printing} *)

module Array : sig
val pp :
?flush:bool ->
?first:string ->
?last:string ->
?sep:string ->
?indent:int ->
(Format.formatter -> 'a -> 'b) -> Format.formatter -> 'a array -> unit
(** Print the contents of an array, with [first] preceeding the first item
(default: ["\[|"]), [last] following the last item (default: ["|\]"])
and [sep] separating items (default: ["; "]). A printing function must
be provided to print the items in the array. The [flush] parameter
(default: [false]) should be set to [true] for the outer-most printing
call. Setting inner calls to [true] - for example, for nested values -
prevent indentation from working properly.
Example:
[pp ~flush:true Format.pp_print_int Format.std_formatter \[|1; 2; 3|\]]
*)
end

module Enum : sig
val pp :
?flush:bool ->
?first:string ->
?last:string ->
?sep:string ->
?indent:int ->
(Format.formatter -> 'a -> 'b) -> Format.formatter -> 'a BatEnum.t -> unit
(** Print the contents of an enum, with [first] preceeding the first item
(default: [""]), [last] following the last item (default: [""])
and [sep] separating items (default: [" "]). A printing function must
be provided to print the items in the enum. The [flush] parameter
(default: [false]) should be set to [true] for the outer-most printing
call. Setting inner calls to [true] - for example, for nested values -
prevent indentation from working properly.
Example:
[pp ~flush:true Format.pp_print_int Format.std_formatter (1 -- 3)] *)
end

module List : sig
val pp :
?flush:bool ->
?first:string ->
?last:string ->
?sep:string ->
?indent:int ->
(Format.formatter -> 'a -> 'b) -> Format.formatter -> 'a list -> unit
(** Print the contents of a list, with [first] preceeding the first item
(default: ["\["]), [last] following the last item (default: ["\]"])
and [sep] separating items (default: ["; "]). A printing function must
be provided to print the items in the list. The [flush] parameter
(default: [false]) should be set to [true] for the outer-most printing
call. Setting inner calls to [true] - for example, for nested values -
prevent indentation from working properly.
Example:
[pp ~flush:true Format.pp_print_int Format.std_formatter \[1; 2; 3\]]
*)
end
end

0 comments on commit 3c6b01e

Please sign in to comment.