ANSI CSI codes for terminals
npm install ansi-csi-terminalES6 library for controlling/animating a terminal with ANSI CSI escape codes.
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”.
``javascript
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 "\03342mHello"
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
* w(text) – Write textup(n)
* – Move cursor updown(n)
* – Move cursor downright(n)
* – Move cursor rightleft(n)
* – Move cursor leftlineDown(n)
* – Move cursor n lines down and go to beginning of the linelineUp(n)
* – Dito, but n lines upx(r)
* – Go to row r (counting starts at 1)xy(r,c)
* – Go to row r and column c (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 line clearLine()
* – Clear entire line (cursor position remains unchanged)scrollUp(n)
* – Scroll n lines up (i.e. text moves up, cursor position stays)scrollDown(n)
* – Dito, but n lines downsaveCursorPos()
* – Save current cursor positionrestoreCursorPos()
* – Return to previously saved cursor positionshowCursor(show)
* – Show cursor if show is true (DECTCEM)hideCursor()
* – Hide cursor (DECTCEM)invertScreen(b)
* – Invert screen colours if b is true (DECSCNM)format(f1, f2, ...)
* – Set formattingsreset()
* – Reset terminal (like when entering reset), clears formatting and buffer and stuff. Good for un-breaking broken terminals. (RIS)color
* – See belowheight
* – Get terminal heightwidth
* – Get terminal width
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