Marcli.Formatter (Marcli v0.3.1)

View Source

Makeup formatter that produces ANSI-escaped terminal output.

Maps Makeup token types (:keyword, :string, :comment, etc.) to ANSI escape sequences using a configurable style map from Marcli.Theme.

Standalone usage with Makeup

theme = Marcli.Theme.default()

Makeup.highlight(source,
  lexer: "elixir",
  formatter: Marcli.Formatter,
  formatter_options: [syntax: theme.syntax, reset: theme.reset]
)

Token type fallback

When a specific token type (e.g. :keyword_constant) is not present in the style map, the formatter walks up the hierarchy by stripping trailing segments: :keyword_constant -> :keyword -> unstyled.

Summary

Functions

Formats a list of Makeup tokens as an ANSI-escaped binary string.

Formats a list of Makeup tokens as an iolist with ANSI escape sequences.

Same as format_as_binary/2 (no wrapping needed for terminal output).

Same as format_as_iolist/2 (no wrapping needed for terminal output).

Formats a single Makeup token {tag, meta, value} into a string.

Types

token()

@type token() :: {atom(), map(), iodata()}

Functions

format_as_binary(tokens, opts \\ [])

@spec format_as_binary(
  [token()],
  keyword()
) :: String.t()

Formats a list of Makeup tokens as an ANSI-escaped binary string.

Options

  • :syntax -- map of token types to ANSI escape sequences (default: Marcli.Theme.default().syntax)
  • :reset -- ANSI reset sequence (default: "\e[0m")

format_as_iolist(tokens, opts \\ [])

@spec format_as_iolist(
  [token()],
  keyword()
) :: iolist()

Formats a list of Makeup tokens as an iolist with ANSI escape sequences.

Accepts the same options as format_as_binary/2.

format_inner_as_binary(tokens, opts \\ [])

@spec format_inner_as_binary(
  [token()],
  keyword()
) :: String.t()

Same as format_as_binary/2 (no wrapping needed for terminal output).

format_inner_as_iolist(tokens, opts \\ [])

@spec format_inner_as_iolist(
  [token()],
  keyword()
) :: iolist()

Same as format_as_iolist/2 (no wrapping needed for terminal output).

format_token(arg, syntax, reset)

@spec format_token(token(), map(), String.t()) :: String.t()

Formats a single Makeup token {tag, meta, value} into a string.

Looks up the ANSI sequence for tag in syntax, falling back through parent types. Handles newlines within token values by resetting and re-opening the style at each line boundary.