├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── terminal.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | terminal.gif 30 | example.js 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Anthony Foster 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-color-readline 2 | Node.js 'readline' alternative with support for coloured syntax highlighting and suggestions. 3 | 4 | ```sh 5 | npm install --save node-color-readline 6 | ``` 7 | 8 | Usage: 9 | 10 | ```js 11 | 12 | var colorReadline = require('node-color-readline'); 13 | var chalk = require('chalk'); 14 | var repl = colorReadline.createInterface({ 15 | input: process.stdin, 16 | output: process.stdout, 17 | colorize: function (str) { 18 | // Make all occurences of 'e' red. 19 | return str.replace(/e/g, function (match) { 20 | return chalk.red(match); 21 | }); 22 | } 23 | }); 24 | 25 | repl.on('line', function (cmd) { 26 | console.log('LINE:', cmd); 27 | }); 28 | 29 | repl.prompt(); 30 | ``` 31 | 32 | ![Screenshot](./terminal.gif) 33 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var colorReadline = require('./'); 4 | var chalk = require('chalk'); 5 | var repl = colorReadline.createInterface({ 6 | input: process.stdin, 7 | output: process.stdout, 8 | colorize: function (str) { 9 | return str.replace(/e/g, function (match) { 10 | return chalk.red(match); 11 | }); 12 | } 13 | }); 14 | 15 | repl.on('line', function (cmd) { 16 | console.log('LINE:', cmd); 17 | }); 18 | 19 | repl.prompt(); 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var ansi = require('ansi'); 4 | var util = require('util'); 5 | var chalk = require('chalk'); 6 | var ReadlineInterface = require('readline').Interface; 7 | 8 | exports.createInterface = function (options) { return new Interface(options); }; 9 | 10 | exports.Interface = Interface; 11 | 12 | util.inherits(Interface, ReadlineInterface); 13 | function Interface(options) { 14 | if (!(this instanceof Interface)) { 15 | return new Interface(options); 16 | } 17 | 18 | this.suggest = (options && options.suggest) || function () { 19 | return null; 20 | }; 21 | 22 | this.colorize = (options && options.colorize) || function (str) { 23 | return str; 24 | }; 25 | 26 | this._ansiCursor = ansi(options && options.output); 27 | ReadlineInterface.call(this, options); 28 | } 29 | 30 | Interface.prototype._originalWriteToOutput = ReadlineInterface.prototype._writeToOutput; 31 | Interface.prototype._writeToOutput = function (stringToWrite) { 32 | if (stringToWrite === '\r\n' || stringToWrite === ' ') { 33 | this.output.write(stringToWrite); 34 | return; 35 | } 36 | if (!stringToWrite) return; 37 | 38 | var startsWithPrompt = stringToWrite.indexOf(this._prompt) === 0; 39 | if (startsWithPrompt) { 40 | this.output.write(this._prompt); 41 | stringToWrite = stringToWrite.substring(this._prompt.length); 42 | renderCurrentLine(this, stringToWrite, true); 43 | } else { 44 | this._originalWriteToOutput(stringToWrite); 45 | } 46 | }; 47 | 48 | 49 | Interface.prototype._insertString = function(c) { 50 | if (this.cursor < this.line.length) { 51 | var beg = this.line.slice(0, this.cursor); 52 | var end = this.line.slice(this.cursor, this.line.length); 53 | this.line = beg + c + end; 54 | this.cursor += c.length; 55 | this._refreshLine(); 56 | } else { 57 | this.line += c; 58 | this.cursor += c.length; 59 | this._refreshLine(); 60 | this._moveCursor(0); 61 | } 62 | }; 63 | 64 | function renderCurrentLine(self, stringToWrite, showSuggestions) { 65 | var suggestionPromise = showSuggestions ? self.suggest(stringToWrite) : null; 66 | if (suggestionPromise && typeof suggestionPromise.then === 'function') { 67 | suggestionPromise.then(afterSuggestion, function (err) { 68 | process.nextTick(function () { throw err; }); 69 | }); 70 | } else { 71 | afterSuggestion(suggestionPromise); 72 | } 73 | function afterSuggestion(suggestion) { 74 | var promptLength = self._prompt.length; 75 | var cursorPos = self._getCursorPos(); 76 | var nX = cursorPos.cols; 77 | if (suggestion && suggestion.indexOf(stringToWrite) === 0) { 78 | self._ansiCursor.horizontalAbsolute(promptLength + 1).eraseLine().write(self.colorize(stringToWrite) + chalk.grey(suggestion.substring(stringToWrite.length))); 79 | self._ansiCursor.horizontalAbsolute(nX + 1); 80 | } else { 81 | self._ansiCursor.horizontalAbsolute(promptLength + 1).eraseLine().write(self.colorize(stringToWrite)); 82 | self._ansiCursor.horizontalAbsolute(nX); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-color-readline", 3 | "version": "1.0.1", 4 | "description": "Node.js 'readline' alternative with support for coloured syntax highlighting and suggestions", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/aantthony/node-color-readline.git" 12 | }, 13 | "keywords": [ 14 | "readline", 15 | "highlight", 16 | "syntax", 17 | "colors", 18 | "repl" 19 | ], 20 | "eslintConfig": { 21 | "env": { 22 | "node": true, 23 | "mocha": true 24 | }, 25 | "rules": { 26 | "consistent-return": 0, 27 | "no-return-assign": 0, 28 | "key-spacing": 0, 29 | "no-multi-spaces": 0, 30 | "no-underscore-dangle": 0, 31 | "no-use-before-define": [ 32 | 2, 33 | "nofunc" 34 | ], 35 | "curly": [ 36 | 2, 37 | "multi-line" 38 | ], 39 | "quotes": [ 40 | 2, 41 | "single" 42 | ] 43 | } 44 | }, 45 | "author": "Anthony Foster", 46 | "license": "MIT", 47 | "bugs": { 48 | "url": "https://github.com/aantthony/node-color-readline/issues" 49 | }, 50 | "homepage": "https://github.com/aantthony/node-color-readline#readme", 51 | "dependencies": { 52 | "ansi": "^0.3.0", 53 | "chalk": "^1.1.1" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /terminal.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aantthony/node-color-readline/67edf9d93af29bbec24f14837cb58273e588e69b/terminal.gif --------------------------------------------------------------------------------