package windtrap

  1. Overview
  2. Docs

Module Windtrap.PpSource

Lightweight pretty-printing with ANSI styling.

Lightweight pretty-printing with ANSI styling.

A minimal replacement for Fmt, using only Stdlib.Format. Provides short aliases for common format operations and composable formatters with optional ANSI color support.

Types

Sourcetype 'a t = Format.formatter -> 'a -> unit

A formatter for values of type 'a.

Sourcetype style = [
  1. | `Bold
  2. | `Faint
  3. | `Green
  4. | `Red
  5. | `Yellow
  6. | `Cyan
  7. | `White
]

Supported ANSI styles.

Style Configuration

Sourceval use_ansi_stdout : bool ref

Whether to emit ANSI escape codes when printing to stdout. Default: false. Set by Env.setup_color.

Sourceval use_ansi_stderr : bool ref

Whether to emit ANSI escape codes when printing to stderr. Default: false. Set by Env.setup_color.

Basic Output

Sourceval str : ('a, Format.formatter, unit, string) format4 -> 'a

Format to string. Equivalent to Format.asprintf.

Sourceval pf : Format.formatter -> ('a, Format.formatter, unit) format -> 'a

Print to a formatter. Equivalent to Format.fprintf.

Sourceval pr : ('a, Format.formatter, unit) format -> 'a

Print to stdout. Equivalent to Format.printf.

Sourceval epr : ('a, Format.formatter, unit) format -> 'a

Print to stderr. Equivalent to Format.eprintf.

Sourceval flush : Format.formatter -> unit -> unit

flush ppf () flushes the formatter ppf.

Styled Output

Sourceval styled : style -> 'a t -> 'a t

styled style pp wraps pp with ANSI escape codes for style when printing to stdout or stderr (controlled by use_ansi_stdout and use_ansi_stderr). Prints without styling on other formatters or when ANSI is disabled.

Basic Formatters

Sourceval string : string t
Sourceval int : int t
Sourceval int32 : int32 t
Sourceval int64 : int64 t
Sourceval bool : bool t
Sourceval char : char t

Combinators

Sourceval list : ?sep:unit t -> 'a t -> 'a list t

list ?sep pp formats a list with pp for each element and sep between them. Default separator: semi.

Sourceval array : ?sep:unit t -> 'a t -> 'a array t

array ?sep pp formats an array with pp for each element and sep between them. Default separator: semi.

Sourceval option : 'a t -> 'a option t

option pp formats None as "None" and Some v as "Some <v>".

Sourceval result : ok:'a t -> error:'b t -> ('a, 'b) result t

result ~ok ~error formats Ok v as "Ok <v>" and Error e as "Error <e>".

Sourceval pair : 'a t -> 'b t -> ('a * 'b) t

pair pp_a pp_b formats a pair as "(<a>, <b>)".

Sourceval brackets : 'a t -> 'a t

brackets pp wraps the output of pp with square brackets.

Sourceval semi : unit t

Format "; " followed by a break hint.

Sourceval comma : unit t

Format ", " followed by a break hint.

Utilities

Sourceval to_string : 'a t -> 'a -> string

to_string pp v formats v using pp and returns the result as a string.

Sourceval styled_string : style -> string -> string

styled_string style s wraps s with ANSI escape codes for style when use_ansi_stdout is enabled. Unlike styled, this works outside of Format formatters (e.g. for building styled strings directly).