package printbox

  1. Overview
  2. Docs
Allows to print nested boxes, lists, arrays, tables in several formats

Install

Dune Dependency

Authors

Maintainers

Sources

printbox-0.12.tbz
sha256=c783dfbbc21ed9bdab04980b58efa901b149f38f9992f2cdc624abd681d8dedb
sha512=43968271141a44ab4655586bf35bb8540d1ca6312e0a93b14311ae82e6edd700be92d598677fda3bdc6fd8537890ea42cd0224fe9447a6cf8471ba915299ee36

doc/printbox/PrintBox/index.html

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_text.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_text.output stdout b2;;
  +--------------------+
  |I love|a|bbb        |
  |to    |-+-----------|
  |press |c|hello world|
  |enter | |           |
  +--------------------+

 - : unit = ()

Since 0.3 there is also basic support for coloring text:

  # let b = PrintBox.(
      frame
        (vlist [ line_with_style Style.(bg_color Green) "hello";
                 hlist [line "world"; line_with_style Style.(fg_color Red) "yolo"]])
    );;
  val b : t = <abstr>
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)

Sourcemodule Style : sig ... end

Box Combinators

Sourcetype t

Main type for a document composed of nested boxes.

  • since 0.2 the type [t] is opaque
Sourcetype ext = ..

Extensions of the representation.

  • since 0.12
Sourcetype view = private
  1. | Empty
  2. | Text of {
    1. l : string list;
    2. style : Style.t;
    }
  3. | Frame of {
    1. sub : t;
    2. stretch : bool;
    }
  4. | Pad of position * t
  5. | Align of {
    1. h : [ `Left | `Center | `Right ];
    2. v : [ `Top | `Center | `Bottom ];
    3. inner : t;
    }
    (*

    Alignment within the surrounding box

    *)
  6. | Grid of [ `Bars | `None ] * t array array
  7. | Tree of int * t * t array
  8. | Anchor of {
    1. id : string;
    2. inner : t;
    }
  9. | Ext of {
    1. key : string;
    2. ext : ext;
    }

The type view can be used to observe the inside of the box, now that t is opaque.

  • since 0.3 added [Align]
  • since 0.5 added [Link]
  • since 0.11 added [Anchor]
  • since 0.12 added [Stretch]
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 : ?stretch:bool -> t -> t

Put a single frame around the box.

  • parameter stretch

    if true (default false), the frame expands to fill the available space. Present since 0.12

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 by n spaces

Sourceval hpad : int -> t -> t

Pad horizontally by n spaces

Sourceval align : h:[ `Left | `Right | `Center ] -> v:[ `Top | `Bottom | `Center ] -> t -> t

Control alignment of the given box wrt its surrounding box, if any.

  • parameter h

    horizontal alignment

  • parameter v

    vertical alignment

  • since 0.3
Sourceval align_right : t -> t

Left-pad to the size of the surrounding box, as in align ~h:`Right ~v:`Top

  • since 0.3
Sourceval align_bottom : t -> t

Align to the bottom, as in align ~h:`Left ~v:`Bottom

  • since 0.3
Sourceval align_bottom_right : t -> t

Align to the right and to the bottom, as in align ~h:`Right ~v:`Bottom

  • since 0.3
Sourceval center_h : t -> t

Horizontal center, as in .

  • since 0.3
Sourceval center_v : t -> t

Vertical center.

  • since 0.3
Sourceval center_hv : t -> t

Try to center within the surrounding box, as in align ~h:`Center ~v:`Center

  • since 0.3
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 pad

    used to pad each cell (for example with vpad or hpad), default doesn't do anything

  • parameter bars

    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 grid_l : ?pad:(t -> t) -> ?bars:bool -> t list list -> t

Same as grid but from lists.

  • since 0.3
Sourceval grid_text_l : ?pad:(t -> t) -> ?bars:bool -> string list list -> t

Same as grid_text but from lists.

  • since 0.3
Sourceval record : ?pad:(t -> t) -> ?bars:bool -> (string * t) list -> t

A record displayed as a table, each field being a columng (label,value).

  # frame @@ record ["a", int 1; "b", float 3.14; "c", bool true];;
  - : t = +-----------+
          |a|b   |c   |
          |-+----+----|
          |1|3.14|true|
          +-----------+
  • since 0.3
Sourceval v_record : ?pad:(t -> t) -> ?bars:bool -> (string * t) list -> t

Like record, but printed vertically rather than horizontally.

  # frame @@ v_record ["a", int 1; "b", float 3.14; "c", bool true];;
  - : t = +------+
          |a|1   |
          |-+----|
          |b|3.14|
          |-+----|
          |c|true|
          +------+
  • since 0.4
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 grid_map_l : ?bars:bool -> ('a -> t) -> 'a list list -> t

Same as grid_map but with lists.

  • since 0.4
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

link ~uri inner points to the given URI, with the visible description being inner. Will render in HTML as a "<a>" element.

  • since 0.5
Sourceval anchor : id:string -> t -> t

anchor ~id inner provides an anchor with the given ID, with the visible hyperlink description being inner. Will render in HTML as an "<a>" element, and as a link in ANSI stylized text. If inner is non-empty, the rendered link URI is "#" ^ id.

  • since 0.11
Sourceval extension : key:string -> ext -> t

extension ~key ext embeds an extended representation ext as a box. ext must be recognized by the used backends as an extension registered under key.

  • since 0.12

Styling combinators

Sourceval line_with_style : Style.t -> string -> t

Like line but with additional styling.

  • since 0.3
Sourceval lines_with_style : Style.t -> string list -> t

Like lines but with additional styling.

  • since 0.3
Sourceval text_with_style : Style.t -> string -> t

Like text but with additional styling.

  • since 0.3
Sourceval sprintf_with_style : Style.t -> ('a, Buffer.t, unit, t) format4 -> 'a

Formatting for text, with style

  • since 0.3
Sourceval asprintf_with_style : Style.t -> ('a, Format.formatter, unit, t) format4 -> 'a

Formatting for text, with style.

  • since 0.3

Simple Structural Interface

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

Innovation. Community. Security.