Skip to content

Instantly share code, notes, and snippets.

@dpyro
Created April 13, 2017 23:05
Show Gist options
  • Save dpyro/d94bb85d284cd91ed156db0404f76e7e to your computer and use it in GitHub Desktop.
Save dpyro/d94bb85d284cd91ed156db0404f76e7e to your computer and use it in GitHub Desktop.
simple readline cli for node.js
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