What is ansi-escape-sequences?
The ansi-escape-sequences npm package provides utilities for working with ANSI escape codes, which are used to control text formatting, color, and other output options on text terminals. This package allows you to easily add color, style, and control sequences to your terminal output.
What are ansi-escape-sequences's main functionalities?
Text Formatting
This feature allows you to format text with styles such as bold, italic, underline, etc. The code sample demonstrates how to make text bold.
const ansi = require('ansi-escape-sequences');
console.log(ansi.format('This is bold text', ['bold']));
Text Coloring
This feature allows you to colorize text with various colors. The code sample demonstrates how to make text red.
const ansi = require('ansi-escape-sequences');
console.log(ansi.format('This is red text', ['red']));
Cursor Control
This feature allows you to control the cursor position in the terminal. The code sample demonstrates how to move the cursor to a specific position before printing text.
const ansi = require('ansi-escape-sequences');
process.stdout.write(ansi.cursor.goto(10, 5));
console.log('Text at position (10, 5)');
Clearing the Screen
This feature allows you to clear parts or the entire terminal screen. The code sample demonstrates how to clear the entire screen.
const ansi = require('ansi-escape-sequences');
process.stdout.write(ansi.erase.display(2));
Other packages similar to ansi-escape-sequences
chalk
Chalk is a popular library for styling terminal strings. It provides a more user-friendly API for applying styles and colors to text. Compared to ansi-escape-sequences, Chalk is more focused on ease of use and readability.
colors
Colors is another library for adding color and style to terminal output. It offers a chainable API for applying multiple styles. Compared to ansi-escape-sequences, Colors provides a more fluent interface for combining styles.
cli-color
CLI-Color is a library for styling terminal output with ANSI colors and styles. It offers a comprehensive set of features for text formatting and cursor control. Compared to ansi-escape-sequences, CLI-Color provides a more extensive set of utilities for terminal manipulation.
work in progress, draft documentation
##ansi-escape-sequences
A simple library containing all the known ansi escape codes and sequences.
Example
var ansi = require("ansi-escape-sequences");
###enum: ansi.sgr → string
Select Graphic Rendition codes
Read only: true
Properties
Name | Type | Default |
---|
reset | string | [0m |
bold | string | [1m |
italic | string | [3m |
underline | string | [4m |
fontDefault | string | [10m |
font2 | string | [11m |
font3 | string | [12m |
font4 | string | [13m |
font5 | string | [14m |
font6 | string | [15m |
imageNegative | string | [7m |
imagePositive | string | [27m |
black | string | [30m |
red | string | [31m |
green | string | [32m |
yellow | string | [33m |
blue | string | [34m |
magenta | string | [35m |
cyan | string | [36m |
white | string | [37m |
Example
console.log(ansi.sgr.red + "this is red" + ansi.sgr.reset);
###ansi.sgrSequence(effectArray) ⇒ string
Returns an a sequence setting one or more effects
Param | Type | Description |
---|
effectArray | string | Array.<string> | a sgr effect, or list of effects |
Example
> ansi.sgrSequence("green")
'\u001b[32m'
> ansi.sgrSequence([ "green", "underline" ])
'\u001b[32;4m'
###ansi.cursorUp()
Moves the cursor lines
(default 1) cells up. If the cursor is already at the edge of the screen, this has no effect
###ansi.cursorDown()
Moves the cursor lines
(default 1) cells down. If the cursor is already at the edge of the screen, this has no effect
###ansi.format(str, effectArray) ⇒ string
Formats the input string with the specified sgr effects
Param | Type | Description |
---|
str | string | the string to format |
effectArray | Array.<string> | a list of sgr effects to add |
Example
> ansi.format("what?", "green")
'\u001b[32mwhat?\u001b[0m'
> ansi.format("what?", ["green", "bold"])
'\u001b[32;1mwhat?\u001b[0m'