├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── console-test.js ├── console-test.txt ├── index.js ├── package.json └── test └── console-strings.js /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "7" 5 | - "6" 6 | - "4" 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Rebecca Turner 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Console Control Strings 2 | 3 | A library of cross-platform tested terminal/console command strings for 4 | doing things like color and cursor positioning. This is a subset of both 5 | ansi and vt100. All control codes included work on both Windows & Unix-like 6 | OSes, except where noted. 7 | 8 | ## Usage 9 | 10 | ```js 11 | var consoleControl = require('console-control-strings') 12 | 13 | console.log(consoleControl.color('blue','bgRed', 'bold') + 'hi there' + consoleControl.color('reset')) 14 | process.stdout.write(consoleControl.goto(75, 10)) 15 | ``` 16 | 17 | ## Why Another? 18 | 19 | There are tons of libraries similar to this one. I wanted one that was: 20 | 21 | 1. Very clear about compatibility goals. 22 | 2. Could emit, for instance, a start color code without an end one. 23 | 3. Returned strings w/o writing to streams. 24 | 4. Was not weighed down with other unrelated baggage. 25 | 26 | ## Functions 27 | 28 | ### var code = consoleControl.up(_num = 1_) 29 | 30 | Returns the escape sequence to move _num_ lines up. 31 | 32 | ### var code = consoleControl.down(_num = 1_) 33 | 34 | Returns the escape sequence to move _num_ lines down. 35 | 36 | ### var code = consoleControl.forward(_num = 1_) 37 | 38 | Returns the escape sequence to move _num_ lines righ. 39 | 40 | ### var code = consoleControl.back(_num = 1_) 41 | 42 | Returns the escape sequence to move _num_ lines left. 43 | 44 | ### var code = consoleControl.nextLine(_num = 1_) 45 | 46 | Returns the escape sequence to move _num_ lines down and to the beginning of 47 | the line. 48 | 49 | ### var code = consoleControl.previousLine(_num = 1_) 50 | 51 | Returns the escape sequence to move _num_ lines up and to the beginning of 52 | the line. 53 | 54 | ### var code = consoleControl.eraseData() 55 | 56 | Returns the escape sequence to erase everything from the current cursor 57 | position to the bottom right of the screen. This is line based, so it 58 | erases the remainder of the current line and all following lines. 59 | 60 | ### var code = consoleControl.eraseLine() 61 | 62 | Returns the escape sequence to erase to the end of the current line. 63 | 64 | ### var code = consoleControl.goto(_x_, _y_) 65 | 66 | Returns the escape sequence to move the cursor to the designated position. 67 | Note that the origin is _1, 1_ not _0, 0_. 68 | 69 | ### var code = consoleControl.gotoSOL() 70 | 71 | Returns the escape sequence to move the cursor to the beginning of the 72 | current line. (That is, it returns a carriage return, `\r`.) 73 | 74 | ### var code = consoleControl.beep() 75 | 76 | Returns the escape sequence to cause the termianl to beep. (That is, it 77 | returns unicode character `\x0007`, a Control-G.) 78 | 79 | ### var code = consoleControl.hideCursor() 80 | 81 | Returns the escape sequence to hide the cursor. 82 | 83 | ### var code = consoleControl.showCursor() 84 | 85 | Returns the escape sequence to show the cursor. 86 | 87 | ### var code = consoleControl.color(_colors = []_) 88 | 89 | ### var code = consoleControl.color(_color1_, _color2_, _…_, _colorn_) 90 | 91 | Returns the escape sequence to set the current terminal display attributes 92 | (mostly colors). Arguments can either be a list of attributes or an array 93 | of attributes. The difference between passing in an array or list of colors 94 | and calling `.color` separately for each one, is that in the former case a 95 | single escape sequence will be produced where as in the latter each change 96 | will have its own distinct escape sequence. Each attribute can be one of: 97 | 98 | * Reset: 99 | * **reset** – Reset all attributes to the terminal default. 100 | * Styles: 101 | * **bold** – Display text as bold. In some terminals this means using a 102 | bold font, in others this means changing the color. In some it means 103 | both. 104 | * **italic** – Display text as italic. This is not available in most Windows terminals. 105 | * **underline** – Underline text. This is not available in most Windows Terminals. 106 | * **inverse** – Invert the foreground and background colors. 107 | * **stopBold** – Do not display text as bold. 108 | * **stopItalic** – Do not display text as italic. 109 | * **stopUnderline** – Do not underline text. 110 | * **stopInverse** – Do not invert foreground and background. 111 | * Colors: 112 | * **white** 113 | * **black** 114 | * **blue** 115 | * **cyan** 116 | * **green** 117 | * **magenta** 118 | * **red** 119 | * **yellow** 120 | * **grey** / **brightBlack** 121 | * **brightRed** 122 | * **brightGreen** 123 | * **brightYellow** 124 | * **brightBlue** 125 | * **brightMagenta** 126 | * **brightCyan** 127 | * **brightWhite** 128 | * Background Colors: 129 | * **bgWhite** 130 | * **bgBlack** 131 | * **bgBlue** 132 | * **bgCyan** 133 | * **bgGreen** 134 | * **bgMagenta** 135 | * **bgRed** 136 | * **bgYellow** 137 | * **bgGrey** / **bgBrightBlack** 138 | * **bgBrightRed** 139 | * **bgBrightGreen** 140 | * **bgBrightYellow** 141 | * **bgBrightBlue** 142 | * **bgBrightMagenta** 143 | * **bgBrightCyan** 144 | * **bgBrightWhite** 145 | 146 | -------------------------------------------------------------------------------- /console-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | var consoleControl = require('./index.js') 3 | 4 | process.stdout.write(consoleControl.goto(1, 1) + consoleControl.eraseData()) 5 | for (var ii = 0; ii < 20; ++ii) { 6 | process.stdout.write(consoleControl.goto(1, ii + 1) + (ii + 1) + '.') 7 | } 8 | 9 | process.stdout.write( 10 | consoleControl.forward(4) + 11 | consoleControl.up(10) + 'line 10' + 12 | consoleControl.down() + 'line 11' + 13 | consoleControl.forward(20) + 'over 20' + 14 | consoleControl.back(17) + 'back 17' + 15 | consoleControl.horizontalAbsolute(5) + 'start!\n' + 16 | consoleControl.goto(10, 12) + 'garbage garbage' + 17 | consoleControl.goto(10, 12) + consoleControl.eraseLine() + 18 | consoleControl.nextLine(3) + '15# ' + 19 | consoleControl.previousLine(1) + '14# ' + 20 | consoleControl.goto(10, 23) + 'actual line 23' + 21 | consoleControl.goto(4, 1) + '456789!123456789@123456789#1234567890$123456789%123456789^123456789&123456789' + 22 | consoleControl.goto(4, 2) + 23 | consoleControl.color('bold') + 'bold' + consoleControl.color('reset') + ' ' + 24 | consoleControl.color('italic') + 'italic' + consoleControl.color('reset') + ' ' + 25 | consoleControl.color('underline') + 'underline' + consoleControl.color('reset') + ' ' + 26 | consoleControl.color('inverse') + 'inverse' + consoleControl.color('reset') + ' ' + 27 | 28 | consoleControl.color('bold') + 'n' + consoleControl.color('stopBold') + 'b ' + 29 | consoleControl.color('italic') + 'n' + consoleControl.color('stopItalic') + 'i ' + 30 | consoleControl.color('underline') + 'n' + consoleControl.color('stopUnderline') + 'u ' + 31 | consoleControl.color('inverse') + 'i' + consoleControl.color('stopInverse') + 'i' + 32 | 33 | consoleControl.goto(4, 3) + 34 | consoleControl.color('white') + 'white ' + 35 | consoleControl.color('black') + 'black ' + 36 | consoleControl.color('blue') + 'blue ' + 37 | consoleControl.color('cyan') + 'cyan ' + 38 | consoleControl.color('green') + 'green ' + 39 | consoleControl.color('magenta') + 'magenta ' + 40 | consoleControl.color('red') + 'red ' + 41 | consoleControl.color('yellow') + 'yellow ' + 42 | consoleControl.color('brightWhite') + 'brightWhite ' + 43 | consoleControl.color('brightBlack') + 'brightBlack ' + 44 | consoleControl.goto(4, 4) + 45 | consoleControl.color('brightBlue') + 'brightBlue ' + 46 | consoleControl.color('brightCyan') + 'brightCyan ' + 47 | consoleControl.color('brightGreen') + 'brightGreen ' + 48 | consoleControl.color('brightMagenta') + 'brightMagenta ' + 49 | consoleControl.color('brightRed') + 'brightRed ' + 50 | consoleControl.color('brightYellow') + 'brightYellow ' + consoleControl.color('reset') + 51 | 52 | consoleControl.goto(4, 5) + 53 | consoleControl.color('bgWhite') + 'white ' + 54 | consoleControl.color('bgBlack') + 'black ' + 55 | consoleControl.color('bgBlue') + 'blue ' + 56 | consoleControl.color('bgCyan') + 'cyan ' + 57 | consoleControl.color('bgGreen') + 'green ' + 58 | consoleControl.color('bgMagenta') + 'magenta ' + 59 | consoleControl.color('bgRed') + 'red ' + 60 | consoleControl.color('bgYellow') + 'yellow ' + 61 | consoleControl.color('bgBrightWhite') + 'brightWhite ' + 62 | consoleControl.color('bgBrightBlack') + 'brightBlack ' + 63 | consoleControl.goto(4, 6) + 64 | consoleControl.color('bgBrightBlue') + 'brightBlue ' + 65 | consoleControl.color('bgBrightCyan') + 'brightCyan ' + 66 | consoleControl.color('bgBrightGreen') + 'brightGreen ' + 67 | consoleControl.color('bgBrightMagenta') + 'brightMagenta ' + 68 | consoleControl.color('bgBrightRed') + 'brightRed ' + 69 | consoleControl.color('bgBrightYellow') + 'brightYellow ' + consoleControl.color('reset') + 70 | consoleControl.goto(1, 24) 71 | ) 72 | -------------------------------------------------------------------------------- /console-test.txt: -------------------------------------------------------------------------------- 1 | 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.line 10line 11over 20back 17start! 2 | 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  -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | // These tables borrowed from `ansi` 4 | 5 | var prefix = '\x1b[' 6 | 7 | exports.up = function up (num) { 8 | return prefix + (num || '') + 'A' 9 | } 10 | 11 | exports.down = function down (num) { 12 | return prefix + (num || '') + 'B' 13 | } 14 | 15 | exports.forward = function forward (num) { 16 | return prefix + (num || '') + 'C' 17 | } 18 | 19 | exports.back = function back (num) { 20 | return prefix + (num || '') + 'D' 21 | } 22 | 23 | exports.nextLine = function nextLine (num) { 24 | return prefix + (num || '') + 'E' 25 | } 26 | 27 | exports.previousLine = function previousLine (num) { 28 | return prefix + (num || '') + 'F' 29 | } 30 | 31 | exports.horizontalAbsolute = function horizontalAbsolute (num) { 32 | if (num == null) throw new Error('horizontalAboslute requires a column to position to') 33 | return prefix + num + 'G' 34 | } 35 | 36 | exports.eraseData = function eraseData () { 37 | return prefix + 'J' 38 | } 39 | 40 | exports.eraseLine = function eraseLine () { 41 | return prefix + 'K' 42 | } 43 | 44 | exports.goto = function (x, y) { 45 | return prefix + y + ';' + x + 'H' 46 | } 47 | 48 | exports.gotoSOL = function () { 49 | return '\r' 50 | } 51 | 52 | exports.beep = function () { 53 | return '\x07' 54 | } 55 | 56 | exports.hideCursor = function hideCursor () { 57 | return prefix + '?25l' 58 | } 59 | 60 | exports.showCursor = function showCursor () { 61 | return prefix + '?25h' 62 | } 63 | 64 | var colors = { 65 | reset: 0, 66 | // styles 67 | bold: 1, 68 | italic: 3, 69 | underline: 4, 70 | inverse: 7, 71 | // resets 72 | stopBold: 22, 73 | stopItalic: 23, 74 | stopUnderline: 24, 75 | stopInverse: 27, 76 | // colors 77 | white: 37, 78 | black: 30, 79 | blue: 34, 80 | cyan: 36, 81 | green: 32, 82 | magenta: 35, 83 | red: 31, 84 | yellow: 33, 85 | bgWhite: 47, 86 | bgBlack: 40, 87 | bgBlue: 44, 88 | bgCyan: 46, 89 | bgGreen: 42, 90 | bgMagenta: 45, 91 | bgRed: 41, 92 | bgYellow: 43, 93 | 94 | grey: 90, 95 | brightBlack: 90, 96 | brightRed: 91, 97 | brightGreen: 92, 98 | brightYellow: 93, 99 | brightBlue: 94, 100 | brightMagenta: 95, 101 | brightCyan: 96, 102 | brightWhite: 97, 103 | 104 | bgGrey: 100, 105 | bgBrightBlack: 100, 106 | bgBrightRed: 101, 107 | bgBrightGreen: 102, 108 | bgBrightYellow: 103, 109 | bgBrightBlue: 104, 110 | bgBrightMagenta: 105, 111 | bgBrightCyan: 106, 112 | bgBrightWhite: 107 113 | } 114 | 115 | exports.color = function color (colorWith) { 116 | if (arguments.length !== 1 || !Array.isArray(colorWith)) { 117 | colorWith = Array.prototype.slice.call(arguments) 118 | } 119 | return prefix + colorWith.map(colorNameToCode).join(';') + 'm' 120 | } 121 | 122 | function colorNameToCode (color) { 123 | if (colors[color] != null) return colors[color] 124 | throw new Error('Unknown color or style name: ' + color) 125 | } 126 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "console-control-strings", 3 | "version": "1.1.0", 4 | "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.", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "standard && tap test/*.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/iarna/console-control-strings" 15 | }, 16 | "keywords": [], 17 | "author": "Rebecca Turner (http://re-becca.org/)", 18 | "license": "ISC", 19 | "files": [ 20 | "LICENSE", 21 | "index.js" 22 | ], 23 | "devDependencies": { 24 | "standard": "^7.1.2", 25 | "tap": "^5.7.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/console-strings.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | var test = require('tap').test 3 | var consoleControl = require('../index.js') 4 | 5 | test('consoleControl', function (t) { 6 | var oneoptarg = { 7 | up: 'A', 8 | down: 'B', 9 | forward: 'C', 10 | back: 'D', 11 | nextLine: 'E', 12 | previousLine: 'F' 13 | } 14 | Object.keys(oneoptarg).forEach(function (move) { 15 | t.is(consoleControl[move](), '\x1b[' + oneoptarg[move], move) 16 | t.is(consoleControl[move](10), '\x1b[10' + oneoptarg[move], move + ' 10') 17 | }) 18 | var noargs = { 19 | eraseData: 'J', 20 | eraseLine: 'K', 21 | hideCursor: '?25l', 22 | showCursor: '?25h' 23 | } 24 | Object.keys(noargs).forEach(function (move) { 25 | t.is(consoleControl[move](), '\x1b[' + noargs[move], move) 26 | }) 27 | t.is(consoleControl.horizontalAbsolute(10), '\x1b[10G', 'horizontalAbsolute 10') 28 | t.is(consoleControl.horizontalAbsolute(0), '\x1b[0G', 'horizontalAbsolute 0') 29 | try { 30 | consoleControl.horizontalAbsolute() 31 | t.fail('horizontalAbsolute') 32 | } catch (e) { 33 | t.pass('horizontalAbsolute') 34 | } 35 | t.is(consoleControl.color('bold', 'white', 'bgBlue'), '\x1b[1;37;44m', 'set color') 36 | try { 37 | consoleControl.color('bold', 'invalid', 'blue') 38 | t.fail('set invalid color') 39 | } catch (e) { 40 | t.pass('set invalid color') 41 | } 42 | t.is(consoleControl.goto(10, 3), '\x1b[3;10H', 'absolute position') 43 | t.is(consoleControl.gotoSOL(), '\r', 'goto start of line') 44 | t.is(consoleControl.beep(), '\x07', 'beep beeps') 45 | t.done() 46 | }) 47 | --------------------------------------------------------------------------------