ansi-csi-terminal
v1.1.2
Published
ANSI CSI codes for terminals
Downloads
4
Readme
ES6 library for controlling/animating a terminal with ANSI CSI escape codes.
ansi-csi-terminal
ANSI CSI defines escape codes for manipulating terminal screens by moving around, setting colours, erasing lines/screens etc. which are supported on most terminals.
This library also contains some VT220 commands.
For a demo, including a half rainbow, run test/index.js
in this repository:
npm run test
Example: Write text, go back and overwrite “rainy“ with a yellow “sunny”.
const csi = require( 'ansi-csi-terminal' );
csi.w( 'It is rainy' )
.left( 5 )
.format( csi.color.yellow.bg, csi.color.black )
.w( 'sunny' );
CSI sequences can be run manually in terminals e.g. with echo -e
. The following line prints text
with green background colour:
echo -e "\033[42mHello"
Other libraries
Please also check some other libraries with CSI support to see if they better fit your needs:
- ansi-escape-sequences is widespread and nicely documented
- node-csi provides more features and lots of formatting options
- node-ansi is very small and simple to use
API
w(text)
– Write textup(n)
– Move cursor updown(n)
– Move cursor downright(n)
– Move cursor rightleft(n)
– Move cursor leftlineDown(n)
– Move cursorn
lines down and go to beginning of the linelineUp(n)
– Dito, butn
lines upx(r)
– Go to rowr
(counting starts at 1)xy(r,c)
– Go to rowr
and columnc
(counting starts at 1)clearToEos()
– Clear from cursor to end of screenclearToBos()
– Clear from cursor to beginning of screenclearScreen()
– Clear content of entire screenclearScreenWithBuffer()
– Clear screen and bufferclearToEnd()
– Clear from cursor to end of the line (cursor position remains unchanged)clearToHome()
– Dito, but clear to beginning of lineclearLine()
– Clear entire line (cursor position remains unchanged)scrollUp(n)
– Scrolln
lines up (i.e. text moves up, cursor position stays)scrollDown(n)
– Dito, butn
lines downsaveCursorPos()
– Save current cursor positionrestoreCursorPos()
– Return to previously saved cursor positionshowCursor(show)
– Show cursor ifshow
istrue
(DECTCEM)hideCursor()
– Hide cursor (DECTCEM)invertScreen(b)
– Invert screen colours ifb
istrue
(DECSCNM)format(f1, f2, ...)
– Set formattingsreset()
– Reset terminal (like when enteringreset
), clears formatting and buffer and stuff. Good for un-breaking broken terminals. (RIS)color
– See belowheight
– Get terminal heightwidth
– Get terminal width
Colors
Color definitions can be passed to csi.format(...args)
.
4-bit Colors
The following 4-bit colors are available: black
red
green
yellow
blue
magenta
cyan
white
They can be made bright with .bright
(on some terminals) and they can be converted to a background color with .bg
:
Example:
csi.format( csi.color.red ) # Red font color
csi.format( csi.color.red.bg ) # Red background color
csi.format( csi.color.red.bg.bright ) # Bright red background color
RGB Colors
Less widespread. But fun.
csi.color.rgb( 232, 211, 23 ); // Gives a nice orange
csi.color.rgb( 232, 211, 23 ).bg; // Same, as background