Add: setBufferSize

This commit is contained in:
anseki 2015-02-22 20:57:07 +09:00
parent 0c503e3ffa
commit c1255a429c
2 changed files with 13 additions and 6 deletions

View file

@ -37,7 +37,7 @@ answer = readlineSync.question([query[, options]])
Displays the `query` to the user, and then returns the user's response after it has been typed. Displays the `query` to the user, and then returns the user's response after it has been typed.
You can specify `options`. (see [Options](#options)) You can specify `options`. (see [Options](#options))
The `query` may be string, or may not be (e.g. number, Date, Object, etc.). This is converted to string (i.e. `toString` method is called) before it is displayed every time. The `query` may be string, or may not be (e.g. number, Date, Object, etc.). This is converted to string (i.e. `toString` method is called) before it is displayed.
### prompt ### prompt
@ -127,7 +127,7 @@ console.log('Login ...');
The typed text is not shown on screen. The typed text is not shown on screen.
```shell ```
PASSWORD : PASSWORD :
Login ... Login ...
``` ```

View file

@ -9,7 +9,6 @@
'use strict'; 'use strict';
var var
BUF_SIZE = 256,
IS_WIN = process.platform === 'win32', IS_WIN = process.platform === 'win32',
SHELL_PATH = IS_WIN ? 'cmd.exe' : '/bin/sh', SHELL_PATH = IS_WIN ? 'cmd.exe' : '/bin/sh',
@ -20,14 +19,15 @@ var
childProc = require('child_process'), childProc = require('child_process'),
stdin = process.stdin, stdin = process.stdin,
stdout = process.stdout, stdout = process.stdout,
buffer = new Buffer(BUF_SIZE),
promptText = '> ', promptText = '> ',
encoding = 'utf8', encoding = 'utf8',
bufSize = 256,
useShell = true, print, tempdir, salt = 0; useShell = true, print, tempdir, salt = 0;
function _readlineSync(display, options) { function _readlineSync(display, options) {
var input = '', rsize, err; var input = '', buffer = new Buffer(bufSize),
rsize, err;
if (display !== '') { // null and undefined were excluded. if (display !== '') { // null and undefined were excluded.
if (typeof print === 'function') { print(display, encoding); } if (typeof print === 'function') { print(display, encoding); }
@ -49,7 +49,7 @@ function _readlineSync(display, options) {
rsize = 0; rsize = 0;
try { try {
rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE); rsize = fs.readSync(stdin.fd, buffer, 0, bufSize);
} catch (e) { } catch (e) {
if (e.code === 'EOF') { break; } // pipe if (e.code === 'EOF') { break; } // pipe
@ -197,6 +197,13 @@ exports.setEncoding = function(newEncoding) {
return encoding; return encoding;
}; };
exports.setBufferSize = function(newBufSize) {
if (typeof newBufSize === 'number') {
bufSize = newBufSize;
}
return bufSize;
};
exports.prompt = function(options) { exports.prompt = function(options) {
return _readlineSync(promptText, options); return _readlineSync(promptText, options);
}; };