Function Repository Resource:

ANSITools

Source Notebook

A collection of useful ANSI escape sequences and helper functions

Contributed by: Antonis Aristeidou

ResourceFunction["ANSITools"][token]

generates ANSI escape codes corresponding to token.

ResourceFunction["ANSITools"]["MoveCursor", args]

generates ANSI escape codes moving the terminal cursor as directed by args.

ResourceFunction["ANSITools"]["Style", text, styleOptions]

generates ANSI escape codes styling text using the styleOptions provided.

ResourceFunction["ANSITools"]["Style", styleOptions]

operator form of Style able to applied directly to text strings.

Details

ANSI escape sequences, as established by the American National Standards Institute (ANSI), are used to control cursor location, styling and other options in text-based terminals.
Not all terminals support the full range of ANSI codes.
The following token values expect the args shown in the center column:
"MoveCursor"row,column or direction,amount move the cursor to a specified position or by a specified amount
"Style"text, styleoptions style text according to the styleoptions
The directions supported by "MoveCursor" are Up, Down, Left and Right.
The following styleoptions are supported:
FontColorDefaultchange the foreground color of text
FontWeightDefaultchange the text display mode
BackgroundDefaultchange the background color of text
The options for FontWeight supported are Default, Bold, Italic, Underlined, Reverse, Invisible, Struckthrough, "Dim" and "Blinking".
24-bit colors are only supported in some modern terminals. The colors Black, White, Red, Blue, Green, Yellow, Cyan and Magenta and their light equivalents LightRed, LightBlue, LightGreen, LightYellow, LightCyan, LightMagenta map to the built-in terminal colors which are more widely supported.
The following token values are supported with no args:
"SavePosition"save the current cursor postion
"LoadPosition"load the last saved cursor position
"RequestPosition"request the cursor position (reports as ESC[{row};{col}R)
"Clear"clear the entire screen
"ClearToStart"clear from cursor to start of the screen
"ClearToEnd"clear from cursor to end of the screen
"ClearLine"clear the entire line
"ClearLineToStart"clear from cursor to start of the line
"ClearLineToEnd"clear from cursor to end of the line
"Tokens"list all known tokens

Examples

Basic Examples

List all known tokens:

In[1]:=
ResourceFunction["ANSITools"]["Tokens"]
Out[1]=

Style some text printed in the console:

Commands can be linked together using StringJoin. Delete the last 10 rows printed in the terminal:

Use the operator form of "Style":

In[2]:=
style = ResourceFunction["ANSITools"]["Style", Red, FontWeight -> "Blinking"];
style["text"]
Out[3]=

ANSITools will always output some string containing ANSI escape sequences:

In[4]:=
TableForm @ {
  ResourceFunction["ANSITools"]["Clear"],
  ResourceFunction["ANSITools"]["MoveCursor", Up, 10],
  ResourceFunction["ANSITools"]["Style", "Some text", Red],
  ResourceFunction["ANSITools"]["Style", "Some text", Red, Bold],
  ResourceFunction["ANSITools"]["Style", "Some text", Hue[0.3, 1, 1 ], FontWeight -> "Blinking"]
  }
Out[4]=

Publisher

Antonis Aristeidou

Requirements

Wolfram Language 12.0 (April 2019) or above

Version History

  • 1.0.0 – 13 June 2025

Related Resources

License Information