Skip to content

Commit

Permalink
box names added in ocaml-ppx#1083
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Oct 17, 2019
1 parent d0dd119 commit c834404
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 54 deletions.
57 changes: 24 additions & 33 deletions src/Fmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,13 @@ let box_depth_colors = [|32; 33; 94; 31; 35; 36|]
let box_depth_color () =
box_depth_colors.(!box_depth % Array.length box_depth_colors)

let debug_box_open ?name box_kind n fs =
let debug_box_open box_kind n fs =
if !box_debug_enabled then (
let openning =
if n = 0 then box_kind else Format.sprintf "%s<%d" box_kind n
in
let name =
Option.value_map name ~f:(fun s -> Format.sprintf "<%s>" s) ~default:""
in
pp_color_k (box_depth_color ())
(fun fs -> Format.fprintf fs "@<0>[@<0>%s@<0>%s>" openning name)
(fun fs -> Format.fprintf fs "@<0>[@<0>%s@<0>>" openning)
fs ;
Int.incr box_depth )

Expand All @@ -188,20 +185,14 @@ let debug_box_close fs =
(fun fs -> Format.fprintf fs "@<0>]")
fs )

let open_box ?name n fs =
debug_box_open ?name "b" n fs ;
Format.pp_open_box fs n
let open_box n fs = debug_box_open "b" n fs ; Format.pp_open_box fs n

and open_vbox ?name n fs =
debug_box_open ?name "v" n fs ;
Format.pp_open_vbox fs n
and open_vbox n fs = debug_box_open "v" n fs ; Format.pp_open_vbox fs n

and open_hvbox ?name n fs =
debug_box_open ?name "hv" n fs ;
Format.pp_open_hvbox fs n
and open_hvbox n fs = debug_box_open "hv" n fs ; Format.pp_open_hvbox fs n

and open_hovbox ?name n fs =
debug_box_open ?name "hov" n fs ;
and open_hovbox n fs =
debug_box_open "hov" n fs ;
Format.pp_open_hovbox fs n

and close_box fs = debug_box_close fs ; Format.pp_close_box fs ()
Expand Down Expand Up @@ -242,45 +233,45 @@ module Safe = struct
~init:(One (f h))
~f:(fun acc x -> Cons (acc, sep, f x)))

type box_kind = ?name:string -> int -> t
type box_kind = int -> t

let cbox ?name = open_box ?name
let cbox = open_box

let vbox ?name = open_vbox ?name
let vbox = open_vbox

let hvbox ?name = open_hvbox ?name
let hvbox = open_hvbox

let hovbox ?name = open_hovbox ?name
let hovbox = open_hovbox

let box_if ?name ?(box_singleton = false) cnd kind n = function
let box_if ?(box_singleton = false) cnd kind n = function
| Empty -> noop
| Non_empty (One x) ->
wrap_if_k (box_singleton && cnd) (kind ?name n) close_box x
wrap_if_k (box_singleton && cnd) (kind n) close_box x
| Non_empty ne ->
let rec aux = function
| One t -> t
| Cons (b, s, t) -> aux b $ s $ t
in
wrap_if_k cnd (kind ?name n) close_box (aux ne)
wrap_if_k cnd (kind n) close_box (aux ne)

let box ?name ?box_singleton = box_if ?name ?box_singleton true
let box ?box_singleton = box_if ?box_singleton true
end

let cbox ?name n = wrap_k (open_box ?name n) close_box
let cbox n = wrap_k (open_box n) close_box

and vbox ?name n = wrap_k (open_vbox ?name n) close_box
and vbox n = wrap_k (open_vbox n) close_box

and hvbox ?name n = wrap_k (open_hvbox ?name n) close_box
and hvbox n = wrap_k (open_hvbox n) close_box

and hovbox ?name n = wrap_k (open_hovbox ?name n) close_box
and hovbox n = wrap_k (open_hovbox n) close_box

and cbox_if ?name cnd n = wrap_if_k cnd (open_box ?name n) close_box
and cbox_if cnd n = wrap_if_k cnd (open_box n) close_box

and vbox_if ?name cnd n = wrap_if_k cnd (open_vbox ?name n) close_box
and vbox_if cnd n = wrap_if_k cnd (open_vbox n) close_box

and hvbox_if ?name cnd n = wrap_if_k cnd (open_hvbox ?name n) close_box
and hvbox_if cnd n = wrap_if_k cnd (open_hvbox n) close_box

and hovbox_if ?name cnd n = wrap_if_k cnd (open_hovbox ?name n) close_box
and hovbox_if cnd n = wrap_if_k cnd (open_hovbox n) close_box

(** Text filling --------------------------------------------------------*)

Expand Down
34 changes: 13 additions & 21 deletions src/Fmt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ val with_box_debug : t -> t
(** Represent boxes inside a format thunk with colored brackets. For debug
purposes *)

val open_vbox : ?name:string -> int -> t
val open_vbox : int -> t
(** Open an vbox with specified indentation. *)

val open_hvbox : ?name:string -> int -> t
val open_hvbox : int -> t
(** Open an hvbox with specified indentation. *)

val open_hovbox : ?name:string -> int -> t
val open_hovbox : int -> t
(** Open an hovbox with specified indentation. *)

val close_box : t
Expand Down Expand Up @@ -225,46 +225,38 @@ module Safe : sig
(** [of_list xs s] generates a boxed value containing the elements of the
list [xs] each separated with separator [s]. *)

val box :
?name:string -> ?box_singleton:bool -> box_kind -> int -> boxed -> t
val box : ?box_singleton:bool -> box_kind -> int -> boxed -> t
(** Wrap a format thunk with a box with specified indentation. *)

val box_if :
?name:string
-> ?box_singleton:bool
-> bool
-> box_kind
-> int
-> boxed
-> t
val box_if : ?box_singleton:bool -> bool -> box_kind -> int -> boxed -> t
(** Conditionally wrap a format thunk with a box with specified
indentation. *)
end

val cbox : ?name:string -> int -> t -> t
val cbox : int -> t -> t
(** Wrap a format thunk with a compacting box with specified indentation. *)

val vbox : ?name:string -> int -> t -> t
val vbox : int -> t -> t
(** Wrap a format thunk with a vbox with specified indentation. *)

val hvbox : ?name:string -> int -> t -> t
val hvbox : int -> t -> t
(** Wrap a format thunk with an hvbox with specified indentation. *)

val hovbox : ?name:string -> int -> t -> t
val hovbox : int -> t -> t
(** Wrap a format thunk with an hovbox with specified indentation. *)

val cbox_if : ?name:string -> bool -> int -> t -> t
val cbox_if : bool -> int -> t -> t
(** Conditionally wrap a format thunk with a compacting sbox with specified
indentation. *)

val vbox_if : ?name:string -> bool -> int -> t -> t
val vbox_if : bool -> int -> t -> t
(** Conditionally wrap a format thunk with a vbox with specified indentation. *)

val hvbox_if : ?name:string -> bool -> int -> t -> t
val hvbox_if : bool -> int -> t -> t
(** Conditionally wrap a format thunk with an hvbox with specified
indentation. *)

val hovbox_if : ?name:string -> bool -> int -> t -> t
val hovbox_if : bool -> int -> t -> t
(** Conditionally wrap a format thunk with an hovbox with specified
indentation. *)

Expand Down

0 comments on commit c834404

Please sign in to comment.