Skip to main content

Terminal Formatter

ANSI escape codes for shell output.

Options

RuntimeOptions
Rustlanguage, theme, background, width, rainbow_brackets
Elixirlanguage, theme, background, width, rainbow_brackets
JavaScriptlanguage, theme, background, width, rainbowBrackets
JavawithLang(), withTheme(), withFormatter(Formatter.TERMINAL)
CLIdefault formatter, --theme, --background, --width, --rainbow-brackets

Example

import {highlight} from '@lumis-sh/lumis'
import {terminal} from '@lumis-sh/lumis/formatters'
import javascript from '@lumis-sh/lumis/langs/javascript'
import frappe from '@lumis-sh/themes/catppuccin_frappe'
const ansi = await highlight(
'const x = 1',
terminal({language: javascript, theme: frappe, background: 'theme', width: 120}),
)
console.log(ansi)

Output format

Terminal output is plain text wrapped in ANSI escape sequences:

\x1b[38;2;202;158;230mfn\x1b[0m \x1b[38;2;140;170;238mmain\x1b[0m() {}
  • the source stays as terminal text, not HTML
  • ANSI escape sequences set foreground color and text styles
  • background / --background controls the fallback background: omit it to inherit the output background, use theme to reuse the theme background, or pass a hex color
  • width / --width pads each rendered line to the requested width, which is only visible alongside background; in the CLI, auto uses the current terminal width when available, and -w is the short form
  • a tab counts as four columns when measuring a line for padding, in every runtime
  • \x1b[0m resets formatting between styled segments

When to use it

  • CLI tools
  • logs and local scripts
  • previewing highlighted code in a terminal