pax_global_header00006660000000000000000000000064127303623540014517gustar00rootroot0000000000000052 comment=722439b4998d2964ac3d3f9ec175c008aa9b7b4b console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/000077500000000000000000000000001273036235400232645ustar00rootroot00000000000000console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/.gitignore000066400000000000000000000000031273036235400252450ustar00rootroot00000000000000*~ console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/LICENSE000066400000000000000000000013571273036235400242770ustar00rootroot00000000000000Copyright (c) 2014, Rebecca Turner Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/README.md000066400000000000000000000105731273036235400245510ustar00rootroot00000000000000# Console Control Strings A library of cross-platform tested terminal/console command strings for doing things like color and cursor positioning. This is a subset of both ansi and vt100. All control codes included work on both Windows & Unix-like OSes, except where noted. ## Usage ```js var consoleControl = require('console-control-strings') console.log(consoleControl.color('blue','bgRed', 'bold') + 'hi there' + consoleControl.color('reset')) process.stdout.write(consoleControl.goto(75, 10)) ``` ## Why Another? There are tons of libraries similar to this one. I wanted one that was: 1. Very clear about compatibility goals. 2. Could emit, for instance, a start color code without an end one. 3. Returned strings w/o writing to streams. 4. Was not weighed down with other unrelated baggage. ## Functions ### var code = consoleControl.up(_num = 1_) Returns the escape sequence to move _num_ lines up. ### var code = consoleControl.down(_num = 1_) Returns the escape sequence to move _num_ lines down. ### var code = consoleControl.forward(_num = 1_) Returns the escape sequence to move _num_ lines righ. ### var code = consoleControl.back(_num = 1_) Returns the escape sequence to move _num_ lines left. ### var code = consoleControl.nextLine(_num = 1_) Returns the escape sequence to move _num_ lines down and to the beginning of the line. ### var code = consoleControl.previousLine(_num = 1_) Returns the escape sequence to move _num_ lines up and to the beginning of the line. ### var code = consoleControl.eraseData() Returns the escape sequence to erase everything from the current cursor position to the bottom right of the screen. This is line based, so it erases the remainder of the current line and all following lines. ### var code = consoleControl.eraseLine() Returns the escape sequence to erase to the end of the current line. ### var code = consoleControl.goto(_x_, _y_) Returns the escape sequence to move the cursor to the designated position. Note that the origin is _1, 1_ not _0, 0_. ### var code = consoleControl.gotoSOL() Returns the escape sequence to move the cursor to the beginning of the current line. (That is, it returns a carriage return, `\r`.) ### var code = consoleControl.beep() Returns the escape sequence to cause the termianl to beep. (That is, it returns unicode character `\x0007`, a Control-G.) ### var code = consoleControl.hideCursor() Returns the escape sequence to hide the cursor. ### var code = consoleControl.showCursor() Returns the escape sequence to show the cursor. ### var code = consoleControl.color(_colors = []_) ### var code = consoleControl.color(_color1_, _color2_, _…_, _colorn_) Returns the escape sequence to set the current terminal display attributes (mostly colors). Arguments can either be a list of attributes or an array of attributes. The difference between passing in an array or list of colors and calling `.color` separately for each one, is that in the former case a single escape sequence will be produced where as in the latter each change will have its own distinct escape sequence. Each attribute can be one of: * Reset: * **reset** – Reset all attributes to the terminal default. * Styles: * **bold** – Display text as bold. In some terminals this means using a bold font, in others this means changing the color. In some it means both. * **italic** – Display text as italic. This is not available in most Windows terminals. * **underline** – Underline text. This is not available in most Windows Terminals. * **inverse** – Invert the foreground and background colors. * **stopBold** – Do not display text as bold. * **stopItalic** – Do not display text as italic. * **stopUnderline** – Do not underline text. * **stopInverse** – Do not invert foreground and background. * Colors: * **white** * **black** * **blue** * **cyan** * **green** * **magenta** * **red** * **yellow** * **grey** / **brightBlack** * **brightRed** * **brightGreen** * **brightYellow** * **brightBlue** * **brightMagenta** * **brightCyan** * **brightWhite** * Background Colors: * **bgWhite** * **bgBlack** * **bgBlue** * **bgCyan** * **bgGreen** * **bgMagenta** * **bgRed** * **bgYellow** * **bgGrey** / **bgBrightBlack** * **bgBrightRed** * **bgBrightGreen** * **bgBrightYellow** * **bgBrightBlue** * **bgBrightMagenta** * **bgBrightCyan** * **bgBrightWhite** console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/console-test.js000066400000000000000000000065771273036235400262600ustar00rootroot00000000000000'use strict' var consoleControl = require('./index.js') process.stdout.write(consoleControl.goto(1, 1) + consoleControl.eraseData()) for (var ii = 0; ii < 20; ++ii) { process.stdout.write(consoleControl.goto(1, ii + 1) + (ii + 1) + '.') } process.stdout.write( consoleControl.forward(4) + consoleControl.up(10) + 'line 10' + consoleControl.down() + 'line 11' + consoleControl.forward(20) + 'over 20' + consoleControl.back(17) + 'back 17' + consoleControl.horizontalAbsolute(5) + 'start!\n' + consoleControl.goto(10, 12) + 'garbage garbage' + consoleControl.goto(10, 12) + consoleControl.eraseLine() + consoleControl.nextLine(3) + '15# ' + consoleControl.previousLine(1) + '14# ' + consoleControl.goto(10, 23) + 'actual line 23' + consoleControl.goto(4, 1) + '456789!123456789@123456789#1234567890$123456789%123456789^123456789&123456789' + consoleControl.goto(4, 2) + consoleControl.color('bold') + 'bold' + consoleControl.color('reset') + ' ' + consoleControl.color('italic') + 'italic' + consoleControl.color('reset') + ' ' + consoleControl.color('underline') + 'underline' + consoleControl.color('reset') + ' ' + consoleControl.color('inverse') + 'inverse' + consoleControl.color('reset') + ' ' + consoleControl.color('bold') + 'n' + consoleControl.color('stopBold') + 'b ' + consoleControl.color('italic') + 'n' + consoleControl.color('stopItalic') + 'i ' + consoleControl.color('underline') + 'n' + consoleControl.color('stopUnderline') + 'u ' + consoleControl.color('inverse') + 'i' + consoleControl.color('stopInverse') + 'i' + consoleControl.goto(4, 3) + consoleControl.color('white') + 'white ' + consoleControl.color('black') + 'black ' + consoleControl.color('blue') + 'blue ' + consoleControl.color('cyan') + 'cyan ' + consoleControl.color('green') + 'green ' + consoleControl.color('magenta') + 'magenta ' + consoleControl.color('red') + 'red ' + consoleControl.color('yellow') + 'yellow ' + consoleControl.color('brightWhite') + 'brightWhite ' + consoleControl.color('brightBlack') + 'brightBlack ' + consoleControl.goto(4, 4) + consoleControl.color('brightBlue') + 'brightBlue ' + consoleControl.color('brightCyan') + 'brightCyan ' + consoleControl.color('brightGreen') + 'brightGreen ' + consoleControl.color('brightMagenta') + 'brightMagenta ' + consoleControl.color('brightRed') + 'brightRed ' + consoleControl.color('brightYellow') + 'brightYellow ' + consoleControl.color('reset') + consoleControl.goto(4, 5) + consoleControl.color('bgWhite') + 'white ' + consoleControl.color('bgBlack') + 'black ' + consoleControl.color('bgBlue') + 'blue ' + consoleControl.color('bgCyan') + 'cyan ' + consoleControl.color('bgGreen') + 'green ' + consoleControl.color('bgMagenta') + 'magenta ' + consoleControl.color('bgRed') + 'red ' + consoleControl.color('bgYellow') + 'yellow ' + consoleControl.color('bgBrightWhite') + 'brightWhite ' + consoleControl.color('bgBrightBlack') + 'brightBlack ' + consoleControl.goto(4, 6) + consoleControl.color('bgBrightBlue') + 'brightBlue ' + consoleControl.color('bgBrightCyan') + 'brightCyan ' + consoleControl.color('bgBrightGreen') + 'brightGreen ' + consoleControl.color('bgBrightMagenta') + 'brightMagenta ' + consoleControl.color('bgBrightRed') + 'brightRed ' + consoleControl.color('bgBrightYellow') + 'brightYellow ' + consoleControl.color('reset') + consoleControl.goto(1, 24) ) console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/console-test.txt000066400000000000000000000017651273036235400264550ustar00rootroot000000000000001.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.line 10line 11over 20back 17start! garbage garbage15# 14# actual line 23456789!123456789@123456789#1234567890$123456789%123456789^123456789&123456789bold italic underline inverse nb ni nu iiwhite black blue cyan green magenta red yellow brightWhite brightBlack brightBlue brightCyan brightGreen brightMagenta brightRed brightYellow white black blue cyan green magenta red yellow brightWhite brightBlack brightBlue brightCyan brightGreen brightMagenta brightRed brightYellow console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/index.js000066400000000000000000000044431273036235400247360ustar00rootroot00000000000000'use strict' // These tables borrowed from `ansi` var prefix = '\x1b[' exports.up = function up (num) { return prefix + (num || '') + 'A' } exports.down = function down (num) { return prefix + (num || '') + 'B' } exports.forward = function forward (num) { return prefix + (num || '') + 'C' } exports.back = function back (num) { return prefix + (num || '') + 'D' } exports.nextLine = function nextLine (num) { return prefix + (num || '') + 'E' } exports.previousLine = function previousLine (num) { return prefix + (num || '') + 'F' } exports.horizontalAbsolute = function horizontalAbsolute (num) { if (num == null) throw new Error('horizontalAboslute requires a column to position to') return prefix + num + 'G' } exports.eraseData = function eraseData () { return prefix + 'J' } exports.eraseLine = function eraseLine () { return prefix + 'K' } exports.goto = function (x, y) { return prefix + y + ';' + x + 'H' } exports.gotoSOL = function () { return '\r' } exports.beep = function () { return '\x07' } exports.hideCursor = function hideCursor () { return prefix + '?25l' } exports.showCursor = function showCursor () { return prefix + '?25h' } var colors = { reset: 0, // styles bold: 1, italic: 3, underline: 4, inverse: 7, // resets stopBold: 22, stopItalic: 23, stopUnderline: 24, stopInverse: 27, // colors white: 37, black: 30, blue: 34, cyan: 36, green: 32, magenta: 35, red: 31, yellow: 33, bgWhite: 47, bgBlack: 40, bgBlue: 44, bgCyan: 46, bgGreen: 42, bgMagenta: 45, bgRed: 41, bgYellow: 43, grey: 90, brightBlack: 90, brightRed: 91, brightGreen: 92, brightYellow: 93, brightBlue: 94, brightMagenta: 95, brightCyan: 96, brightWhite: 97, bgGrey: 100, bgBrightBlack: 100, bgBrightRed: 101, bgBrightGreen: 102, bgBrightYellow: 103, bgBrightBlue: 104, bgBrightMagenta: 105, bgBrightCyan: 106, bgBrightWhite: 107 } exports.color = function color (colorWith) { if (arguments.length !== 1 || !Array.isArray(colorWith)) { colorWith = Array.prototype.slice.call(arguments) } return prefix + colorWith.map(colorNameToCode).join(';') + 'm' } function colorNameToCode (color) { if (colors[color] != null) return colors[color] throw new Error('Unknown color or style name: ' + color) } console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/package.json000066400000000000000000000014271273036235400255560ustar00rootroot00000000000000{ "name": "console-control-strings", "version": "1.1.0", "description": "A library of cross-platform tested terminal/console command strings for doing things like color and cursor positioning. This is a subset of both ansi and vt100. All control codes included work on both Windows & Unix-like OSes, except where noted.", "main": "index.js", "directories": { "test": "test" }, "scripts": { "test": "standard && tap test/*.js" }, "repository": { "type": "git", "url": "https://github.com/iarna/console-control-strings" }, "keywords": [], "author": "Rebecca Turner (http://re-becca.org/)", "license": "ISC", "files": [ "LICENSE", "index.js" ], "devDependencies": { "standard": "^7.1.2", "tap": "^5.7.2" } } console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/test/000077500000000000000000000000001273036235400242435ustar00rootroot00000000000000console-control-strings-722439b4998d2964ac3d3f9ec175c008aa9b7b4b/test/console-strings.js000066400000000000000000000025711273036235400277370ustar00rootroot00000000000000'use strict' var test = require('tap').test var consoleControl = require('../index.js') test('consoleControl', function (t) { var oneoptarg = { up: 'A', down: 'B', forward: 'C', back: 'D', nextLine: 'E', previousLine: 'F' } Object.keys(oneoptarg).forEach(function (move) { t.is(consoleControl[move](), '\x1b[' + oneoptarg[move], move) t.is(consoleControl[move](10), '\x1b[10' + oneoptarg[move], move + ' 10') }) var noargs = { eraseData: 'J', eraseLine: 'K', hideCursor: '?25l', showCursor: '?25h' } Object.keys(noargs).forEach(function (move) { t.is(consoleControl[move](), '\x1b[' + noargs[move], move) }) t.is(consoleControl.horizontalAbsolute(10), '\x1b[10G', 'horizontalAbsolute 10') t.is(consoleControl.horizontalAbsolute(0), '\x1b[0G', 'horizontalAbsolute 0') try { consoleControl.horizontalAbsolute() t.fail('horizontalAbsolute') } catch (e) { t.pass('horizontalAbsolute') } t.is(consoleControl.color('bold', 'white', 'bgBlue'), '\x1b[1;37;44m', 'set color') try { consoleControl.color('bold', 'invalid', 'blue') t.fail('set invalid color') } catch (e) { t.pass('set invalid color') } t.is(consoleControl.goto(10, 3), '\x1b[3;10H', 'absolute position') t.is(consoleControl.gotoSOL(), '\r', 'goto start of line') t.is(consoleControl.beep(), '\x07', 'beep beeps') t.done() })