package ansifmt
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=7bd5aff4eb547ca9bbc50cda6fa71af016673fbed0b0913b6c7bf5fce4688459
sha512=1042f4d0ae6d02ab90c1034d839f7b6d30980983b6192cb6c4c18dce030312693a82edbc37e5fa70acf4d1713f0875c3e7e2b5ff12fcac5c64737465c5e72def
doc/ansifmt/Ansifmt/Ansi/index.html
Module Ansifmt.Ansi
This module defines the escape sequence data type.
Every value of this type is one styling (`Bold, `Reverse, `Foreground Color.red) or the composition of two of them (`Bold&`Reverse), which can be chained (`Dim & `Italic & `Blink & `Background Color.magenta).
open Ansifmt
let chesnay = `Italic
let rocquencout = Ansi.(`Bold & `Reverse & `Foreground Color.red)
(* compose to infinity! *)
let inria = Ansi.(chesnay & rocquencourt)chesnay, rocquencourt and inria all have the same type t.
In a composition chain, if two stylings are overlapping (for example, `Foreground Color.blue & `Foreground Color.red), the rightmost takes precedence.
type t = [ | `Bold| `Dim| `Italic| `Underline| `Blink| `Reverse| `Foreground of Color.t| `Background of Color.t| `Composed of t * t
]Represents an ANSI SGR escape sequence.
compose left right combines the left and right sequences into one.
See also: ( & )
val serialize : t -> stringserialize ansi produces a serialized representation of the ansi escape sequence.
Example
open Ansifmt
let styling = Ansi.(`Dim & `Background (`Rgb (0, 0, 0)))
let () = Printf.printf "%s\n" (Ansi.serialize styling)dim & background(rgb(0, 0, 0)) will be printed.
See also: deserialize.
val deserialize : string -> t optiondeserialize string produces an escape sequence from a serialization string. If it fails to parse, returns None.
Example
open Ansifmt
let serialized = "blink & BOLD & foreground(rgb(255, 0, 0))"
let () =
match Ansi.deserialize serialized with
| None -> Printf.eprintf "oh nothing don't worry\n"
| Some ansi ->
Printf.printf "%sHACKED!\n" (Ansi.show ansi)
;;A blinking bold HACKED! will be printed in red.
See also: serialize.
val show : t -> stringshow ansi renders the ansi escape sequence into a string.
Example
open Ansifmt
let versailles = Ansi.(`Underline & `Background Color.green)
let () = Printf.printf "%s hello!\n" (Ansi.show versailles)hello! will be printed with an underline and a green background.
See also: Ansifmt.Fmt, wrap, unshow.
val unshow : t -> stringunshow ansi renders the ANSI escape sequence that cancels ansi into a string.
Example
open Ansifmt
let cergy = Ansi.(`Blink & `Dim & `Foreground Color.blue)
let () =
Printf.printf
"%s hello! %s bye.\n"
(Ansi.show cergy)
(Ansi.unshow cergy)
;;While hello! will be printed dim, blinking and in blue, bye. will be unstylized.
See also: Ansifmt.Fmt, wrap, show.
val wrap : t -> string -> stringwrap ansi string wraps string with the rendered ansi escape sequence and its cancelling counterpart.
For example, if string is "Hello" and ansi is `Bold, the result will be "\x1b[1mHello\x1b[22m", which makes the string appear bold but not what comes after.
Example
open Ansifmt
let styling = `Foreground (`Rgb (255, 127, 0))
let text = "I love OCaml"
let () = Printf.printf "%s\n" (Ansi.wrap styling text)I love OCaml will be printed in a beautiful orange color.
See also: Ansifmt.Fmt, show, unshow.
val enrich : t -> string -> stringenrich is a synonym of wrap.
left & right is the same as compose left right.
NOTE: & is associative but not commutative.
Example
open Ansifmt
let styling = Ansi.(`Bold & `Foreground Color.yellow)module Attributes : module type of Attributes