package spectrum
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=db49a8c779384c12e87df17f5c3baa77
sha512=3c534a955cc998ff14eab8f3674bcbfd98875df7937c9468bf0c6209490ff334b940920d932a62884b72d68c85415831080af132da4287f4cef848fb6af7f568
doc/spectrum/Spectrum/index.html
Module SpectrumSource
Colour and formatting for terminal output.
Spectrum integrates ANSI color and style formatting with OCaml's Format semantic tags. String tags are defined for ANSI styles (bold, underline, etc.) and named colours from the xterm 256-color palette, plus 24-bit colours via CSS-style hex codes and RGB or HSL values.
Terminal capabilities are detected automatically and colours are quantized to match what the terminal supports, using perceptually accurate LAB color space distance calculations.
Basic Usage
(* Prepare a formatter to handle color tags *)
let reset = Spectrum.prepare_ppf Format.std_formatter in
let () = Format.printf "@{<green>Hello@} @{<bold>world@}@." in
reset ()
(* Or use the Simple API for one-off printing *)
let () = Spectrum.Simple.printf "@{<green>Hello@} @{<bold>world@}@."Tag syntax: @{<TAG>content@}
- Named colors:
@{<green>text@},@{<dark-orange>text@} - Hex colors:
@{<#ff5733>text@},@{<#f00>text@} - RGB:
@{<rgb(255 87 51)>text@} - HSL:
@{<hsl(60 100 50)>text@} - Styles:
@{<bold>text@},@{<underline>text@},@{<italic>text@},@{<overline>text@} - Qualifiers:
@{<bg:red>text@},@{<fg:blue>text@} - Compound:
@{<bold,bg:red,yellow>text@}
Interface
Spectrum provides two module variants:
The default top-level interface (just Spectrum.xyz) is equivalent to Noexn. Both expose Printer, which includes prepare_ppf and the Printer.Simple convenience module.
Note: Format.sprintf uses its own buffer, so you must use Simple.sprintf for styled sprintf, or create your own buffer with Format.fprintf.
See Also
- GitHub repository
Spectrum_toolsfor color conversion utilitiesSpectrum_palettesfor pre-generated palette modules
Terminal capability detection for color support.
Lexer for parsing color/style tags and ANSI escape sequences.
Parser for converting lexer output into structured tokens.
Printer module type - provides formatted printing with ANSI color codes.
Serializer module type - converts parsed tokens to ANSI escape codes.
Default printer behavior (equivalent to Noexn).
Includes all functions from Noexn at the top level for convenient access.
include Printer
Prepare a formatter to handle color tags. Returns a reset function to restore original formatter state.
This enables Spectrum's tag processing on a Format.formatter, allowing you to use @{<tag>content@} syntax in your format strings. The returned function restores the formatter to its original state.
(* Basic usage with stdout *)
let reset = Spectrum.prepare_ppf Format.std_formatter in
Format.printf "@{<green>Success:@} Operation completed@.";
Format.printf "@{<bold>%d@} items processed@." 42;
reset () (* Using with a custom formatter *)
let buffer = Buffer.create 256 in
let fmt = Format.formatter_of_buffer buffer in
let reset = Spectrum.prepare_ppf fmt in
Format.fprintf fmt "@{<red>Error:@} Something went wrong@.";
Format.pp_print_flush fmt ();
reset ();
let result = Buffer.contents buffer in
Printf.printf "Captured: %s\n" result (* Multiple formatters simultaneously *)
let reset_out = Spectrum.prepare_ppf Format.std_formatter in
let reset_err = Spectrum.prepare_ppf Format.err_formatter in
Format.printf "@{<green>Info:@} Starting process@.";
Format.eprintf "@{<yellow>Warning:@} Low memory@.";
reset_out ();
reset_err ()