a

ansi-render

@preview

provides a simple way to render text with ANSI escape sequences.

v0.9.2
MIT

Package Information

Last Updated
Minimum Typst Version
0.14.0
Authors
8LWXpg
Categories
visualization
Disciplines
computer-science

1. Get the package

Download the package using the TPIX CLI:

tpix get @preview/ansi-render:0.9.2

2. Import in your Typst file

Add this to your .typ file:

#import "@preview/ansi-render:0.9.2": *

Version History

0.9.20.14.0
d89e9ab5b65b...
0.9.10.14.0
46d61a978b5c...
0.9.00.10.0
e712b29897fd...
0.8.00.10.0
40ccd1b25ee4...
0.7.00.10.0
4974c61092fb...
0.6.10.10.0
1f483b1ad389...
0.6.00.10.0
1fbee2bef796...
0.5.10.8.0
cfb37d3bc44a...
0.5.00.8.0
1ed9df4a22d8...
0.4.20.8.0
f7f7a8b9c02e...
0.4.1
2673c581a020...
0.4.0
f816370dd29b...
0.3.0
d27329c1b7dd...
0.2.0
e7788a6a8230...
0.1.0
9c57f31744b4...

ANSI Escape Sequence Renderer

Tagged version
GitHub Repo stars
License on GitHub
typst package

This script provides a simple way to render text with ANSI escape sequences. Package ansi-render provides a function ansi-render, and a dictionary of themes terminal-themes.

Usage

#import "@preview/ansi-render:0.9.2": *

#ansi-render(
  string,
  font:           string or none,
  size:           length,
  width:          auto or relative length,
  height:         auto or relative length,
  breakable:      boolean,
  radius:         relative length or dictionary,
  inset:          relative length or dictionary,
  outset:         relative length or dictionary,
  spacing:        relative length or fraction,
  above:          relative length or fraction,
  below:          relative length or fraction,
  clip:           boolean,
  bold-is-bright: boolean,
  theme:          terminal-themes.theme,
)

Parameters

  • string - string with ANSI escape sequences
  • font - font name or none, default is Cascadia Code, set to none to use the same font as raw
  • size - font size, default is 1em
  • bold-is-bright - boolean, whether bold text is rendered with bright colors, default is false
  • theme - theme, default is vscode-light
  • parameters from block function with the same default value, only affects outmost block layout:
    • width
    • height
    • breakable
    • radius
    • inset
    • outset
    • spacing
    • above
    • below
    • clip

Themes

see themes

Demo

see demo.typ demo.pdf

#ansi-render(
"\u{1b}[38;2;255;0;0mThis text is red.\u{1b}[0m
\u{1b}[48;2;0;255;0mThis background is green.\u{1b}[0m
\u{1b}[38;2;255;255;255m\u{1b}[48;2;0;0;255mThis text is white on a blue background.\u{1b}[0m
\u{1b}[1mThis text is bold.\u{1b}[0m
\u{1b}[4mThis text is underlined.\u{1b}[0m
\u{1b}[38;2;255;165;0m\u{1b}[48;2;255;255;0mThis text is orange on a yellow background.\u{1b}[0m",
inset: 5pt, radius: 3pt,
theme: terminal-themes.vscode
)

RGB color with bold and underlined text

#ansi-render(
"\u{1b}[38;5;196mRed text\u{1b}[0m
\u{1b}[48;5;27mBlue background\u{1b}[0m
\u{1b}[38;5;226;48;5;18mYellow text on blue background\u{1b}[0m
\u{1b}[7mInverted text\u{1b}[0m
\u{1b}[38;5;208;48;5;237mOrange text on gray background\u{1b}[0m
\u{1b}[38;5;39;48;5;208mBlue text on orange background\u{1b}[0m
\u{1b}[38;5;255;48;5;0mWhite text on black background\u{1b}[0m",
inset: 5pt, radius: 3pt,
theme: terminal-themes.vscode
)

256 color

#ansi-render(
"\u{1b}[31;1mHello \u{1b}[7mWorld\u{1b}[0m

\u{1b}[53;4;36mOver  and \u{1b}[35m Under!
\u{1b}[7;90mreverse\u{1b}[101m and \u{1b}[94;27mreverse",
inset: 5pt, radius: 3pt,
theme: terminal-themes.vscode
)

16 color with overline and underline and reverse

// uses the font that supports ligatures
#ansi-render(read("./test/test.txt"), inset: 5pt, radius: 3pt, font: "Cascadia Code", theme: terminal-themes.putty)

render file output

Capturing ANSI Output

Output to File

The most straight forward way is writing to a file then read that in Typst.

command > out.txt

Other Method

If writing to a file doesn't work, that means the program will detect the output and trim ANSI sequence accordingly, which means we need a pty interface to execute the script, the following should work in Linux:

script "command" out.txt
exit

About Default Font

Typst's default font for raw is Dejavu Sans Mono, but it has incorrect top-edge, so the default is set to Cascadia Code, which is included in web editor.