ansi2
ANSI stream control of position, style, and color via fluent API.
ansi2 sends ANSI escape codes to a writable stream to:
- move the cursor around
- clear portions of the screen, or all of it
- use standard ANSI colors and RGB colors for both foreground and background
- accepts CSS style colors
- show/hide the cursor
- emit a beep sound
Install
npm install --save ansi2
Table of Contents
- Usage: Base
- Usage: Colors
- Usage: Styles
- Usage: Position
Usage: Base
var ansi = require('ansi2')(process.stdout)
ansi.write('boring default text')
ansi.reset()
ansi.resetFg()
ansi.resetBg()
ansi.reset().blue().blackBg().write('blah').reset().write('\n')
ansi.bold().blue('some bold blue text').newline()
.underline().green('some underlined green text').newline()
Usage: Colors
Available colors as functions:
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- grey/gray
Use the "bright" versions of those by adding suffix "Bright" to the function name.
Apply the color to the background by adding suffix "Bg" to the function name.
Apply both suffixes for a bright background (add suffix "BrightBg").
Other color functions accept RGB values and CSS style hex codes:
rgb(r, g, b)
- Also has a background version as rgbBg(r, g, b)
hex(string)
- Also has a background version as hexBg(string)
. May have a hash symbol prefix, or not.
Color functions called with an argument will change the color and then write the string.
ansi.blue().write('blue text')
ansi.blue('blue text')
ansi.blackBg().write('blue on black text')
ansi.rgb(255, 0, 0)
ansi.hex('FF0000')
ansi.hex('#FF0000')
Usage: Styles
Available styles as functions:
- bold
- italic
- underline
- inverse
Reset the style by adding suffix "Reset" to the function name.
Style functions called with an argument will change the style and then write the string.
ansi.bold().write('bold text')
ansi.bold('bold text')
ansi.italic()
ansi.boldReset()
Usage: Position
Available position functions:
- up - move cursor up one line, or, pass a number argument to specify line count
- down - move cursor down one line, or, pass a number argument to specify line count
- forward - move cursor right one column, or, pass a number argument to specify column count
- back - move cursor left one column, or, pass a number argument to specify column count
- next - move cursor to the beginning (left) one line down, or, pass a number argument to specify line count
- previous - move cursor to the beginning (left) one line up, or, pass a number argument to specify line count
- left - move cursor to the beginning of the current line, or, pass a number argument to specify the column to move to
- clearBelow - clear all screen data below current cursor position
- clearAbove - clear all screen data above current cursor position (not history)
- clear - clear entire screen (not history)
- clearAll - clear entire screen and all scroll history
- eraseBefore - erase line content to the left of the cursor position
- eraseAfter - erase line content to the right of the cursor position
- eraseLine - erase entire current line
- scrollUp - scroll up one line, or, specify a number argument to specify how many lines to scroll
- scrollDown - scroll down one line, or, specify a number argument to specify how many lines to scroll
- save - save current cursor position
- restore - restore current cursor position
- position - get the current cursor position
- hide - hide the cursor
- show - show the cursor
- restartLine - convenience function to move to the beginning of the line and erase all line content. specify a number argument and it will go to that column and then erase content after that position.
- goto - convenience function to move to a specific line and column on the screen (both default to 0)
ansi.up()
ansi.up(5)
ansi.goto()
ansi.goto(7, 5)
ansi.restartLine().write('new text overwriting current line')
ansi.write('label: some text')
.restartLine(7).write('new text')