Created
April 13, 2017 23:05
-
-
Save dpyro/d94bb85d284cd91ed156db0404f76e7e to your computer and use it in GitHub Desktop.
simple readline cli for node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline') | |
/** | |
* Create an active bound readline interface using `stdin` and `stdout`. | |
* | |
* @param {function(string): void} callback handler for each inputed line | |
* @returns {readline.ReadLine} the active configured interface | |
*/ | |
function createReadlineInterface(callback) { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}) | |
// More events at: https://nodejs.org/api/readline.html#readline_class_interface | |
rl.on('line', (line) => { | |
callback(line) | |
rl.prompt() | |
}) | |
rl.on('close', () => { | |
console.log('\nBye!') | |
process.exit(0) | |
}) | |
rl.prompt() | |
return rl | |
} | |
module.exports = createReadlineInterface |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment