package ansifmt

  1. Overview
  2. Docs

Module Ansifmt.FmtSource

A string which components can be styled with ANSI escape sequences.

include Rich_string.TYPE with type Enricher.t = Ansi.t
module Enricher : Rich_string.ENRICHER with type t = Ansi.t

The enricher.

Sourcetype t =
  1. | Empty
  2. | String of string
  3. | Enriched of Enricher.t * t
  4. | Join of t * t list

The type of a rich string.

Sourceval (=) : t -> t -> bool

rs1 = rs2 determines whether rs1 and rs2 are equal.

IMPORTANT: no pruning or optimization is performed. This means that two rich strings that render the same might not be considered equal.

Sourceval empty : t

empty is the identity string with respect to (++).

Sourceval (++) : t -> t -> t

rs1 ++ rs2 appends rs2 at the end of rs1.

It is equivalent to the built-in string operator ^.

  # print (String "hello" ++ String "world")
  "helloworld"
  - : unit = ()
Sourceval string : string -> t

string s lifts the built-in string s to the enriched world.

For example, if the enricher is an ANSI style type, this enables the ability to stylize s.

Sourceval enrich : Enricher.t -> t -> t

enrich enrichment rs adds the enrichment to the rich string rs.

For example, if the enricher is an ANSI style type, this translates to adding a new style to s.

Sourceval join : ?on:t -> t list -> t

join ?on:sep rss concatenates all rss into one, separated by sep. By default, sep is a single space.

It is equivalent to the built-in string function String.concat.

Sourceval flatten : t list -> t

flatten rss concatenates all rss into one.

It is equivalent to join~on:Empty rss.

Sourceval join_lines : t list -> t

join_lines rss concatenates all rss into one, separated by line feeds.

It is equivalent to join~on:(String "\n") rss.

Sourceval is_empty : t -> bool

is_empty rs tests whether rs is Empty.

Sourceval is_enriched : t -> bool

is_enriched rs tests whether rs is not just a plain String.

Sourceval stylize : Ansi.t -> t -> t

stylize ansi rs adds the ansi escape sequence to the rich string rs. Synonym of enrich.

Sourceval prune_styled : t -> t

prune_styled rs removes every ANSI escape sequence from the rich string rs.

Sourceval render : with_styles:bool -> t -> string

render ~with_styles rs generates a built-in string from the rich string rs that includes ANSI escape sequences if with_styles is true.

Sourceval print : ?out:out_channel -> ?ending:t option -> ?with_styles:[< `Always | `Auto | `Never Auto ] -> t -> unit

print ?out ?ending ?with_styles rs writes the rich string rs to the out channel (defaults to stdout), optionally appending ending (defaults to a newline), optionally including ANSI escape sequences (defaults to doing it if out is a TTY).