package printbox

  1. Overview
  2. Docs

Pretty-Printing of nested Boxes

Allows to print nested boxes, lists, arrays, tables in a nice way on any monospaced support.

 # let b = PrintBox.(
     frame
       (vlist [ line "hello";
                hlist [line "world"; line "yolo"]])
   );;
 val b : t = <abstr>

 # PrintBox.output ~indent:2 stdout b;;
 +----------+
 |hello     |
 |----------|
 |world|yolo|
 +----------+
 - : unit = ()

 # let b2 = PrintBox.(
     frame
       (hlist [ text "I love\nto\npress\nenter";
                grid_text [| [|"a"; "bbb"|];
                             [|"c"; "hello world"|] |]])
   );;
 val b2 : PrintBox.t = <abstr>

 # PrintBox.output stdout b2;;
 +--------------------+
 |I love|a|bbb        |
 |to    |-+-----------|
 |press |c|hello world|
 |enter | |           |
 +--------------------+

- : unit = ()
type position = {
  1. x : int;
  2. y : int;
}

Positions are relative to the upper-left corner, that is, when x increases we go toward the right, and when y increases we go toward the bottom (same order as a printer)

Box Combinators

type t =
  1. | Empty
  2. | Text of string
  3. | Frame of t
  4. | Pad of position * t
  5. | Grid of [ `Bars | `None ] * t array array
  6. | Tree of int * t * t array

A box, either empty, containing directly text, or a table or tree of sub-boxes

val empty : t

Empty box, of size 0

val line : string -> t

Make a single-line box.

val text : string -> t

Any text, possibly with several lines

val sprintf : ('a, Buffer.t, unit, t) format4 -> 'a

Formatting for text

val lines : string list -> t

Shortcut for text, with a list of lines

val int_ : int -> t
val bool_ : bool -> t
val float_ : float -> t
val frame : t -> t

Put a single frame around the box

val pad : t -> t

Pad the given box with some free space

val pad' : col:int -> lines:int -> t -> t

Pad with the given number of free cells for lines and columns

val vpad : int -> t -> t

Pad vertically

val hpad : int -> t -> t

Pad horizontally

val grid : ?pad:(t -> t) -> ?bars:bool -> t array array -> 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!

  • parameter framed

    if true, each item of the grid will be framed. default value is true

val grid_text : ?pad:(t -> t) -> ?bars:bool -> string array array -> 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 : ?bars:bool -> line:int -> col:int -> (line:int -> col:int -> t) -> t

Same as grid but takes the matrix as a function

val vlist : ?pad:(t -> t) -> ?bars:bool -> t list -> t

Vertical list of boxes

val hlist : ?pad:(t -> t) -> ?bars:bool -> t list -> t

Horizontal list of boxes

val grid_map : ?bars:bool -> ('a -> t) -> 'a array array -> t
val vlist_map : ?bars:bool -> ('a -> t) -> 'a list -> t
val hlist_map : ?bars:bool -> ('a -> t) -> 'a list -> t
val tree : ?indent:int -> t -> t list -> t

Tree structure, with a node label and a list of children nodes

val mk_tree : ?indent:int -> ('a -> t * 'a list) -> 'a -> t

Definition of a tree with a local function that maps nodes to their content and children

Simple Structural Interface

type 'a ktree = unit -> [ `Nil | `Node of 'a * 'a ktree list ]
type box = t
module Simple : sig ... end