package printbox

  1. Overview
  2. Docs

Module PrintBoxSource

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 = ()
Sourcetype 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

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

The type t is now opaque

  • since 0.2 .

The type view can be used to observe the inside of the box.

Sourceval view : t -> view

Observe the content of the box.

  • since 0.2

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

Sourceval empty : t

Empty box, of size 0

Sourceval line : string -> t

Make a single-line box.

Sourceval text : string -> t

Any text, possibly with several lines

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

Formatting for text

Sourceval asprintf : ('a, Format.formatter, unit, t) format4 -> 'a

Formatting for text.

  • since 0.2
Sourceval lines : string list -> t

Shortcut for text, with a list of lines. lines l is the same as text (String.concat "\n" l).

Sourceval int_ : int -> t
Sourceval bool_ : bool -> t
Sourceval float_ : float -> t
Sourceval int : int -> t
  • since 0.2
Sourceval bool : bool -> t
  • since 0.2
Sourceval float : float -> t
  • since 0.2
Sourceval frame : t -> t

Put a single frame around the box

Sourceval pad : t -> t

Pad the given box with some free space

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

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

Sourceval vpad : int -> t -> t

Pad vertically

Sourceval hpad : int -> t -> t

Pad horizontally

Sourceval 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

Sourceval grid_text : ?pad:(t -> t) -> ?bars:bool -> string array array -> t

Same as grid, but wraps every cell into a text box

Sourceval transpose : 'a array array -> 'a array array

Transpose a matrix

Sourceval init_grid : ?bars:bool -> line:int -> col:int -> (line:int -> col:int -> t) -> t

Same as grid but takes the matrix as a function

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

Vertical list of boxes

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

Horizontal list of boxes

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

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

Sourceval 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

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