SpectrumColour 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.
(* 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@}
@{<green>text@}, @{<dark-orange>text@}@{<#ff5733>text@}, @{<#f00>text@}@{<rgb(255 87 51)>text@}@{<hsl(60 100 50)>text@}@{<bold>text@}, @{<underline>text@}, @{<italic>text@}, @{<overline>text@}@{<bg:red>text@}, @{<fg:blue>text@}@{<bold,bg:red,yellow>text@}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.
Spectrum_tools for color conversion utilitiesSpectrum_palettes for pre-generated palette modulesmodule Capabilities : module type of Spectrum_capabilities.CapabilitiesTerminal capability detection for color support.
module type Printer = sig ... endPrinter module type - provides formatted printing with ANSI color codes.
module type Serializer = sig ... endSerializer 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 PrinterPrepare 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 ()module Simple : sig ... endSimple one-shot printing interface that handles formatter setup/teardown.
module Private : sig ... endInternal modules exposed for testing purposes.