read-cli-input
Advanced tools
Comparing version
var read = require('./'); | ||
console.log('what up ?'); | ||
read(process.stdin, 1, function (answer) { | ||
read(1, function (answer) { | ||
print(answer); | ||
console.log("What are your most favorite 3 food ?"); | ||
read(5, function (answer) { | ||
console.log("Your 3 favorite food:"); | ||
read({ lines: 3, prefix: prefix }, function (answer) { | ||
print(answer); | ||
@@ -15,4 +15,8 @@ console.log('last words before we close the program?'); | ||
function prefix (line) { | ||
process.stdout.write(' ' + (line + 1) + '.'); | ||
} | ||
function print (input) { | ||
console.log('Answer was: ', input.join(', ')); | ||
} |
45
index.js
@@ -5,20 +5,22 @@ var onKeyPress = require("on-key-press"); | ||
function read (io, lines, callback) { | ||
if (arguments.length == 2) { | ||
callback = lines; | ||
lines = Number(io); | ||
io = process.stdin; | ||
} else if (arguments.length == 1) { | ||
callback = io; | ||
io = process.stdin; | ||
lines = 1; | ||
function read (options, callback) { | ||
if (arguments.length == 1) { | ||
callback = options; | ||
options = undefined; | ||
} | ||
options = normalizeOptions(options); | ||
var input = ''; | ||
var lines = options.lines; | ||
onKeyPress(io, each, function () { | ||
onKeyPress(options.stdin, each, function () { | ||
callback(rows(input)); | ||
}); | ||
if (options.prefix) options.prefix(options.lines - lines); | ||
function each (ch, key, done) { | ||
input += ch; | ||
if (key && key.name == 'enter') { | ||
@@ -28,6 +30,6 @@ if (--lines <= 0) { | ||
} | ||
if (options.prefix) options.prefix(options.lines - lines); | ||
} | ||
input += ch; | ||
if (/(^|\n)\n\n$/.test(input)) { | ||
@@ -41,2 +43,3 @@ done(); | ||
return input | ||
.trim() | ||
.split(/\s*\n+/) | ||
@@ -50,1 +53,19 @@ .map(function (row) { | ||
} | ||
function normalizeOptions (options) { | ||
if (!options) { | ||
options = { | ||
lines: 1 | ||
}; | ||
} | ||
if (typeof options == 'number') { | ||
options = { | ||
lines: options | ||
}; | ||
} | ||
options.stdin || (options.stdin = process.stdin); | ||
return options; | ||
} |
{ | ||
"name": "read-cli-input", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Read user input from CLI for specified amount of lines.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -24,7 +24,10 @@ ## read-cli-input | ||
By default, it reads from `process.stdin`. You can specify custom IO if you pass | ||
three arguments; | ||
By default, it reads from `process.stdin`. You can specify custom stdin, and a function to generate a prefix for multi-line inputs. | ||
```js | ||
read(process.stdin, 3, console.log); | ||
read({ stdin: process.stdin, lines: 3, prefix: prefix }, console.log); | ||
function prefix (line) { | ||
process.stdout.write(line + 1 + '.'); // It'll output "1." "2." "3." ... | ||
} | ||
``` | ||
@@ -31,0 +34,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2848
26.92%69
32.69%1
-50%39
8.33%