What is ansi-escapes?
The ansi-escapes npm package provides a collection of ANSI escape codes for manipulating the terminal. These codes can be used to control cursor movement, erase text, manage scrollback, and more, allowing developers to create interactive command-line applications.
What are ansi-escapes's main functionalities?
Cursor Movement
Moves the cursor to the specified x and y position (column 5, row 10).
const ansiEscapes = require('ansi-escapes');
console.log(ansiEscapes.cursorTo(5, 10));
Erase Lines
Erases the specified number of lines (2 lines in this case) from the current cursor position up.
const ansiEscapes = require('ansi-escapes');
console.log(ansiEscapes.eraseLines(2));
Scrolling
Scrolls the terminal viewport up or down one line.
const ansiEscapes = require('ansi-escapes');
console.log(ansiEscapes.scrollUp());
console.log(ansiEscapes.scrollDown());
Clearing Screen
Clears the terminal screen and moves the cursor to the upper-left corner.
const ansiEscapes = require('ansi-escapes');
console.log(ansiEscapes.clearScreen);
Beep
Triggers the terminal bell, which often makes an audible beep sound.
const ansiEscapes = require('ansi-escapes');
console.log(ansiEscapes.beep);
Other packages similar to ansi-escapes
chalk
Chalk is a popular package for styling terminal strings with colors and styles but does not include the cursor and screen manipulation features of ansi-escapes.
blessed
Blessed is a high-level terminal interface library that includes screen and cursor manipulation, similar to ansi-escapes, but offers a more comprehensive API for building terminal applications.
cli-cursor
Cli-cursor allows for showing and hiding the terminal cursor, which is a subset of the functionality provided by ansi-escapes.
inquirer
Inquirer is a package for creating interactive command-line user interfaces. It uses escape codes under the hood for cursor movement and screen manipulation, similar to ansi-escapes, but provides a higher-level API for building prompts.
ansi-escapes
ANSI escape codes for manipulating the terminal
Install
$ npm install ansi-escapes
Usage
const ansiEscapes = require('ansi-escapes');
process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);
API
cursorTo(x, y?)
Set the absolute position of the cursor. x0
y0
is the top left of the screen.
cursorMove(x, y?)
Set the position of the cursor relative to its current position.
cursorUp(count)
Move cursor up a specific amount of rows. Default is 1
.
cursorDown(count)
Move cursor down a specific amount of rows. Default is 1
.
cursorForward(count)
Move cursor forward a specific amount of columns. Default is 1
.
cursorBackward(count)
Move cursor backward a specific amount of columns. Default is 1
.
cursorLeft
Move cursor to the left side.
cursorSavePosition
Save cursor position.
cursorRestorePosition
Restore saved cursor position.
cursorGetPosition
Get cursor position.
cursorNextLine
Move cursor to the next line.
cursorPrevLine
Move cursor to the previous line.
cursorHide
Hide cursor.
cursorShow
Show cursor.
eraseLines(count)
Erase from the current cursor position up the specified amount of rows.
eraseEndLine
Erase from the current cursor position to the end of the current line.
eraseStartLine
Erase from the current cursor position to the start of the current line.
eraseLine
Erase the entire current line.
eraseDown
Erase the screen from the current line down to the bottom of the screen.
eraseUp
Erase the screen from the current line up to the top of the screen.
eraseScreen
Erase the screen and move the cursor the top left position.
scrollUp
Scroll display up one line.
scrollDown
Scroll display down one line.
clearScreen
Clear the terminal screen. (Viewport)
clearTerminal
Clear the whole terminal, including scrollback buffer. (Not just the visible part of it)
beep
Output a beeping sound.
link(text, url)
Create a clickable link.
Supported terminals. Use supports-hyperlinks
to detect link support.
image(filePath, options?)
Display an image.
Currently only supported on iTerm2 >=3
See term-img for a higher-level module.
input
Type: Buffer
Buffer of an image. Usually read in with fs.readFile()
.
options
Type: object
width
height
Type: string | number
The width and height are given as a number followed by a unit, or the word "auto".
N
: N character cells.Npx
: N pixels.N%
: N percent of the session's width or height.auto
: The image's inherent size will be used to determine an appropriate dimension.
preserveAspectRatio
Type: boolean
Default: true
iTerm.setCwd(path?)
Type: string
Default: process.cwd()
Inform iTerm2 of the current directory to help semantic history and enable Cmd-clicking relative paths.
Related
- ansi-styles - ANSI escape codes for styling strings in the terminal