From 10a1a0643e900afe9fc175e846f736997bdf6ecc Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 25 Jun 2014 03:27:30 +0200 Subject: [PATCH] minor updates (new functions in CCPrint and PrintBox) --- core/CCPrint.ml | 18 ++++++++++++++++-- core/CCPrint.mli | 3 +++ misc/printBox.ml | 19 +++++++++++++++++++ misc/printBox.mli | 12 ++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/core/CCPrint.ml b/core/CCPrint.ml index e7648fce..5e8b9fef 100644 --- a/core/CCPrint.ml +++ b/core/CCPrint.ml @@ -122,9 +122,23 @@ let fprintf oc format = let buffer = Buffer.create 64 in Printf.kbprintf (fun fmt -> Buffer.output_buffer oc buffer) - buffer - format + buffer + format let printf format = fprintf stdout format let eprintf format = fprintf stderr format +let _with_file_out filename f = + let oc = open_out filename in + begin try + let x = f oc in + flush oc; + close_out oc; + x + with e -> + close_out_noerr oc; + raise e + end + +let to_file filename format = + _with_file_out filename (fun oc -> fprintf oc format) diff --git a/core/CCPrint.mli b/core/CCPrint.mli index e3e1109d..75b282e1 100644 --- a/core/CCPrint.mli +++ b/core/CCPrint.mli @@ -69,5 +69,8 @@ val sprintf : ('a, Buffer.t, unit, string) format4 -> 'a val fprintf : out_channel -> ('a, Buffer.t, unit, unit) format4 -> 'a (** Print on a channel *) +val to_file : string -> ('a, Buffer.t, unit, unit) format4 -> 'a + (** Print to the given file *) + val printf : ('a, Buffer.t, unit, unit) format4 -> 'a val eprintf : ('a, Buffer.t, unit, unit) format4 -> 'a diff --git a/misc/printBox.ml b/misc/printBox.ml index de703a11..4abada71 100644 --- a/misc/printBox.ml +++ b/misc/printBox.ml @@ -359,6 +359,25 @@ let _write_hline ~out pos n = Output.put_char out (_move_x pos i) '-' done +type simple_box = + [ `Empty + | `Pad of simple_box + | `Text of string + | `Vlist of simple_box list + | `Hlist of simple_box list + | `Table of simple_box array array + | `Tree of simple_box * simple_box list + ] + +let rec of_simple = function + | `Empty -> empty + | `Pad b -> pad (of_simple b) + | `Text t -> pad (text t) + | `Vlist l -> vlist (List.map of_simple l) + | `Hlist l -> hlist (List.map of_simple l) + | `Table a -> grid (Box._map_matrix of_simple a) + | `Tree (b,l) -> tree (of_simple b) (List.map of_simple l) + (* render given box on the output, starting with upper left corner at the given position. [expected_size] is the size of the available surrounding space. [offset] is the offset of the box diff --git a/misc/printBox.mli b/misc/printBox.mli index a22e4320..e769915a 100644 --- a/misc/printBox.mli +++ b/misc/printBox.mli @@ -182,6 +182,18 @@ val mk_tree : ?indent:int -> ('a -> Box.t * 'a list) -> 'a -> Box.t (** Definition of a tree with a local function that maps nodes to their content and children *) +type simple_box = + [ `Empty + | `Pad of simple_box + | `Text of string + | `Vlist of simple_box list + | `Hlist of simple_box list + | `Table of simple_box array array + | `Tree of simple_box * simple_box list + ] + +val of_simple : simple_box -> Box.t + (** {2 Rendering} *) val render : Output.t -> Box.t -> unit