ANSI terminal formatter for MF2 highlight tokens.
Wraps each token in ANSI colour escape codes suitable for IEx
output, mix task messages, or anywhere a terminal is assumed.
Colours are chosen for legibility on both light and dark
backgrounds using ANSI 4-bit colours only (no truecolor).
Summary
Functions
Renders a token list as an ANSI-coloured string.
Types
@type options() :: [ {:palette, %{required(Localize.Message.Highlighter.class()) => [atom()]}} ]
Functions
@spec render([Localize.Message.Highlighter.token()], options()) :: String.t() | {:error, Exception.t()}
Renders a token list as an ANSI-coloured string.
Arguments
tokensis a list oft:Highlighter.token/0tuples.optionsis a keyword list.
Options
:palette— a map%{class => [ansi_atom]}overriding the default colour for specific classes.
Returns
- A string containing ANSI escape codes.
Examples
iex> {:ok, ast} = Localize.Message.Parser.parse("Hello {$name}!")
iex> tokens = Localize.Message.Highlighter.to_tokens(ast)
iex> rendered = Localize.Message.Formatter.ANSI.render(tokens)
iex> String.contains?(rendered, "$name") and String.contains?(rendered, "[")
true