easy_format

    A port of OCaml's easy-format: pretty-printing of trees made of atoms, lists and labels, with styles

    pretty-printing
    format
    indentation
    ocaml
    Download zip
    Author
    Version
    0.1.0
    License
    BSD-3-Clause
    Last updated
    3 hours ago
    Downloads
    3

    Dependencies

    #bobzhang/easy_format

    A port of easy-format 1.3.4 to MoonBit, on top of bobzhang/format, a port of OCaml's Format: pretty-printing of trees made of atoms, lists and labels, with indentation made easy.

    let point = @easy_format.T::List(("{", ",", "}", @easy_format.list), [
    Label((Atom("x:", @easy_format.atom), @easy_format.label), Atom("1", @easy_format.atom)),
    Label((Atom("y:", @easy_format.atom), @easy_format.label), Atom("2", @easy_format.atom)),
    ])
    @easy_format.to_string(point) // "{ x: 1, y: 2 }"

    A tree (T) is made of:

    • Atom(text, param): text printed as is;
    • List((opening, separator, closing, param), items): a sequence such as [ 1, 2, 3 ], printed horizontally, vertically or wrapped;
    • Label((label, param), item): an item with a label such as x: or let x =;
    • Custom(f): printed with the formatter directly.

    The parameters (AtomParam, ListParam, LabelParam) are records: derive them from the defaults, e.g. { ..@easy_format.list, wrap_body: ForceBreaks }.

    The functions of OCaml's Easy_format.Pretty are to_formatter, to_buffer, to_string and define_styles, with styles (the markers of semantic tags, e.g. for HTML or terminal colors) and escaping. Those of Easy_format.Compact are compact_to_formatter, compact_to_buffer and compact_to_string. The deprecated Easy_format.Param values are list_true, list_false, label_true and label_false.

    #Testing

    The test and the examples of easy-format (test/test_easy_format.ml, examples/simple_example.ml and examples/lambda_example.ml) are ported, and their outputs are compared with the outputs of the OCaml programs.

    #License

    BSD-3-Clause, like easy-format; see LICENSE.

    AtomParam

    pub(all) struct AtomParam {
    atom_style : String?
    } derive(Eq,
    Debug
    )

    Parameters of an atom.

    AtomParam::equal

    fn AtomParam::equal(AtomParam, AtomParam) -> Bool

    AtomParam::not_equal

    fn AtomParam::not_equal(x : AtomParam, y : AtomParam) -> Bool

    Escape

    pub(all) enum Escape {
    NoEscape
    Escape(((StringView) -> Unit, StringView) -> Unit)
    EscapeString((String) -> String)
    }

    How to escape the text, but not the markers of the styles.

    LabelBreak

    pub(all) enum LabelBreak {
    Auto
    Always
    AlwaysRec
    Never
    } derive(Eq,
    Debug
    )

    When to break the line after a label.

    LabelBreak::equal

    fn LabelBreak::equal(LabelBreak, LabelBreak) -> Bool

    LabelBreak::not_equal

    fn LabelBreak::not_equal(x : LabelBreak, y : LabelBreak) -> Bool

    LabelParam

    pub(all) struct LabelParam {
    label_break : LabelBreak
    space_after_label : Bool
    indent_after_label : Int
    label_style : String?
    } derive(Eq,
    Debug
    )

    Parameters of a label.

    LabelParam::equal

    fn LabelParam::equal(LabelParam, LabelParam) -> Bool

    LabelParam::not_equal

    fn LabelParam::not_equal(x : LabelParam, y : LabelParam) -> Bool

    ListParam

    pub(all) struct ListParam {
    space_after_opening : Bool
    space_after_separator : Bool
    space_before_separator : Bool
    separators_stick_left : Bool
    space_before_closing : Bool
    stick_to_label : Bool
    align_closing : Bool
    wrap_body : Wrap
    indent_body : Int
    list_style : String?
    opening_style : String?
    body_style : String?
    separator_style : String?
    closing_style : String?
    } derive(Eq,
    Debug
    )

    Parameters of a list.

    ListParam::equal

    fn ListParam::equal(ListParam, ListParam) -> Bool

    ListParam::not_equal

    fn ListParam::not_equal(x : ListParam, y : ListParam) -> Bool

    Style

    pub(all) struct Style {
    tag_open : String
    tag_close : String
    } derive(Eq,
    Debug
    )

    The markers printed around the text of a style: the opening and closing markers of a semantic tag, which are not counted in the width of the text.

    Style::equal

    fn Style::equal(Style, Style) -> Bool

    Style::not_equal

    fn Style::not_equal(x : Style, y : Style) -> Bool

    Style::to_repr

    pub(all) enum T {
    Atom(String, AtomParam)
    List((String, String, String, ListParam), Array[T])
    Label((T, LabelParam), T)
    Custom((
    Formatter
    ) -> Unit)
    }

    A tree to be pretty-printed.

    Wrap

    pub(all) enum Wrap {
    WrapAtoms
    AlwaysWrap
    NeverWrap
    ForceBreaks
    ForceBreaksRec
    NoBreaks
    } derive(Eq,
    Debug
    )

    How the body of a list may be wrapped.

    Wrap::equal

    fn Wrap::equal(Wrap, Wrap) -> Bool

    Wrap::not_equal

    fn Wrap::not_equal(x : Wrap, y : Wrap) -> Bool

    Wrap::to_repr

    atom

    let atom : AtomParam

    The default parameters of atoms. Derive new parameters from it with { ..atom, field: value }.

    compact_to_buffer

    fn compact_to_buffer(buf : StringBuilder, x : T) -> Unit

    Print a tree into a string builder without spacing or newlines other than those in the input data. Compact.to_buffer in OCaml.

    compact_to_formatter

    fn compact_to_formatter(fmt :
    Formatter
    , x : T) -> Unit

    Print a tree with a formatter without spacing or newlines other than those in the input data, and flush it. Compact.to_formatter in OCaml.

    compact_to_string

    fn compact_to_string(x : T) -> String

    Print a tree into a string without spacing or newlines other than those in the input data. Compact.to_string in OCaml.

    define_styles

    fn define_styles(fmt :
    Formatter
    , escape : Escape, styles : ArrayView[(String, Style)]) -> Unit

    Prepare a formatter for printing with styles (the markers of the styles are printed with semantic tags) and escaping. Pretty.define_styles in OCaml.

    label

    let label : LabelParam

    The default parameters of labels. Derive new parameters from it with { ..label, field: value }.

    label_false

    #deprecated("Derive the parameters from `label` instead")
    let label_false : LabelParam

    All the boolean fields set to false, indent_after_label = 2.

    label_true

    #deprecated("Derive the parameters from `label` instead")
    let label_true : LabelParam

    All the boolean fields set to true, indent_after_label = 2.

    list

    let list : ListParam

    The default parameters of lists. Derive new parameters from it with { ..list, field: value }.

    list_false

    #deprecated("Derive the parameters from `list` instead")
    let list_false : ListParam

    All the boolean fields set to false, indent_body = 2.

    list_true

    #deprecated("Derive the parameters from `list` instead")
    let list_true : ListParam

    All the boolean fields set to true, indent_body = 2.

    to_buffer

    fn to_buffer(buf : StringBuilder, x : T, escape? : Escape, styles? : ArrayView[(String, Style)]) -> Unit

    Pretty-print a tree into a string builder, with a fresh formatter (whose margin is 78). Pretty.to_buffer in OCaml.

    to_formatter

    fn to_formatter(fmt :
    Formatter
    , x : T) -> Unit

    Pretty-print a tree with a formatter, and flush it. Pretty.to_formatter in OCaml.

    to_string

    fn to_string(x : T, escape? : Escape, styles? : ArrayView[(String, Style)]) -> String

    Pretty-print a tree into a string. Pretty.to_string in OCaml.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io