0.1.4 • Published 3 days ago
ansi2 v0.1.4
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
// wrap a writable stream
var ansi = require('ansi2')(process.stdout)
// write a string to stream.
// it's affected by the current color/style/position.
// default styling + color:
ansi.write('boring default text')
// reset the foreground, background, and styling.
ansi.reset()
// Or, reset only the foreground:
ansi.resetFg()
// Or, reset only the background:
ansi.resetBg()
// all functions are chainable:
ansi.reset().blue().blackBg().write('blah').reset().write('\n')
// convenience method helps reset before writing a newline
// to avoid repeatedly writing: .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 asrgbBg(r, g, b)
hex(string)
- Also has a background version ashexBg(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')
// Or, use a shorthand, provide the text to the color function:
ansi.blue('blue text')
// still using blue for foreground.
// change the background now.
// standard colors have a "background" version
// by adding 'Bg' to their function name.
ansi.blackBg().write('blue on black text')
// or specify colors via RGB or hex values.
// all these change the color to red.
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')
// Or, use a shorthand, provide the text to the color function:
ansi.bold('bold text')
ansi.italic()
// then, undo the bold styling only, retaining 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)
// example using default, this will move up one line:
ansi.up()
// example specifying amount to move, this moves up 5 lines:
ansi.up(5)
// moves to the top left:
ansi.goto()
// moves to line 5 column 7:
ansi.goto(7, 5) // think (x, y)
// erase current line and then write new content:
ansi.restartLine().write('new text overwriting current line')
// or, restart back at a certain column to overwrite only the later part:
ansi.write('label: some text') // shows whole string
.restartLine(7).write('new text') // shows: label: new text