pax_global_header00006660000000000000000000000064123332116760014516gustar00rootroot0000000000000052 comment=b7015075cfbc8fc5c09d9039529fd994f38927b4 ansi.js-0.3.0/000077500000000000000000000000001233321167600130635ustar00rootroot00000000000000ansi.js-0.3.0/.gitignore000066400000000000000000000000151233321167600150470ustar00rootroot00000000000000node_modules ansi.js-0.3.0/.jshintrc000066400000000000000000000000461233321167600147100ustar00rootroot00000000000000{ "laxcomma": true, "asi": true } ansi.js-0.3.0/History.md000066400000000000000000000006061233321167600150500ustar00rootroot00000000000000 0.3.0 / 2014-05-09 ================== * package: remove "test" script and "devDependencies" * package: remove "engines" section * pacakge: remove "bin" section * package: beautify * examples: remove `starwars` example (#15) * Documented goto, horizontalAbsolute, and eraseLine methods in README.md (#12, @Jammerwoch) * add `.jshintrc` file < 0.3.0 ======= * Prehistoric ansi.js-0.3.0/README.md000066400000000000000000000062131233321167600143440ustar00rootroot00000000000000ansi.js ========= ### Advanced ANSI formatting tool for Node.js `ansi.js` is a module for Node.js that provides an easy-to-use API for writing ANSI escape codes to `Stream` instances. ANSI escape codes are used to do fancy things in a terminal window, like render text in colors, delete characters, lines, the entire window, or hide and show the cursor, and lots more! #### Features: * 256 color support for the terminal! * Make a beep sound from your terminal! * Works with *any* writable `Stream` instance. * Allows you to move the cursor anywhere on the terminal window. * Allows you to delete existing contents from the terminal window. * Allows you to hide and show the cursor. * Converts CSS color codes and RGB values into ANSI escape codes. * Low-level; you are in control of when escape codes are used, it's not abstracted. Installation ------------ Install with `npm`: ``` bash $ npm install ansi ``` Example ------- ``` js var ansi = require('ansi') , cursor = ansi(process.stdout) // You can chain your calls forever: cursor .red() // Set font color to red .bg.grey() // Set background color to grey .write('Hello World!') // Write 'Hello World!' to stdout .bg.reset() // Reset the bgcolor before writing the trailing \n, // to avoid Terminal glitches .write('\n') // And a final \n to wrap things up // Rendering modes are persistent: cursor.hex('#660000').bold().underline() // You can use the regular logging functions, text will be green: console.log('This is blood red, bold text') // To reset just the foreground color: cursor.fg.reset() console.log('This will still be bold') // to go to a location (x,y) on the console // note: 1-indexed, not 0-indexed: cursor.goto(10, 5).write('Five down, ten over') // to clear the current line: cursor.horizontalAbsolute(0).eraseLine().write('Starting again') // to go to a different column on the current line: cursor.horizontalAbsolute(5).write('column five') // Clean up after yourself! cursor.reset() ``` License ------- (The MIT License) Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ansi.js-0.3.0/examples/000077500000000000000000000000001233321167600147015ustar00rootroot00000000000000ansi.js-0.3.0/examples/beep/000077500000000000000000000000001233321167600156145ustar00rootroot00000000000000ansi.js-0.3.0/examples/beep/index.js000077500000000000000000000005121233321167600172620ustar00rootroot00000000000000#!/usr/bin/env node /** * Invokes the terminal "beep" sound once per second on every exact second. */ process.title = 'beep' var cursor = require('../../')(process.stdout) function beep () { cursor.beep() setTimeout(beep, 1000 - (new Date()).getMilliseconds()) } setTimeout(beep, 1000 - (new Date()).getMilliseconds()) ansi.js-0.3.0/examples/clear/000077500000000000000000000000001233321167600157675ustar00rootroot00000000000000ansi.js-0.3.0/examples/clear/index.js000077500000000000000000000005441233321167600174420ustar00rootroot00000000000000#!/usr/bin/env node /** * Like GNU ncurses "clear" command. * https://github.com/mscdex/node-ncurses/blob/master/deps/ncurses/progs/clear.c */ process.title = 'clear' function lf () { return '\n' } require('../../')(process.stdout) .write(Array.apply(null, Array(process.stdout.getWindowSize()[1])).map(lf).join('')) .eraseData(2) .goto(1, 1) ansi.js-0.3.0/examples/cursorPosition.js000077500000000000000000000012001233321167600202750ustar00rootroot00000000000000#!/usr/bin/env node var tty = require('tty') var cursor = require('../')(process.stdout) // listen for the queryPosition report on stdin process.stdin.resume() raw(true) process.stdin.once('data', function (b) { var match = /\[(\d+)\;(\d+)R$/.exec(b.toString()) if (match) { var xy = match.slice(1, 3).reverse().map(Number) console.error(xy) } // cleanup and close stdin raw(false) process.stdin.pause() }) // send the query position request code to stdout cursor.queryPosition() function raw (mode) { if (process.stdin.setRawMode) { process.stdin.setRawMode(mode) } else { tty.setRawMode(mode) } } ansi.js-0.3.0/examples/progress/000077500000000000000000000000001233321167600165455ustar00rootroot00000000000000ansi.js-0.3.0/examples/progress/index.js000066400000000000000000000032741233321167600202200ustar00rootroot00000000000000#!/usr/bin/env node var assert = require('assert') , ansi = require('../../') function Progress (stream, width) { this.cursor = ansi(stream) this.delta = this.cursor.newlines this.width = width | 0 || 10 this.open = '[' this.close = ']' this.complete = '█' this.incomplete = '_' // initial render this.progress = 0 } Object.defineProperty(Progress.prototype, 'progress', { get: get , set: set , configurable: true , enumerable: true }) function get () { return this._progress } function set (v) { this._progress = Math.max(0, Math.min(v, 100)) var w = this.width - this.complete.length - this.incomplete.length , n = w * (this._progress / 100) | 0 , i = w - n , com = c(this.complete, n) , inc = c(this.incomplete, i) , delta = this.cursor.newlines - this.delta assert.equal(com.length + inc.length, w) if (delta > 0) { this.cursor.up(delta) this.delta = this.cursor.newlines } this.cursor .horizontalAbsolute(0) .eraseLine(2) .fg.white() .write(this.open) .fg.grey() .bold() .write(com) .resetBold() .write(inc) .fg.white() .write(this.close) .fg.reset() .write('\n') } function c (char, length) { return Array.apply(null, Array(length)).map(function () { return char }).join('') } // Usage var width = parseInt(process.argv[2], 10) || process.stdout.getWindowSize()[0] / 2 , p = new Progress(process.stdout, width) ;(function tick () { p.progress += Math.random() * 5 p.cursor .eraseLine(2) .write('Progress: ') .bold().write(p.progress.toFixed(2)) .write('%') .resetBold() .write('\n') if (p.progress < 100) setTimeout(tick, 100) })() ansi.js-0.3.0/lib/000077500000000000000000000000001233321167600136315ustar00rootroot00000000000000ansi.js-0.3.0/lib/ansi.js000066400000000000000000000174341233321167600151320ustar00rootroot00000000000000 /** * References: * * - http://en.wikipedia.org/wiki/ANSI_escape_code * - http://www.termsys.demon.co.uk/vtansi.htm * */ /** * Module dependencies. */ var emitNewlineEvents = require('./newlines') , prefix = '\x1b[' // For all escape codes , suffix = 'm' // Only for color codes /** * The ANSI escape sequences. */ var codes = { up: 'A' , down: 'B' , forward: 'C' , back: 'D' , nextLine: 'E' , previousLine: 'F' , horizontalAbsolute: 'G' , eraseData: 'J' , eraseLine: 'K' , scrollUp: 'S' , scrollDown: 'T' , savePosition: 's' , restorePosition: 'u' , queryPosition: '6n' , hide: '?25l' , show: '?25h' } /** * Rendering ANSI codes. */ var styles = { bold: 1 , italic: 3 , underline: 4 , inverse: 7 } /** * The negating ANSI code for the rendering modes. */ var reset = { bold: 22 , italic: 23 , underline: 24 , inverse: 27 } /** * The standard, styleable ANSI colors. */ var colors = { white: 37 , black: 30 , blue: 34 , cyan: 36 , green: 32 , magenta: 35 , red: 31 , yellow: 33 , grey: 90 , brightBlack: 90 , brightRed: 91 , brightGreen: 92 , brightYellow: 93 , brightBlue: 94 , brightMagenta: 95 , brightCyan: 96 , brightWhite: 97 } /** * Creates a Cursor instance based off the given `writable stream` instance. */ function ansi (stream, options) { if (stream._ansicursor) { return stream._ansicursor } else { return stream._ansicursor = new Cursor(stream, options) } } module.exports = exports = ansi /** * The `Cursor` class. */ function Cursor (stream, options) { if (!(this instanceof Cursor)) { return new Cursor(stream, options) } if (typeof stream != 'object' || typeof stream.write != 'function') { throw new Error('a valid Stream instance must be passed in') } // the stream to use this.stream = stream // when 'enabled' is false then all the functions are no-ops except for write() this.enabled = options && options.enabled if (typeof this.enabled === 'undefined') { this.enabled = stream.isTTY } this.enabled = !!this.enabled // then `buffering` is true, then `write()` calls are buffered in // memory until `flush()` is invoked this.buffering = !!(options && options.buffering) this._buffer = [] // controls the foreground and background colors this.fg = this.foreground = new Colorer(this, 0) this.bg = this.background = new Colorer(this, 10) // defaults this.Bold = false this.Italic = false this.Underline = false this.Inverse = false // keep track of the number of "newlines" that get encountered this.newlines = 0 emitNewlineEvents(stream) stream.on('newline', function () { this.newlines++ }.bind(this)) } exports.Cursor = Cursor /** * Helper function that calls `write()` on the underlying Stream. * Returns `this` instead of the write() return value to keep * the chaining going. */ Cursor.prototype.write = function (data) { if (this.buffering) { this._buffer.push(arguments) } else { this.stream.write.apply(this.stream, arguments) } return this } /** * Buffer `write()` calls into memory. * * @api public */ Cursor.prototype.buffer = function () { this.buffering = true return this } /** * Write out the in-memory buffer. * * @api public */ Cursor.prototype.flush = function () { this.buffering = false var str = this._buffer.map(function (args) { if (args.length != 1) throw new Error('unexpected args length! ' + args.length); return args[0]; }).join(''); this._buffer.splice(0); // empty this.write(str); return this } /** * The `Colorer` class manages both the background and foreground colors. */ function Colorer (cursor, base) { this.current = null this.cursor = cursor this.base = base } exports.Colorer = Colorer /** * Write an ANSI color code, ensuring that the same code doesn't get rewritten. */ Colorer.prototype._setColorCode = function setColorCode (code) { var c = String(code) if (this.current === c) return this.cursor.enabled && this.cursor.write(prefix + c + suffix) this.current = c return this } /** * Set up the positional ANSI codes. */ Object.keys(codes).forEach(function (name) { var code = String(codes[name]) Cursor.prototype[name] = function () { var c = code if (arguments.length > 0) { c = toArray(arguments).map(Math.round).join(';') + code } this.enabled && this.write(prefix + c) return this } }) /** * Set up the functions for the rendering ANSI codes. */ Object.keys(styles).forEach(function (style) { var name = style[0].toUpperCase() + style.substring(1) , c = styles[style] , r = reset[style] Cursor.prototype[style] = function () { if (this[name]) return this.enabled && this.write(prefix + c + suffix) this[name] = true return this } Cursor.prototype['reset' + name] = function () { if (!this[name]) return this.enabled && this.write(prefix + r + suffix) this[name] = false return this } }) /** * Setup the functions for the standard colors. */ Object.keys(colors).forEach(function (color) { var code = colors[color] Colorer.prototype[color] = function () { this._setColorCode(this.base + code) return this.cursor } Cursor.prototype[color] = function () { return this.foreground[color]() } }) /** * Makes a beep sound! */ Cursor.prototype.beep = function () { this.enabled && this.write('\x07') return this } /** * Moves cursor to specific position */ Cursor.prototype.goto = function (x, y) { x = x | 0 y = y | 0 this.enabled && this.write(prefix + y + ';' + x + 'H') return this } /** * Resets the color. */ Colorer.prototype.reset = function () { this._setColorCode(this.base + 39) return this.cursor } /** * Resets all ANSI formatting on the stream. */ Cursor.prototype.reset = function () { this.enabled && this.write(prefix + '0' + suffix) this.Bold = false this.Italic = false this.Underline = false this.Inverse = false this.foreground.current = null this.background.current = null return this } /** * Sets the foreground color with the given RGB values. * The closest match out of the 216 colors is picked. */ Colorer.prototype.rgb = function (r, g, b) { var base = this.base + 38 , code = rgb(r, g, b) this._setColorCode(base + ';5;' + code) return this.cursor } /** * Same as `cursor.fg.rgb(r, g, b)`. */ Cursor.prototype.rgb = function (r, g, b) { return this.foreground.rgb(r, g, b) } /** * Accepts CSS color codes for use with ANSI escape codes. * For example: `#FF000` would be bright red. */ Colorer.prototype.hex = function (color) { return this.rgb.apply(this, hex(color)) } /** * Same as `cursor.fg.hex(color)`. */ Cursor.prototype.hex = function (color) { return this.foreground.hex(color) } // UTIL FUNCTIONS // /** * Translates a 255 RGB value to a 0-5 ANSI RGV value, * then returns the single ANSI color code to use. */ function rgb (r, g, b) { var red = r / 255 * 5 , green = g / 255 * 5 , blue = b / 255 * 5 return rgb5(red, green, blue) } /** * Turns rgb 0-5 values into a single ANSI color code to use. */ function rgb5 (r, g, b) { var red = Math.round(r) , green = Math.round(g) , blue = Math.round(b) return 16 + (red*36) + (green*6) + blue } /** * Accepts a hex CSS color code string (# is optional) and * translates it into an Array of 3 RGB 0-255 values, which * can then be used with rgb(). */ function hex (color) { var c = color[0] === '#' ? color.substring(1) : color , r = c.substring(0, 2) , g = c.substring(2, 4) , b = c.substring(4, 6) return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)] } /** * Turns an array-like object into a real array. */ function toArray (a) { var i = 0 , l = a.length , rtn = [] for (; i 0) { var len = data.length , i = 0 // now try to calculate any deltas if (typeof data == 'string') { for (; i (http://tootallnate.net)", "repository": { "type": "git", "url": "git://github.com/TooTallNate/ansi.js.git" }, "main": "./lib/ansi.js" }