mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-24 10:06:40 -05:00
allow padding in PrintBox
This commit is contained in:
parent
5422a6ad65
commit
13634950b1
2 changed files with 41 additions and 20 deletions
|
|
@ -153,6 +153,7 @@ module Box = struct
|
|||
| Line of string
|
||||
| Text of string list (* in a box *)
|
||||
| Frame of 'a
|
||||
| Pad of int * 'a
|
||||
| Grid of grid_shape * 'a array array
|
||||
|
||||
type t = {
|
||||
|
|
@ -221,6 +222,10 @@ module Box = struct
|
|||
| Frame t ->
|
||||
let {x;y} = size t in
|
||||
{ x=x+2; y=y+2; }
|
||||
| Pad (n, b') ->
|
||||
assert (n>0);
|
||||
let {x;y} = size b' in
|
||||
{ x=x+2*n; y=y+2*n; }
|
||||
| Grid (_,m) ->
|
||||
let dim = _dim_matrix m in
|
||||
let lines, columns = _size_matrix m in
|
||||
|
|
@ -250,25 +255,32 @@ let bool_ x = line (string_of_bool x)
|
|||
let frame b =
|
||||
Box._make (Box.Frame b)
|
||||
|
||||
let grid ?(framed=true) m =
|
||||
Box._make (Box.Grid ((if framed then Box.GridFramed else Box.GridBase), m))
|
||||
let pad' n b =
|
||||
assert (n>=0);
|
||||
if n=0 then b else Box._make (Box.Pad (n, b))
|
||||
|
||||
let init_grid ?framed ~line ~col f =
|
||||
let pad b = pad' 1 b
|
||||
|
||||
let grid ?(pad=false) ?(framed=true) m =
|
||||
let b = Box._make (Box.Grid ((if framed then Box.GridFramed else Box.GridBase), m)) in
|
||||
if pad then pad' 1 b else b
|
||||
|
||||
let init_grid ?pad ?framed ~line ~col f =
|
||||
let m = Array.init line (fun j-> Array.init col (fun i -> f ~line:j ~col:i)) in
|
||||
grid ?framed m
|
||||
grid ?pad ?framed m
|
||||
|
||||
let vlist ?framed l =
|
||||
let vlist ?pad ?framed l =
|
||||
let a = Array.of_list l in
|
||||
grid ?framed (Array.map (fun line -> [| line |]) a)
|
||||
grid ?pad ?framed (Array.map (fun line -> [| line |]) a)
|
||||
|
||||
let hlist ?framed l =
|
||||
grid ?framed [| Array.of_list l |]
|
||||
let hlist ?pad ?framed l =
|
||||
grid ?pad ?framed [| Array.of_list l |]
|
||||
|
||||
let hlist_map ?framed f l = hlist ?framed (List.map f l)
|
||||
let vlist_map ?framed f l = vlist ?framed (List.map f l)
|
||||
let grid_map ?framed f m = grid ?framed (Array.map (Array.map f) m)
|
||||
let hlist_map ?pad ?framed f l = hlist ?pad ?framed (List.map f l)
|
||||
let vlist_map ?pad ?framed f l = vlist ?pad ?framed (List.map f l)
|
||||
let grid_map ?pad ?framed f m = grid ?pad ?framed (Array.map (Array.map f) m)
|
||||
|
||||
let grid_text ?framed m = grid_map ?framed text m
|
||||
let grid_text ?pad ?framed m = grid_map ?pad ?framed text m
|
||||
|
||||
let transpose m =
|
||||
let dim = Box._dim_matrix m in
|
||||
|
|
@ -307,6 +319,9 @@ let rec _render ?expected_size ~out b pos =
|
|||
_write_vline out (_move_y pos 1) y;
|
||||
_write_vline out (_move pos (x+1) 1) y;
|
||||
_render ~out b' (_move pos 1 1)
|
||||
| Box.Pad (n, b') ->
|
||||
let expected_size = Box.size b in
|
||||
_render ~expected_size ~out b' (_move pos n n)
|
||||
| Box.Grid (grid_shape,m) ->
|
||||
let dim = Box._dim_matrix m in
|
||||
let lines, columns = Box._size_matrix m in
|
||||
|
|
|
|||
|
|
@ -121,34 +121,40 @@ val float_ : float -> Box.t
|
|||
val frame : Box.t -> Box.t
|
||||
(** Put a single frame around the box *)
|
||||
|
||||
val grid : ?framed:bool -> Box.t array array -> Box.t
|
||||
val pad : Box.t -> Box.t
|
||||
(** Pad the given box with some free space *)
|
||||
|
||||
val pad' : int -> Box.t -> Box.t
|
||||
(** Pad with the given number of free cells *)
|
||||
|
||||
val grid : ?pad:bool -> ?framed:bool -> Box.t array array -> Box.t
|
||||
(** Grid of boxes (no frame between boxes). The matrix is indexed
|
||||
with lines first, then columns. The array must be a proper matrix,
|
||||
that is, all lines must have the same number of columns!
|
||||
@param framed if [true], each item of the grid will be framed.
|
||||
default value is [true] *)
|
||||
|
||||
val grid_text : ?framed:bool -> string array array -> Box.t
|
||||
val grid_text : ?pad:bool -> ?framed:bool -> string array array -> Box.t
|
||||
(** Same as {!grid}, but wraps every cell into a {!text} box *)
|
||||
|
||||
val transpose : 'a array array -> 'a array array
|
||||
(** Transpose a matrix *)
|
||||
|
||||
val init_grid : ?framed:bool ->
|
||||
val init_grid : ?pad:bool -> ?framed:bool ->
|
||||
line:int -> col:int -> (line:int -> col:int -> Box.t) -> Box.t
|
||||
(** Same as {!grid} but takes the matrix as a function *)
|
||||
|
||||
val vlist : ?framed:bool -> Box.t list -> Box.t
|
||||
val vlist : ?pad:bool -> ?framed:bool -> Box.t list -> Box.t
|
||||
(** Vertical list of boxes *)
|
||||
|
||||
val hlist : ?framed:bool -> Box.t list -> Box.t
|
||||
val hlist : ?pad:bool -> ?framed:bool -> Box.t list -> Box.t
|
||||
(** Horizontal list of boxes *)
|
||||
|
||||
val grid_map : ?framed:bool -> ('a -> Box.t) -> 'a array array -> Box.t
|
||||
val grid_map : ?pad:bool -> ?framed:bool -> ('a -> Box.t) -> 'a array array -> Box.t
|
||||
|
||||
val vlist_map : ?framed:bool -> ('a -> Box.t) -> 'a list -> Box.t
|
||||
val vlist_map : ?pad:bool -> ?framed:bool -> ('a -> Box.t) -> 'a list -> Box.t
|
||||
|
||||
val hlist_map : ?framed:bool -> ('a -> Box.t) -> 'a list -> Box.t
|
||||
val hlist_map : ?pad:bool -> ?framed:bool -> ('a -> Box.t) -> 'a list -> Box.t
|
||||
|
||||
val render : Output.t -> Box.t -> unit
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue