2013-08-29 12:55:23 +02:00
|
|
|
/*
|
|
|
|
* readlineSync
|
|
|
|
* https://github.com/anseki/readline-sync
|
|
|
|
*
|
2015-01-26 17:11:25 +01:00
|
|
|
* Copyright (c) 2015 anseki
|
2013-08-29 12:55:23 +02:00
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-07-13 06:55:17 +02:00
|
|
|
var
|
2015-02-11 23:39:59 +01:00
|
|
|
IS_WIN = process.platform === 'win32',
|
2015-03-25 08:56:43 +01:00
|
|
|
|
2014-07-13 06:55:17 +02:00
|
|
|
ALGORITHM_CIPHER = 'aes-256-cbc',
|
|
|
|
ALGORITHM_HASH = 'sha256',
|
2015-03-06 07:23:32 +01:00
|
|
|
DEFAULT_ERR_MSG = 'The platform doesn\'t support interactive reading',
|
2014-07-13 06:55:17 +02:00
|
|
|
|
2013-08-29 19:26:22 +02:00
|
|
|
fs = require('fs'),
|
2015-03-20 04:03:58 +01:00
|
|
|
TTY = process.binding('tty_wrap').TTY,
|
2015-02-11 20:44:05 +01:00
|
|
|
childProc = require('child_process'),
|
|
|
|
|
|
|
|
promptText = '> ',
|
|
|
|
encoding = 'utf8',
|
2015-02-22 14:23:46 +01:00
|
|
|
bufSize = 1024,
|
2015-03-07 16:19:40 +01:00
|
|
|
print,
|
2015-03-20 04:03:58 +01:00
|
|
|
mask = '*',
|
2015-03-25 08:56:43 +01:00
|
|
|
useExt = false,
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
fdR = 'none', fdW, ttyR, isRawMode = false,
|
2015-03-27 12:25:59 +01:00
|
|
|
extHostPath, extHostArgs, tempdir, salt = 0;
|
2013-08-29 12:55:23 +02:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
/*
|
|
|
|
display: string
|
|
|
|
noEchoBack: boolean
|
|
|
|
mask: string
|
|
|
|
keyIn: boolean
|
|
|
|
noTrim: boolean
|
|
|
|
*/
|
|
|
|
function readlineSync(options) {
|
2015-03-25 13:22:44 +01:00
|
|
|
var input = '', displayTmp;
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2015-03-25 08:56:43 +01:00
|
|
|
function tryExt() {
|
|
|
|
var res = readlineExt(options);
|
2015-03-07 16:19:40 +01:00
|
|
|
if (res.error) { throw res.error; }
|
2015-03-20 04:03:58 +01:00
|
|
|
return res.input;
|
|
|
|
}
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var fsB, constants;
|
|
|
|
|
|
|
|
function getFsB() {
|
|
|
|
if (!fsB) {
|
|
|
|
fsB = process.binding('fs'); // For raw device path
|
|
|
|
constants = process.binding('constants');
|
|
|
|
}
|
|
|
|
return fsB;
|
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
if (typeof fdR !== 'string') { return; }
|
|
|
|
fdR = null;
|
|
|
|
|
|
|
|
if (IS_WIN) {
|
|
|
|
if (process.stdin.isTTY) {
|
|
|
|
fdR = process.stdin.fd;
|
|
|
|
ttyR = process.stdin._handle;
|
|
|
|
} else {
|
2015-03-07 16:19:40 +01:00
|
|
|
try {
|
2015-03-20 04:03:58 +01:00
|
|
|
// The stream by fs.openSync('\\\\.\\CON', 'r') can't switch to raw mode.
|
|
|
|
// 'CONIN$' might fail on XP, 2000, 7 (x86).
|
|
|
|
fdR = getFsB().open('CONIN$', constants.O_RDWR, parseInt('0666', 8));
|
|
|
|
ttyR = new TTY(fdR, true);
|
2015-03-07 16:19:40 +01:00
|
|
|
} catch (e) {}
|
2015-03-20 04:03:58 +01:00
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
fdW = process.stdout.fd;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fdW = fs.openSync('\\\\.\\CON', 'w');
|
|
|
|
} catch (e) {}
|
|
|
|
if (typeof fdW !== 'number') { // Retry
|
2015-03-07 16:19:40 +01:00
|
|
|
try {
|
2015-03-20 04:03:58 +01:00
|
|
|
fdW = getFsB().open('CONOUT$', constants.O_RDWR, parseInt('0666', 8));
|
2015-03-07 16:19:40 +01:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
2014-07-12 00:37:25 +02:00
|
|
|
|
2015-03-11 09:06:27 +01:00
|
|
|
} else {
|
2015-03-20 04:03:58 +01:00
|
|
|
if (process.stdin.isTTY) {
|
|
|
|
try {
|
|
|
|
fdR = fs.openSync('/dev/tty', 'r'); // device file, not process.stdin
|
|
|
|
ttyR = process.stdin._handle;
|
|
|
|
} catch (e) {}
|
|
|
|
} else {
|
|
|
|
// Node v0.12 read() fails.
|
|
|
|
try {
|
|
|
|
fdR = fs.openSync('/dev/tty', 'r');
|
|
|
|
ttyR = new TTY(fdR, false);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
fdW = process.stdout.fd;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fdW = fs.openSync('/dev/tty', 'w');
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
}
|
2015-03-20 04:03:58 +01:00
|
|
|
})();
|
|
|
|
|
2015-03-25 08:56:43 +01:00
|
|
|
// Call before tryExt()
|
2015-03-20 04:03:58 +01:00
|
|
|
if (options.display !== '' && typeof print === 'function')
|
|
|
|
{ print(options.display, encoding); }
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
(function() { // try read
|
2015-03-25 13:22:44 +01:00
|
|
|
var buffer, reqSize, readSize, chunk, isInputLine = false,
|
|
|
|
isEditable = !options.noEchoBack && !options.keyIn;
|
|
|
|
|
|
|
|
function writeTTY(text) {
|
|
|
|
fs.writeSync(fdW, text);
|
|
|
|
isInputLine = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node v0.10- returns an error if same mode is set.
|
|
|
|
function setRawMode(mode) {
|
|
|
|
if (mode === isRawMode) { return true; }
|
|
|
|
if (ttyR.setRawMode(mode) !== 0) { return false; }
|
|
|
|
isRawMode = mode;
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-03-25 08:56:43 +01:00
|
|
|
if (useExt || !ttyR ||
|
2015-03-20 04:03:58 +01:00
|
|
|
typeof fdW !== 'number' && (options.display !== '' || !isEditable)) {
|
2015-03-25 08:56:43 +01:00
|
|
|
input = tryExt();
|
2015-03-20 04:03:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.display !== '') {
|
2015-03-25 13:22:44 +01:00
|
|
|
writeTTY(options.display);
|
2015-03-20 04:03:58 +01:00
|
|
|
options.display = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setRawMode(!isEditable)) {
|
2015-03-25 08:56:43 +01:00
|
|
|
input = tryExt();
|
2015-03-20 04:03:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize));
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
readSize = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
readSize = fs.readSync(fdR, buffer, 0, reqSize);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'EOF') { break; }
|
|
|
|
setRawMode(false);
|
2015-03-25 08:56:43 +01:00
|
|
|
input += tryExt();
|
2015-03-20 04:03:58 +01:00
|
|
|
return;
|
2013-12-17 18:18:39 +01:00
|
|
|
}
|
2014-07-12 00:37:25 +02:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
if (readSize === 0) { break; }
|
|
|
|
chunk = buffer.toString(encoding, 0, readSize);
|
|
|
|
// other ctrl-chars
|
|
|
|
if ((chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '')) === '')
|
|
|
|
{ continue; }
|
|
|
|
|
2015-03-25 13:22:44 +01:00
|
|
|
if (!isEditable && (displayTmp = chunk.replace(/[\r\n]/g, '')) !== '') {
|
2015-03-22 06:11:55 +01:00
|
|
|
if (options.noEchoBack) {
|
2015-03-25 13:22:44 +01:00
|
|
|
displayTmp = options.mask === '' ? '' :
|
|
|
|
(new Array(displayTmp.length + 1)).join(options.mask);
|
2015-03-22 06:11:55 +01:00
|
|
|
}
|
2015-03-25 13:22:44 +01:00
|
|
|
if (displayTmp !== '') { writeTTY(displayTmp); }
|
2015-03-20 04:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
input += chunk;
|
|
|
|
if (/[\r\n]$/.test(input) ||
|
|
|
|
options.keyIn && input.length >= reqSize) { break; }
|
2013-12-17 18:18:39 +01:00
|
|
|
}
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2015-03-25 13:22:44 +01:00
|
|
|
if (!isEditable && !(options.keyIn && !isInputLine)) { writeTTY('\n'); }
|
2015-03-20 04:03:58 +01:00
|
|
|
setRawMode(false);
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (typeof print === 'function') {
|
2015-03-25 13:22:44 +01:00
|
|
|
displayTmp = input.replace(/[\r\n]/g, '');
|
2015-03-20 04:03:58 +01:00
|
|
|
print((options.noEchoBack ?
|
2015-03-25 13:22:44 +01:00
|
|
|
(new Array(displayTmp.length + 1)).join(options.mask) : displayTmp) +
|
2015-03-25 08:56:43 +01:00
|
|
|
'\n', encoding);
|
2013-08-29 12:55:23 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
return options.noTrim || options.keyIn ?
|
|
|
|
input.replace(/[\r\n]+$/, '') : input.trim();
|
2014-06-27 03:06:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-25 08:56:43 +01:00
|
|
|
function readlineExt(options) {
|
2015-03-27 12:25:59 +01:00
|
|
|
var hostArgs, res = {},
|
|
|
|
execOptions = {env: process.env, encoding: encoding};
|
2015-03-25 08:56:43 +01:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if (!extHostPath) {
|
|
|
|
if (IS_WIN) {
|
|
|
|
if (process.env.PSModulePath) { // Windows PowerShell
|
|
|
|
extHostPath = 'powershell.exe';
|
|
|
|
extHostArgs = ['-ExecutionPolicy', 'Bypass', '-File', __dirname + '\\read.ps1'];
|
|
|
|
} else { // Windows Script Host
|
|
|
|
extHostPath = 'cscript.exe';
|
|
|
|
extHostArgs = ['//nologo', __dirname + '\\read.cs.js'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
extHostPath = '/bin/sh';
|
|
|
|
extHostArgs = [__dirname + '/read.sh'];
|
|
|
|
}
|
2015-03-11 09:06:27 +01:00
|
|
|
}
|
2015-03-27 12:25:59 +01:00
|
|
|
if (IS_WIN && !process.env.PSModulePath) { // Windows Script Host
|
2015-03-27 13:12:00 +01:00
|
|
|
options.encoded = true; // Parsing args is DOS?
|
2015-03-27 12:25:59 +01:00
|
|
|
// ScriptPW (Win XP and Server2003) needs TTY stream as STDIN.
|
|
|
|
// In this case, If STDIN isn't TTY, an error is thrown.
|
|
|
|
execOptions.stdio = [process.stdin];
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (childProc.execFileSync) {
|
2015-03-27 12:25:59 +01:00
|
|
|
hostArgs = getHostArgs(options);
|
2015-03-03 18:30:41 +01:00
|
|
|
try {
|
2015-03-27 12:25:59 +01:00
|
|
|
res.input = childProc.execFileSync(extHostPath, hostArgs, execOptions);
|
2015-03-03 18:30:41 +01:00
|
|
|
} catch (e) { // non-zero exit code
|
2015-03-06 07:23:32 +01:00
|
|
|
res.error = new Error(DEFAULT_ERR_MSG);
|
|
|
|
res.error.method = 'execFileSync';
|
2015-03-27 12:25:59 +01:00
|
|
|
res.error.program = extHostPath;
|
|
|
|
res.error.args = hostArgs;
|
|
|
|
res.error.extMessage = e.stderr.trim();
|
|
|
|
res.error.exitCode = e.status;
|
2015-03-17 04:11:46 +01:00
|
|
|
res.error.code = e.code;
|
|
|
|
res.error.signal = e.signal;
|
2015-03-03 18:30:41 +01:00
|
|
|
}
|
2015-02-11 23:39:59 +01:00
|
|
|
} else {
|
2015-03-27 12:25:59 +01:00
|
|
|
res = _execFileSync(options, execOptions);
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
2015-03-13 10:34:41 +01:00
|
|
|
if (!res.error) {
|
|
|
|
res.input = res.input.replace(/^'|'$/g, '');
|
|
|
|
options.display = '';
|
|
|
|
}
|
2015-02-11 23:39:59 +01:00
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
return res;
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// piping via files (node v0.10-)
|
2015-03-27 12:25:59 +01:00
|
|
|
function _execFileSync(options, execOptions) {
|
2015-02-11 23:39:59 +01:00
|
|
|
|
|
|
|
function getTempfile(name) {
|
|
|
|
var path = require('path'), filepath, suffix = '', fd;
|
|
|
|
tempdir = tempdir || require('os').tmpdir();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
filepath = path.join(tempdir, name + suffix);
|
|
|
|
try {
|
|
|
|
fd = fs.openSync(filepath, 'wx');
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'EEXIST') {
|
|
|
|
suffix++;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.closeSync(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
var hostArgs, shellPath, shellArgs, res = {}, exitCode,
|
2015-03-13 10:34:41 +01:00
|
|
|
pathStdout = getTempfile('readline-sync.stdout'),
|
|
|
|
pathStderr = getTempfile('readline-sync.stderr'),
|
|
|
|
pathExit = getTempfile('readline-sync.exit'),
|
|
|
|
pathDone = getTempfile('readline-sync.done'),
|
2014-07-13 06:55:17 +02:00
|
|
|
crypto = require('crypto'), shasum, decipher, password;
|
|
|
|
|
|
|
|
shasum = crypto.createHash(ALGORITHM_HASH);
|
|
|
|
shasum.update('' + process.pid + (salt++) + Math.random());
|
|
|
|
password = shasum.digest('hex');
|
|
|
|
decipher = crypto.createDecipher(ALGORITHM_CIPHER, password);
|
2014-06-29 00:00:15 +02:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if (IS_WIN) { options.encoded = true; }
|
|
|
|
hostArgs = getHostArgs(options);
|
2015-03-02 14:44:36 +01:00
|
|
|
if (IS_WIN) {
|
2015-03-27 12:25:59 +01:00
|
|
|
shellPath = process.env.ComSpec || 'cmd.exe';
|
2015-03-07 16:19:40 +01:00
|
|
|
process.env.Q = '"'; // The quote (") that isn't escaped.
|
2015-03-03 18:30:41 +01:00
|
|
|
// `()` for ignore space by echo
|
2015-03-27 12:25:59 +01:00
|
|
|
shellArgs = ['/V:ON', '/S', '/C',
|
|
|
|
'(%Q%' + shellPath + '%Q% /V:ON /S /C %Q%' +
|
|
|
|
'%Q%' + extHostPath + '%Q%' +
|
|
|
|
hostArgs.map(function(arg) { return ' %Q%' + arg + '%Q%'; }).join('') +
|
2015-03-13 10:34:41 +01:00
|
|
|
' & (echo !ERRORLEVEL!)>%Q%' + pathExit + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' +
|
2015-03-02 14:44:36 +01:00
|
|
|
' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' +
|
|
|
|
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
2015-03-03 18:30:41 +01:00
|
|
|
' >%Q%' + pathStdout + '%Q%' +
|
|
|
|
' & (echo 1)>%Q%' + pathDone + '%Q%'];
|
2015-03-02 14:44:36 +01:00
|
|
|
} else {
|
2015-03-27 12:25:59 +01:00
|
|
|
shellPath = extHostPath;
|
|
|
|
shellArgs = ['-c',
|
2015-03-05 09:39:46 +01:00
|
|
|
// Use `()`, not `{}` for `-c` (text param)
|
2015-03-27 12:25:59 +01:00
|
|
|
'("' + extHostPath + '"' +
|
|
|
|
hostArgs.map(function(arg)
|
2015-03-13 10:34:41 +01:00
|
|
|
{ return " '" + arg.replace(/'/g, "'\\''") + "'"; }).join('') +
|
|
|
|
'; echo $?>"' + pathExit + '") 2>"' + pathStderr + '"' +
|
2015-03-02 14:44:36 +01:00
|
|
|
' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' +
|
|
|
|
' "' + ALGORITHM_CIPHER + '" "' + password + '"' +
|
2015-03-03 18:30:41 +01:00
|
|
|
' >"' + pathStdout + '"' +
|
|
|
|
'; echo 1 >"' + pathDone + '"'];
|
2015-03-02 14:44:36 +01:00
|
|
|
}
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-27 12:25:59 +01:00
|
|
|
childProc.spawn(shellPath, shellArgs, execOptions);
|
2015-03-13 10:34:41 +01:00
|
|
|
} catch (e) {
|
|
|
|
res.error = new Error(e.message);
|
2015-03-25 08:56:43 +01:00
|
|
|
res.error.method = '_execFileSync - spawn';
|
2015-03-27 12:25:59 +01:00
|
|
|
res.error.program = shellPath;
|
|
|
|
res.error.args = shellArgs;
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
2014-06-29 00:00:15 +02:00
|
|
|
|
2014-07-13 08:32:02 +02:00
|
|
|
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {}
|
2015-03-17 04:11:46 +01:00
|
|
|
if ((exitCode = fs.readFileSync(pathExit, {encoding: encoding}).trim()) === '0') {
|
2015-03-06 07:23:32 +01:00
|
|
|
res.input =
|
2014-07-13 06:55:17 +02:00
|
|
|
decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), 'hex', encoding) +
|
|
|
|
decipher.final(encoding);
|
2015-03-03 18:30:41 +01:00
|
|
|
} else {
|
2015-03-06 07:23:32 +01:00
|
|
|
res.error = new Error(DEFAULT_ERR_MSG);
|
2015-03-25 08:56:43 +01:00
|
|
|
res.error.method = '_execFileSync';
|
2015-03-27 12:25:59 +01:00
|
|
|
res.error.program = shellPath;
|
|
|
|
res.error.args = shellArgs;
|
|
|
|
res.error.extMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim();
|
2015-03-17 04:11:46 +01:00
|
|
|
res.error.exitCode = +exitCode;
|
2014-06-29 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.unlinkSync(pathStdout);
|
2015-03-03 18:30:41 +01:00
|
|
|
fs.unlinkSync(pathStderr);
|
2015-03-13 10:34:41 +01:00
|
|
|
fs.unlinkSync(pathExit);
|
2014-06-29 00:00:15 +02:00
|
|
|
fs.unlinkSync(pathDone);
|
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
return res;
|
2014-06-29 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
function getHostArgs(options) {
|
|
|
|
// To send any text to crazy Windows shell safely.
|
|
|
|
function encodeDOS(arg) {
|
|
|
|
return arg.replace(/[^\w\u0080-\uFFFF]/g, function(chr) {
|
|
|
|
return '#' + chr.charCodeAt(0) + ';';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return extHostArgs.concat((function(conf) {
|
|
|
|
var args = [], key;
|
|
|
|
for (key in conf) {
|
|
|
|
if (conf.hasOwnProperty(key)) {
|
|
|
|
if (conf[key] === 'boolean') {
|
|
|
|
if (options[key]) { args.push('--' + key); }
|
|
|
|
} else if (conf[key] === 'string') {
|
|
|
|
if (options[key] !== '') {
|
|
|
|
args.push('--' + key,
|
|
|
|
options.encoded ? encodeDOS(options[key]) : options[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
})({
|
|
|
|
display: 'string',
|
|
|
|
noEchoBack: 'boolean',
|
|
|
|
mask: 'string',
|
|
|
|
keyIn: 'boolean',
|
|
|
|
encoded: 'boolean'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2014-06-27 03:06:29 +02:00
|
|
|
// for dev
|
2015-03-25 08:56:43 +01:00
|
|
|
exports._useExtSet = function(use) { useExt = use; };
|
2013-08-29 19:26:22 +02:00
|
|
|
|
2014-07-11 23:25:59 +02:00
|
|
|
exports.setPrint = function(fnc) { print = fnc; };
|
|
|
|
|
2013-08-29 19:26:22 +02:00
|
|
|
exports.setPrompt = function(newPrompt) {
|
2014-07-12 01:16:11 +02:00
|
|
|
/* jshint eqnull:true */
|
2014-07-13 14:30:04 +02:00
|
|
|
if (newPrompt != null) {
|
2014-07-12 01:16:11 +02:00
|
|
|
/* jshint eqnull:false */
|
2014-07-13 14:30:04 +02:00
|
|
|
promptText = newPrompt;
|
|
|
|
}
|
2014-07-12 01:56:21 +02:00
|
|
|
return promptText;
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.setEncoding = function(newEncoding) {
|
|
|
|
if (typeof newEncoding === 'string') {
|
|
|
|
encoding = newEncoding;
|
|
|
|
}
|
2014-07-12 01:56:21 +02:00
|
|
|
return encoding;
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
2015-03-20 12:34:18 +01:00
|
|
|
exports.setMask = function(newMask) {
|
|
|
|
if (typeof newMask === 'string') {
|
|
|
|
mask = newMask;
|
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
};
|
|
|
|
|
2015-02-22 12:57:07 +01:00
|
|
|
exports.setBufferSize = function(newBufSize) {
|
2015-03-13 10:34:41 +01:00
|
|
|
newBufSize = parseInt(newBufSize, 10);
|
|
|
|
if (!isNaN(newBufSize) && typeof newBufSize === 'number') {
|
2015-02-22 12:57:07 +01:00
|
|
|
bufSize = newBufSize;
|
|
|
|
}
|
|
|
|
return bufSize;
|
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.prompt = function(options) {
|
2015-03-11 09:06:27 +01:00
|
|
|
var readOptions = {
|
|
|
|
display: promptText + '',
|
2015-03-12 16:59:30 +01:00
|
|
|
noEchoBack: !!(options && options.noEchoBack),
|
2015-03-25 08:56:43 +01:00
|
|
|
mask: mask,
|
2015-03-11 09:06:27 +01:00
|
|
|
keyIn: false,
|
2015-03-12 16:59:30 +01:00
|
|
|
noTrim: !!(options && options.noTrim)
|
2015-03-11 09:06:27 +01:00
|
|
|
};
|
2015-03-25 08:56:43 +01:00
|
|
|
return readlineSync(readOptions);
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.question = function(query, options) {
|
2015-03-11 09:06:27 +01:00
|
|
|
var readOptions = {
|
2014-07-12 01:16:11 +02:00
|
|
|
/* jshint eqnull:true */
|
2015-03-11 09:06:27 +01:00
|
|
|
display: query != null ? query + '' : '',
|
2014-07-12 01:16:11 +02:00
|
|
|
/* jshint eqnull:false */
|
2015-03-12 16:59:30 +01:00
|
|
|
noEchoBack: !!(options && options.noEchoBack),
|
2015-03-25 08:56:43 +01:00
|
|
|
mask: mask,
|
2015-03-11 09:06:27 +01:00
|
|
|
keyIn: false,
|
2015-03-12 16:59:30 +01:00
|
|
|
noTrim: !!(options && options.noTrim)
|
2015-03-11 09:06:27 +01:00
|
|
|
};
|
2015-03-25 08:56:43 +01:00
|
|
|
return readlineSync(readOptions);
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
2015-03-01 15:46:52 +01:00
|
|
|
|
2015-03-07 16:19:40 +01:00
|
|
|
exports.keyIn = function(query, options) {
|
2015-03-11 09:06:27 +01:00
|
|
|
var readOptions = {
|
2015-03-01 15:46:52 +01:00
|
|
|
/* jshint eqnull:true */
|
2015-03-11 09:06:27 +01:00
|
|
|
display: query != null ? query + '' : '',
|
2015-03-01 15:46:52 +01:00
|
|
|
/* jshint eqnull:false */
|
2015-03-13 10:34:41 +01:00
|
|
|
noEchoBack: !!(options && options.noEchoBack),
|
2015-03-25 08:56:43 +01:00
|
|
|
mask: mask,
|
2015-03-11 09:06:27 +01:00
|
|
|
keyIn: true,
|
|
|
|
noTrim: true
|
|
|
|
};
|
2015-03-25 08:56:43 +01:00
|
|
|
return readlineSync(readOptions);
|
2015-03-01 15:46:52 +01:00
|
|
|
};
|