fixed #1
This commit is contained in:
parent
1a136eeca7
commit
7bcbbe3ef8
3 changed files with 24 additions and 12 deletions
14
README.md
14
README.md
|
@ -52,9 +52,19 @@ readlineSync.setPrompt(encoding)
|
||||||
Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
|
Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
|
||||||
|
|
||||||
## Note
|
## Note
|
||||||
The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms.
|
The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms.
|
||||||
|
If in those platforms, an error is thrown.
|
||||||
|
|
||||||
|
```js
|
||||||
|
try {
|
||||||
|
answer = readlineSync.question('What is your favorite food? :');
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Release History
|
## Release History
|
||||||
* 2013-12-18 v0.2.1 Error handle for the environment which don't support interactively reading from stdin.
|
* 2013-12-18 v0.2.2 Error handle for the environment which don't support interactively reading from stdin.
|
||||||
* 2013-08-30 v0.2.0 Rewrite exporting methods.
|
* 2013-08-30 v0.2.0 Rewrite exporting methods.
|
||||||
* 2013-08-29 v0.1.0 Initial release.
|
* 2013-08-29 v0.1.0 Initial release.
|
||||||
|
|
|
@ -17,26 +17,28 @@ var promptText = '> ',
|
||||||
buffer = new Buffer(BUF_SIZE);
|
buffer = new Buffer(BUF_SIZE);
|
||||||
|
|
||||||
var _readlineSync = function(display) {
|
var _readlineSync = function(display) {
|
||||||
var input = '', rsize;
|
var input = '', rsize, err;
|
||||||
|
|
||||||
if (display) { stdout.write(display, encoding); }
|
if (display) { stdout.write(display, encoding); }
|
||||||
|
|
||||||
stdin.resume();
|
stdin.resume();
|
||||||
while (true) {
|
while (true) {
|
||||||
rsize = 0;
|
rsize = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE);
|
rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === 'EAGAIN') {
|
if (e.code === 'EOF') { break; } // pipe
|
||||||
// Error: EAGAIN, resource temporarily unavailable
|
if (e.code === 'EAGAIN') { // EAGAIN, resource temporarily unavailable
|
||||||
if (display) { stdout.write('\n', encoding); } // Next of prompt line.
|
// util can't inherit Error.
|
||||||
console.error('Error: This machine don\'t support interactively reading from stdin.');
|
err = new Error('The platform don\'t support interactively reading from stdin');
|
||||||
process.exit(1);
|
err.errno = e.errno;
|
||||||
} else if (e.code === 'EOF') {
|
err.code = e.code;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
throw e;
|
if (display) { stdout.write('\n', encoding); } // Return from prompt line.
|
||||||
|
throw err || e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rsize === 0) { break; }
|
if (rsize === 0) { break; }
|
||||||
input += buffer.toString(encoding, 0, rsize);
|
input += buffer.toString(encoding, 0, rsize);
|
||||||
if (/[\r\n]$/.test(input)) { break; }
|
if (/[\r\n]$/.test(input)) { break; }
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "readline-sync",
|
"name": "readline-sync",
|
||||||
"description": "Synchronous Readline",
|
"description": "Synchronous Readline",
|
||||||
"version": "0.2.1",
|
"version": "0.2.2",
|
||||||
"homepage": "https://github.com/anseki/readline-sync",
|
"homepage": "https://github.com/anseki/readline-sync",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "anseki"
|
"name": "anseki"
|
||||||
|
|
Loading…
Reference in a new issue