2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-06-07 16:03:57 -07:00
|
|
|
// Inspiration for this code comes from Salvatore Sanfilippo's linenoise.
|
2011-11-01 20:52:35 +01:00
|
|
|
// https://github.com/antirez/linenoise
|
2010-05-31 11:50:35 -07:00
|
|
|
// Reference:
|
|
|
|
// * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
|
|
|
// * http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
|
|
|
ERR_INVALID_CURSOR_POS,
|
|
|
|
ERR_INVALID_OPT_VALUE
|
|
|
|
} = require('internal/errors').codes;
|
2018-12-11 23:24:22 +08:00
|
|
|
const { validateString } = require('internal/validators');
|
2019-03-20 14:58:33 +01:00
|
|
|
const { inspect } = require('internal/util/inspect');
|
2018-10-26 17:53:04 -07:00
|
|
|
const { emitExperimentalWarning } = require('internal/util');
|
2017-04-29 13:46:26 -07:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const {
|
2017-04-29 17:05:04 -07:00
|
|
|
CSI,
|
2017-04-29 13:46:26 -07:00
|
|
|
emitKeys,
|
|
|
|
getStringWidth,
|
|
|
|
isFullWidthCodePoint,
|
|
|
|
stripVTControlCharacters
|
|
|
|
} = require('internal/readline');
|
|
|
|
|
2017-04-29 17:05:04 -07:00
|
|
|
const {
|
|
|
|
kEscape,
|
|
|
|
kClearToBeginning,
|
|
|
|
kClearToEnd,
|
|
|
|
kClearLine,
|
|
|
|
kClearScreenDown
|
|
|
|
} = CSI;
|
|
|
|
|
2018-05-06 23:08:00 +02:00
|
|
|
// Lazy load StringDecoder for startup performance.
|
|
|
|
let StringDecoder;
|
|
|
|
|
2018-10-26 17:53:04 -07:00
|
|
|
// Lazy load Readable for startup performance.
|
|
|
|
let Readable;
|
|
|
|
|
2015-01-21 11:36:59 -05:00
|
|
|
const kHistorySize = 30;
|
2016-08-15 17:37:01 +05:30
|
|
|
const kMincrlfDelay = 100;
|
2017-04-29 13:46:26 -07:00
|
|
|
// \r\n, \n, or \r followed by something other than \n
|
|
|
|
const lineEnding = /\r?\n|\r(?!\n)/;
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2018-10-26 17:53:04 -07:00
|
|
|
const kLineObjectStream = Symbol('line object stream');
|
|
|
|
|
2017-04-29 13:46:26 -07:00
|
|
|
const KEYPRESS_DECODER = Symbol('keypress-decoder');
|
|
|
|
const ESCAPE_DECODER = Symbol('escape-decoder');
|
|
|
|
|
|
|
|
// GNU readline library - keyseq-timeout is 500ms (default)
|
|
|
|
const ESCAPE_CODE_TIMEOUT = 500;
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2017-04-29 13:33:35 -07:00
|
|
|
function createInterface(input, output, completer, terminal) {
|
2016-01-18 17:51:15 +08:00
|
|
|
return new Interface(input, output, completer, terminal);
|
2017-04-29 13:46:26 -07:00
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2010-11-11 22:36:39 -08:00
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
function Interface(input, output, completer, terminal) {
|
2011-01-19 11:46:14 -08:00
|
|
|
if (!(this instanceof Interface)) {
|
2016-01-18 17:51:15 +08:00
|
|
|
return new Interface(input, output, completer, terminal);
|
2011-01-19 11:46:14 -08:00
|
|
|
}
|
2012-03-26 15:21:25 -07:00
|
|
|
|
2018-05-06 23:08:00 +02:00
|
|
|
if (StringDecoder === undefined)
|
|
|
|
StringDecoder = require('string_decoder').StringDecoder;
|
|
|
|
|
2016-08-15 17:37:01 +05:30
|
|
|
this._sawReturnAt = 0;
|
2016-05-13 10:54:31 +03:00
|
|
|
this.isCompletionEnabled = true;
|
2016-08-24 00:05:59 +05:30
|
|
|
this._sawKeyPress = false;
|
2016-07-15 23:33:16 +02:00
|
|
|
this._previousKey = null;
|
2018-04-04 00:00:35 +04:30
|
|
|
this.escapeCodeTimeout = ESCAPE_CODE_TIMEOUT;
|
2013-01-29 17:50:44 -08:00
|
|
|
|
2012-06-12 21:53:08 +02:00
|
|
|
EventEmitter.call(this);
|
2015-04-23 00:35:53 -07:00
|
|
|
var historySize;
|
2017-03-20 18:30:41 -04:00
|
|
|
var removeHistoryDuplicates = false;
|
2016-08-15 17:37:01 +05:30
|
|
|
let crlfDelay;
|
2016-06-02 20:31:23 -05:00
|
|
|
let prompt = '> ';
|
2012-06-12 21:53:08 +02:00
|
|
|
|
2016-01-18 17:51:15 +08:00
|
|
|
if (input && input.input) {
|
2019-03-22 03:44:26 +01:00
|
|
|
// An options object was given
|
2012-03-26 15:21:25 -07:00
|
|
|
output = input.output;
|
|
|
|
completer = input.completer;
|
|
|
|
terminal = input.terminal;
|
2015-04-23 00:35:53 -07:00
|
|
|
historySize = input.historySize;
|
2017-03-20 18:30:41 -04:00
|
|
|
removeHistoryDuplicates = input.removeHistoryDuplicates;
|
2016-06-02 20:31:23 -05:00
|
|
|
if (input.prompt !== undefined) {
|
|
|
|
prompt = input.prompt;
|
|
|
|
}
|
2018-04-04 00:00:35 +04:30
|
|
|
if (input.escapeCodeTimeout !== undefined) {
|
|
|
|
if (Number.isFinite(input.escapeCodeTimeout)) {
|
|
|
|
this.escapeCodeTimeout = input.escapeCodeTimeout;
|
|
|
|
} else {
|
|
|
|
throw new ERR_INVALID_OPT_VALUE(
|
|
|
|
'escapeCodeTimeout',
|
|
|
|
this.escapeCodeTimeout
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 17:37:01 +05:30
|
|
|
crlfDelay = input.crlfDelay;
|
2012-03-26 15:21:25 -07:00
|
|
|
input = input.input;
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:17:10 -07:00
|
|
|
if (completer && typeof completer !== 'function') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('completer', completer);
|
2011-09-14 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 18:58:40 -04:00
|
|
|
if (historySize === undefined) {
|
|
|
|
historySize = kHistorySize;
|
|
|
|
}
|
|
|
|
|
2015-04-23 00:35:53 -07:00
|
|
|
if (typeof historySize !== 'number' ||
|
2018-02-13 00:56:35 +01:00
|
|
|
Number.isNaN(historySize) ||
|
2015-04-23 00:35:53 -07:00
|
|
|
historySize < 0) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE.RangeError('historySize', historySize);
|
2015-04-23 00:35:53 -07:00
|
|
|
}
|
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Backwards compat; check the isTTY prop of the output stream
|
2012-03-26 15:21:25 -07:00
|
|
|
// when `terminal` was not specified
|
2015-01-28 20:05:53 -05:00
|
|
|
if (terminal === undefined && !(output === null || output === undefined)) {
|
2012-03-26 15:21:25 -07:00
|
|
|
terminal = !!output.isTTY;
|
|
|
|
}
|
|
|
|
|
2011-01-19 11:46:14 -08:00
|
|
|
var self = this;
|
|
|
|
|
2010-05-31 11:50:35 -07:00
|
|
|
this.output = output;
|
2011-01-19 11:46:14 -08:00
|
|
|
this.input = input;
|
2015-04-23 00:35:53 -07:00
|
|
|
this.historySize = historySize;
|
2017-03-20 18:30:41 -04:00
|
|
|
this.removeHistoryDuplicates = !!removeHistoryDuplicates;
|
2017-06-06 20:22:22 +08:00
|
|
|
this.crlfDelay = crlfDelay ?
|
|
|
|
Math.max(kMincrlfDelay, crlfDelay) : kMincrlfDelay;
|
2011-09-07 14:39:49 +07:00
|
|
|
// Check arity, 2 - for async, 1 for sync
|
2015-05-20 21:17:10 -07:00
|
|
|
if (typeof completer === 'function') {
|
2017-07-20 16:05:39 +08:00
|
|
|
this.completer = completer.length === 2 ?
|
|
|
|
completer :
|
|
|
|
function completerWrapper(v, cb) {
|
|
|
|
cb(null, completer(v));
|
|
|
|
};
|
2015-05-20 21:17:10 -07:00
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2016-06-02 20:31:23 -05:00
|
|
|
this.setPrompt(prompt);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
this.terminal = !!terminal;
|
2010-06-08 13:05:21 -07:00
|
|
|
|
2019-02-22 16:07:43 +04:00
|
|
|
if (process.env.TERM === 'dumb') {
|
|
|
|
this._ttyWrite = _ttyWriteDumb.bind(this);
|
|
|
|
}
|
|
|
|
|
2012-07-23 20:44:12 -07:00
|
|
|
function ondata(data) {
|
|
|
|
self._normalWrite(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onend() {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof self._line_buffer === 'string' &&
|
|
|
|
self._line_buffer.length > 0) {
|
2014-01-22 09:53:23 +08:00
|
|
|
self.emit('line', self._line_buffer);
|
|
|
|
}
|
2012-07-23 20:44:12 -07:00
|
|
|
self.close();
|
|
|
|
}
|
|
|
|
|
2014-05-08 00:05:39 +08:00
|
|
|
function ontermend() {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof self.line === 'string' && self.line.length > 0) {
|
2014-05-08 00:05:39 +08:00
|
|
|
self.emit('line', self.line);
|
|
|
|
}
|
|
|
|
self.close();
|
|
|
|
}
|
|
|
|
|
2012-07-23 20:44:12 -07:00
|
|
|
function onkeypress(s, key) {
|
|
|
|
self._ttyWrite(s, key);
|
2016-10-11 15:20:03 -07:00
|
|
|
if (key && key.sequence) {
|
2019-01-21 01:22:27 +01:00
|
|
|
// If the key.sequence is half of a surrogate pair
|
2016-10-11 15:20:03 -07:00
|
|
|
// (>= 0xd800 and <= 0xdfff), refresh the line so
|
|
|
|
// the character is displayed appropriately.
|
|
|
|
const ch = key.sequence.codePointAt(0);
|
|
|
|
if (ch >= 0xd800 && ch <= 0xdfff)
|
|
|
|
self._refreshLine();
|
|
|
|
}
|
2012-07-23 20:44:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function onresize() {
|
|
|
|
self._refreshLine();
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:53:04 -07:00
|
|
|
this[kLineObjectStream] = undefined;
|
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
if (!this.terminal) {
|
2017-07-20 16:05:39 +08:00
|
|
|
function onSelfCloseWithoutTerminal() {
|
2012-07-23 20:44:12 -07:00
|
|
|
input.removeListener('data', ondata);
|
|
|
|
input.removeListener('end', onend);
|
2017-07-20 16:05:39 +08:00
|
|
|
}
|
2011-01-19 11:46:14 -08:00
|
|
|
|
2017-07-20 16:05:39 +08:00
|
|
|
input.on('data', ondata);
|
|
|
|
input.on('end', onend);
|
|
|
|
self.once('close', onSelfCloseWithoutTerminal);
|
|
|
|
this._decoder = new StringDecoder('utf8');
|
2011-01-19 11:46:14 -08:00
|
|
|
} else {
|
2017-07-20 16:05:39 +08:00
|
|
|
function onSelfCloseWithTerminal() {
|
|
|
|
input.removeListener('keypress', onkeypress);
|
|
|
|
input.removeListener('end', ontermend);
|
|
|
|
if (output !== null && output !== undefined) {
|
|
|
|
output.removeListener('resize', onresize);
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 11:46:14 -08:00
|
|
|
|
2016-05-13 10:54:31 +03:00
|
|
|
emitKeypressEvents(input, this);
|
2012-03-26 15:21:25 -07:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// `input` usually refers to stdin
|
2012-07-23 20:44:12 -07:00
|
|
|
input.on('keypress', onkeypress);
|
2014-05-08 00:05:39 +08:00
|
|
|
input.on('end', ontermend);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2010-06-08 13:05:21 -07:00
|
|
|
// Current line
|
2010-12-01 18:07:20 -08:00
|
|
|
this.line = '';
|
2010-06-08 13:05:21 -07:00
|
|
|
|
2012-05-21 19:41:56 -03:00
|
|
|
this._setRawMode(true);
|
2012-03-26 15:21:25 -07:00
|
|
|
this.terminal = true;
|
2010-05-31 11:50:35 -07:00
|
|
|
|
|
|
|
// Cursor position on the line.
|
|
|
|
this.cursor = 0;
|
|
|
|
|
|
|
|
this.history = [];
|
|
|
|
this.historyIndex = -1;
|
2010-09-12 21:23:53 -07:00
|
|
|
|
2015-01-28 20:05:53 -05:00
|
|
|
if (output !== null && output !== undefined)
|
2014-09-22 13:21:11 -07:00
|
|
|
output.on('resize', onresize);
|
|
|
|
|
2017-07-20 16:05:39 +08:00
|
|
|
self.once('close', onSelfCloseWithTerminal);
|
2010-05-31 11:50:35 -07:00
|
|
|
}
|
2012-10-24 02:42:57 +02:00
|
|
|
|
|
|
|
input.resume();
|
2010-05-31 11:50:35 -07:00
|
|
|
}
|
|
|
|
|
2018-11-30 17:55:48 +01:00
|
|
|
Object.setPrototypeOf(Interface.prototype, EventEmitter.prototype);
|
2018-12-02 15:03:01 +01:00
|
|
|
Object.setPrototypeOf(Interface, EventEmitter);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2016-05-14 22:29:19 -07:00
|
|
|
Object.defineProperty(Interface.prototype, 'columns', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
var columns = Infinity;
|
|
|
|
if (this.output && this.output.columns)
|
|
|
|
columns = this.output.columns;
|
|
|
|
return columns;
|
|
|
|
}
|
2010-08-11 23:14:12 -07:00
|
|
|
});
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2013-03-15 16:18:30 -10:00
|
|
|
Interface.prototype.setPrompt = function(prompt) {
|
2010-05-31 11:50:35 -07:00
|
|
|
this._prompt = prompt;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-21 19:41:56 -03:00
|
|
|
Interface.prototype._setRawMode = function(mode) {
|
2016-05-08 03:29:43 +02:00
|
|
|
const wasInRawMode = this.input.isRaw;
|
|
|
|
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof this.input.setRawMode === 'function') {
|
2016-05-08 03:29:43 +02:00
|
|
|
this.input.setRawMode(mode);
|
2012-05-21 19:41:56 -03:00
|
|
|
}
|
2016-05-08 03:29:43 +02:00
|
|
|
|
|
|
|
return wasInRawMode;
|
2012-06-11 07:48:02 -07:00
|
|
|
};
|
2012-05-21 19:41:56 -03:00
|
|
|
|
|
|
|
|
2011-12-09 15:24:15 +06:00
|
|
|
Interface.prototype.prompt = function(preserveCursor) {
|
2012-02-15 09:08:26 -05:00
|
|
|
if (this.paused) this.resume();
|
2019-02-22 16:07:43 +04:00
|
|
|
if (this.terminal && process.env.TERM !== 'dumb') {
|
2011-12-09 15:24:15 +06:00
|
|
|
if (!preserveCursor) this.cursor = 0;
|
2010-05-31 11:50:35 -07:00
|
|
|
this._refreshLine();
|
|
|
|
} else {
|
2014-09-22 13:21:11 -07:00
|
|
|
this._writeToOutput(this._prompt);
|
2010-05-31 11:50:35 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-30 01:34:31 -08:00
|
|
|
Interface.prototype.question = function(query, cb) {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof cb === 'function') {
|
2010-12-30 18:28:30 -08:00
|
|
|
if (this._questionCallback) {
|
|
|
|
this.prompt();
|
|
|
|
} else {
|
|
|
|
this._oldPrompt = this._prompt;
|
|
|
|
this.setPrompt(query);
|
|
|
|
this._questionCallback = cb;
|
|
|
|
this.prompt();
|
|
|
|
}
|
2010-12-30 01:34:31 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype._onLine = function(line) {
|
|
|
|
if (this._questionCallback) {
|
|
|
|
var cb = this._questionCallback;
|
|
|
|
this._questionCallback = null;
|
|
|
|
this.setPrompt(this._oldPrompt);
|
2011-01-06 16:06:27 -08:00
|
|
|
cb(line);
|
2010-12-30 01:34:31 -08:00
|
|
|
} else {
|
|
|
|
this.emit('line', line);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-22 13:21:11 -07:00
|
|
|
Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
|
2018-12-11 23:24:22 +08:00
|
|
|
validateString(stringToWrite, 'stringToWrite');
|
2014-09-22 13:21:11 -07:00
|
|
|
|
2017-06-20 14:37:00 -07:00
|
|
|
if (this.output !== null && this.output !== undefined) {
|
2014-09-22 13:21:11 -07:00
|
|
|
this.output.write(stringToWrite);
|
2017-06-20 14:37:00 -07:00
|
|
|
}
|
2014-09-22 13:21:11 -07:00
|
|
|
};
|
2010-12-30 01:34:31 -08:00
|
|
|
|
2010-12-01 18:07:20 -08:00
|
|
|
Interface.prototype._addHistory = function() {
|
|
|
|
if (this.line.length === 0) return '';
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// If the history is disabled then return the line
|
2016-04-22 18:58:40 -04:00
|
|
|
if (this.historySize === 0) return this.line;
|
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// If the trimmed line is empty then return the line
|
2016-08-24 00:05:59 +05:30
|
|
|
if (this.line.trim().length === 0) return this.line;
|
|
|
|
|
2012-06-13 21:37:31 +04:00
|
|
|
if (this.history.length === 0 || this.history[0] !== this.line) {
|
2017-03-20 18:30:41 -04:00
|
|
|
if (this.removeHistoryDuplicates) {
|
2015-09-21 10:08:36 -04:00
|
|
|
// Remove older history line if identical to new one
|
|
|
|
const dupIndex = this.history.indexOf(this.line);
|
|
|
|
if (dupIndex !== -1) this.history.splice(dupIndex, 1);
|
|
|
|
}
|
|
|
|
|
2012-06-13 21:37:31 +04:00
|
|
|
this.history.unshift(this.line);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2012-06-13 21:37:31 +04:00
|
|
|
// Only store so many
|
2015-04-23 00:35:53 -07:00
|
|
|
if (this.history.length > this.historySize) this.history.pop();
|
2012-06-13 21:37:31 +04:00
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2012-06-13 21:37:31 +04:00
|
|
|
this.historyIndex = -1;
|
2010-06-07 21:19:25 -07:00
|
|
|
return this.history[0];
|
2010-05-31 11:50:35 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-01 18:07:20 -08:00
|
|
|
Interface.prototype._refreshLine = function() {
|
2012-03-20 15:14:40 -07:00
|
|
|
// line length
|
|
|
|
var line = this._prompt + this.line;
|
2013-03-15 16:18:30 -10:00
|
|
|
var dispPos = this._getDisplayPos(line);
|
|
|
|
var lineCols = dispPos.cols;
|
|
|
|
var lineRows = dispPos.rows;
|
2012-03-20 15:14:40 -07:00
|
|
|
|
|
|
|
// cursor position
|
|
|
|
var cursorPos = this._getCursorPos();
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// First move to the bottom of the current line, based on cursor pos
|
2012-03-20 15:14:40 -07:00
|
|
|
var prevRows = this.prevRows || 0;
|
|
|
|
if (prevRows > 0) {
|
2017-04-29 13:33:35 -07:00
|
|
|
moveCursor(this.output, 0, -prevRows);
|
2012-03-20 15:14:40 -07:00
|
|
|
}
|
|
|
|
|
2010-05-31 11:50:35 -07:00
|
|
|
// Cursor to left edge.
|
2017-04-29 13:33:35 -07:00
|
|
|
cursorTo(this.output, 0);
|
2012-03-20 15:14:40 -07:00
|
|
|
// erase data
|
2017-04-29 13:33:35 -07:00
|
|
|
clearScreenDown(this.output);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
|
|
|
// Write the prompt and the current buffer content.
|
2014-09-22 13:21:11 -07:00
|
|
|
this._writeToOutput(line);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2012-03-20 15:14:40 -07:00
|
|
|
// Force terminal to allocate a new line
|
|
|
|
if (lineCols === 0) {
|
2014-09-22 13:21:11 -07:00
|
|
|
this._writeToOutput(' ');
|
2012-03-20 15:14:40 -07:00
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
|
|
|
|
// Move cursor to original position.
|
2017-04-29 13:33:35 -07:00
|
|
|
cursorTo(this.output, cursorPos.cols);
|
2012-03-20 15:14:40 -07:00
|
|
|
|
|
|
|
var diff = lineRows - cursorPos.rows;
|
|
|
|
if (diff > 0) {
|
2017-04-29 13:33:35 -07:00
|
|
|
moveCursor(this.output, 0, -diff);
|
2012-03-20 15:14:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.prevRows = cursorPos.rows;
|
2010-05-31 11:50:35 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-17 11:31:07 -07:00
|
|
|
Interface.prototype.close = function() {
|
|
|
|
if (this.closed) return;
|
2013-07-30 14:43:31 +01:00
|
|
|
this.pause();
|
2012-03-26 15:21:25 -07:00
|
|
|
if (this.terminal) {
|
2012-05-21 19:41:56 -03:00
|
|
|
this._setRawMode(false);
|
2010-12-30 15:46:47 -08:00
|
|
|
}
|
2012-04-17 11:31:07 -07:00
|
|
|
this.closed = true;
|
|
|
|
this.emit('close');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype.pause = function() {
|
|
|
|
if (this.paused) return;
|
2012-02-15 09:08:26 -05:00
|
|
|
this.input.pause();
|
|
|
|
this.paused = true;
|
|
|
|
this.emit('pause');
|
2013-08-27 18:59:58 -07:00
|
|
|
return this;
|
2010-12-30 15:46:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype.resume = function() {
|
2012-04-17 11:31:07 -07:00
|
|
|
if (!this.paused) return;
|
2012-02-15 09:08:26 -05:00
|
|
|
this.input.resume();
|
|
|
|
this.paused = false;
|
|
|
|
this.emit('resume');
|
2013-08-27 18:59:58 -07:00
|
|
|
return this;
|
2010-12-30 15:46:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
Interface.prototype.write = function(d, key) {
|
2012-02-15 09:08:26 -05:00
|
|
|
if (this.paused) this.resume();
|
2019-02-22 16:07:43 +04:00
|
|
|
if (this.terminal) {
|
|
|
|
this._ttyWrite(d, key);
|
|
|
|
} else {
|
|
|
|
this._normalWrite(d);
|
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
};
|
|
|
|
|
2010-12-01 18:07:20 -08:00
|
|
|
Interface.prototype._normalWrite = function(b) {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (b === undefined) {
|
2012-04-06 11:41:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-04-06 14:33:58 -07:00
|
|
|
var string = this._decoder.write(b);
|
2016-08-15 17:37:01 +05:30
|
|
|
if (this._sawReturnAt &&
|
2018-02-04 13:43:21 -05:00
|
|
|
Date.now() - this._sawReturnAt <= this.crlfDelay) {
|
2013-01-29 17:50:44 -08:00
|
|
|
string = string.replace(/^\n/, '');
|
2016-08-15 17:37:01 +05:30
|
|
|
this._sawReturnAt = 0;
|
2013-01-29 17:50:44 -08:00
|
|
|
}
|
|
|
|
|
2014-09-14 23:40:49 +08:00
|
|
|
// Run test() on the new string chunk, not on the entire line buffer.
|
|
|
|
var newPartContainsEnding = lineEnding.test(string);
|
|
|
|
|
2012-04-06 14:33:58 -07:00
|
|
|
if (this._line_buffer) {
|
|
|
|
string = this._line_buffer + string;
|
|
|
|
this._line_buffer = null;
|
|
|
|
}
|
2014-09-14 23:40:49 +08:00
|
|
|
if (newPartContainsEnding) {
|
2018-02-04 13:43:21 -05:00
|
|
|
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
|
2013-01-29 17:50:44 -08:00
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Got one or more newlines; process into "line" events
|
2013-01-29 17:50:44 -08:00
|
|
|
var lines = string.split(lineEnding);
|
2018-12-03 17:15:45 +01:00
|
|
|
// Either '' or (conceivably) the unfinished portion of the next line
|
2012-04-06 14:33:58 -07:00
|
|
|
string = lines.pop();
|
|
|
|
this._line_buffer = string;
|
2017-02-26 18:15:36 -08:00
|
|
|
for (var n = 0; n < lines.length; n++)
|
|
|
|
this._onLine(lines[n]);
|
2012-04-06 14:33:58 -07:00
|
|
|
} else if (string) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// No newlines this time, save what we have for next time
|
2012-04-06 14:33:58 -07:00
|
|
|
this._line_buffer = string;
|
2012-04-06 11:41:59 -07:00
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
};
|
|
|
|
|
2010-12-01 18:07:20 -08:00
|
|
|
Interface.prototype._insertString = function(c) {
|
2010-08-09 02:18:32 -07:00
|
|
|
if (this.cursor < this.line.length) {
|
|
|
|
var beg = this.line.slice(0, this.cursor);
|
|
|
|
var end = this.line.slice(this.cursor, this.line.length);
|
|
|
|
this.line = beg + c + end;
|
|
|
|
this.cursor += c.length;
|
|
|
|
this._refreshLine();
|
|
|
|
} else {
|
|
|
|
this.line += c;
|
|
|
|
this.cursor += c.length;
|
2012-03-23 11:24:06 -07:00
|
|
|
|
|
|
|
if (this._getCursorPos().cols === 0) {
|
2012-03-29 01:27:57 +02:00
|
|
|
this._refreshLine();
|
2012-03-23 11:24:06 -07:00
|
|
|
} else {
|
2014-09-22 13:21:11 -07:00
|
|
|
this._writeToOutput(c);
|
2012-03-23 11:24:06 -07:00
|
|
|
}
|
2012-03-20 15:14:40 -07:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// A hack to get the line refreshed if it's needed
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(0);
|
2010-08-09 02:18:32 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-15 23:33:16 +02:00
|
|
|
Interface.prototype._tabComplete = function(lastKeypressWasTab) {
|
2010-08-09 02:18:32 -07:00
|
|
|
var self = this;
|
|
|
|
|
2011-09-08 16:03:27 +07:00
|
|
|
self.pause();
|
2017-07-20 16:05:39 +08:00
|
|
|
self.completer(self.line.slice(0, self.cursor), function onComplete(err, rv) {
|
2011-09-07 14:39:49 +07:00
|
|
|
self.resume();
|
|
|
|
|
|
|
|
if (err) {
|
2019-03-07 22:09:01 +08:00
|
|
|
self._writeToOutput(`tab completion error ${inspect(err)}`);
|
2011-09-07 14:39:49 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-12 22:04:50 +01:00
|
|
|
const completions = rv[0];
|
2019-03-22 03:44:26 +01:00
|
|
|
const completeOn = rv[1]; // The text that was completed
|
2011-09-07 14:39:49 +07:00
|
|
|
if (completions && completions.length) {
|
|
|
|
// Apply/show completions.
|
2016-07-15 23:33:16 +02:00
|
|
|
if (lastKeypressWasTab) {
|
2014-09-22 13:21:11 -07:00
|
|
|
self._writeToOutput('\r\n');
|
2017-07-20 16:05:39 +08:00
|
|
|
var width = completions.reduce(function completionReducer(a, b) {
|
2011-09-07 14:39:49 +07:00
|
|
|
return a.length > b.length ? a : b;
|
|
|
|
}).length + 2; // 2 space padding
|
2015-09-11 15:47:41 +01:00
|
|
|
var maxColumns = Math.floor(self.columns / width);
|
|
|
|
if (!maxColumns || maxColumns === Infinity) {
|
|
|
|
maxColumns = 1;
|
|
|
|
}
|
2017-02-19 13:45:30 +01:00
|
|
|
var group = [];
|
2017-07-16 15:24:19 +08:00
|
|
|
for (var i = 0; i < completions.length; i++) {
|
2017-02-19 13:45:30 +01:00
|
|
|
var c = completions[i];
|
2011-09-07 14:39:49 +07:00
|
|
|
if (c === '') {
|
2012-07-06 19:41:01 -07:00
|
|
|
handleGroup(self, group, width, maxColumns);
|
2011-09-07 14:39:49 +07:00
|
|
|
group = [];
|
|
|
|
} else {
|
|
|
|
group.push(c);
|
|
|
|
}
|
|
|
|
}
|
2012-07-06 19:41:01 -07:00
|
|
|
handleGroup(self, group, width, maxColumns);
|
2016-07-15 23:33:16 +02:00
|
|
|
}
|
2011-09-07 14:39:49 +07:00
|
|
|
|
2016-07-15 23:33:16 +02:00
|
|
|
// If there is a common prefix to all matches, then apply that portion.
|
2017-12-24 23:08:07 -08:00
|
|
|
var f = completions.filter((e) => e);
|
2017-04-24 02:21:30 -04:00
|
|
|
var prefix = commonPrefix(f);
|
2016-07-15 23:33:16 +02:00
|
|
|
if (prefix.length > completeOn.length) {
|
|
|
|
self._insertString(prefix.slice(completeOn.length));
|
2011-09-07 14:39:49 +07:00
|
|
|
}
|
2016-07-15 23:33:16 +02:00
|
|
|
|
2011-09-09 03:05:21 +07:00
|
|
|
self._refreshLine();
|
2010-08-09 02:18:32 -07:00
|
|
|
}
|
2011-09-07 14:39:49 +07:00
|
|
|
});
|
2010-08-09 02:18:32 -07:00
|
|
|
};
|
|
|
|
|
2012-07-04 23:01:03 +02:00
|
|
|
// this = Interface instance
|
2012-07-06 19:41:01 -07:00
|
|
|
function handleGroup(self, group, width, maxColumns) {
|
2017-01-31 00:40:19 -05:00
|
|
|
if (group.length === 0) {
|
2012-07-04 23:01:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var minRows = Math.ceil(group.length / maxColumns);
|
|
|
|
for (var row = 0; row < minRows; row++) {
|
|
|
|
for (var col = 0; col < maxColumns; col++) {
|
|
|
|
var idx = row * maxColumns + col;
|
|
|
|
if (idx >= group.length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var item = group[idx];
|
2014-09-22 13:21:11 -07:00
|
|
|
self._writeToOutput(item);
|
2012-07-04 23:01:03 +02:00
|
|
|
if (col < maxColumns - 1) {
|
2017-07-16 15:24:19 +08:00
|
|
|
for (var s = 0; s < width - item.length; s++) {
|
2014-09-22 13:21:11 -07:00
|
|
|
self._writeToOutput(' ');
|
2012-07-04 23:01:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 13:21:11 -07:00
|
|
|
self._writeToOutput('\r\n');
|
2012-07-04 23:01:03 +02:00
|
|
|
}
|
2014-09-22 13:21:11 -07:00
|
|
|
self._writeToOutput('\r\n');
|
2012-07-04 23:01:03 +02:00
|
|
|
}
|
2011-01-24 10:55:30 -08:00
|
|
|
|
2010-08-17 16:53:27 -07:00
|
|
|
function commonPrefix(strings) {
|
2017-01-31 00:40:19 -05:00
|
|
|
if (!strings || strings.length === 0) {
|
2010-12-01 18:07:20 -08:00
|
|
|
return '';
|
2010-08-17 16:53:27 -07:00
|
|
|
}
|
2016-07-15 23:33:16 +02:00
|
|
|
if (strings.length === 1) return strings[0];
|
2010-08-17 16:53:27 -07:00
|
|
|
var sorted = strings.slice().sort();
|
|
|
|
var min = sorted[0];
|
|
|
|
var max = sorted[sorted.length - 1];
|
2010-09-23 08:14:16 -04:00
|
|
|
for (var i = 0, len = min.length; i < len; i++) {
|
2017-01-31 00:40:19 -05:00
|
|
|
if (min[i] !== max[i]) {
|
2010-08-17 16:53:27 -07:00
|
|
|
return min.slice(0, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
2011-01-24 10:55:30 -08:00
|
|
|
|
2011-01-25 03:54:26 +01:00
|
|
|
Interface.prototype._wordLeft = function() {
|
|
|
|
if (this.cursor > 0) {
|
|
|
|
var leading = this.line.slice(0, this.cursor);
|
2017-06-16 20:14:58 +03:00
|
|
|
var match = leading.match(/(?:[^\w\s]+|\w+|)\s*$/);
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(-match[0].length);
|
2011-01-25 03:54:26 +01:00
|
|
|
}
|
2011-07-29 10:03:05 -07:00
|
|
|
};
|
2011-01-25 03:54:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype._wordRight = function() {
|
|
|
|
if (this.cursor < this.line.length) {
|
|
|
|
var trailing = this.line.slice(this.cursor);
|
2019-01-25 21:15:06 -05:00
|
|
|
var match = trailing.match(/^(?:\s+|[^\w\s]+|\w+)\s*/);
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(match[0].length);
|
2011-01-25 03:54:26 +01:00
|
|
|
}
|
2011-07-29 10:03:05 -07:00
|
|
|
};
|
2011-01-25 03:54:26 +01:00
|
|
|
|
2019-01-25 21:15:06 -05:00
|
|
|
function charLengthLeft(str, i) {
|
|
|
|
if (i <= 0)
|
|
|
|
return 0;
|
|
|
|
if (i > 1 && str.codePointAt(i - 2) >= 2 ** 16 ||
|
|
|
|
str.codePointAt(i - 1) >= 2 ** 16) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function charLengthAt(str, i) {
|
|
|
|
if (str.length <= i)
|
|
|
|
return 0;
|
|
|
|
return str.codePointAt(i) >= 2 ** 16 ? 2 : 1;
|
|
|
|
}
|
2011-01-25 03:54:26 +01:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
Interface.prototype._deleteLeft = function() {
|
|
|
|
if (this.cursor > 0 && this.line.length > 0) {
|
2019-01-25 21:15:06 -05:00
|
|
|
// The number of UTF-16 units comprising the character to the left
|
|
|
|
const charSize = charLengthLeft(this.line, this.cursor);
|
|
|
|
this.line = this.line.slice(0, this.cursor - charSize) +
|
2011-01-14 03:44:05 +01:00
|
|
|
this.line.slice(this.cursor, this.line.length);
|
|
|
|
|
2019-01-25 21:15:06 -05:00
|
|
|
this.cursor -= charSize;
|
2011-01-14 03:44:05 +01:00
|
|
|
this._refreshLine();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-24 10:55:30 -08:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
Interface.prototype._deleteRight = function() {
|
2019-01-25 21:15:06 -05:00
|
|
|
if (this.cursor < this.line.length) {
|
|
|
|
// The number of UTF-16 units comprising the character to the left
|
|
|
|
const charSize = charLengthAt(this.line, this.cursor);
|
|
|
|
this.line = this.line.slice(0, this.cursor) +
|
|
|
|
this.line.slice(this.cursor + charSize, this.line.length);
|
|
|
|
this._refreshLine();
|
|
|
|
}
|
2011-01-24 10:55:30 -08:00
|
|
|
};
|
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
|
2011-01-25 03:54:26 +01:00
|
|
|
Interface.prototype._deleteWordLeft = function() {
|
|
|
|
if (this.cursor > 0) {
|
|
|
|
var leading = this.line.slice(0, this.cursor);
|
2017-06-16 20:14:58 +03:00
|
|
|
var match = leading.match(/(?:[^\w\s]+|\w+|)\s*$/);
|
2011-01-25 03:54:26 +01:00
|
|
|
leading = leading.slice(0, leading.length - match[0].length);
|
|
|
|
this.line = leading + this.line.slice(this.cursor, this.line.length);
|
|
|
|
this.cursor = leading.length;
|
|
|
|
this._refreshLine();
|
|
|
|
}
|
2011-07-29 10:03:05 -07:00
|
|
|
};
|
2011-01-25 03:54:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype._deleteWordRight = function() {
|
|
|
|
if (this.cursor < this.line.length) {
|
|
|
|
var trailing = this.line.slice(this.cursor);
|
2017-06-16 20:14:58 +03:00
|
|
|
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
|
2011-01-25 03:54:26 +01:00
|
|
|
this.line = this.line.slice(0, this.cursor) +
|
|
|
|
trailing.slice(match[0].length);
|
|
|
|
this._refreshLine();
|
|
|
|
}
|
2011-07-29 10:03:05 -07:00
|
|
|
};
|
2011-01-25 03:54:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype._deleteLineLeft = function() {
|
|
|
|
this.line = this.line.slice(this.cursor);
|
|
|
|
this.cursor = 0;
|
|
|
|
this._refreshLine();
|
2011-07-29 10:03:05 -07:00
|
|
|
};
|
2011-01-25 03:54:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
Interface.prototype._deleteLineRight = function() {
|
|
|
|
this.line = this.line.slice(0, this.cursor);
|
|
|
|
this._refreshLine();
|
2011-07-29 10:03:05 -07:00
|
|
|
};
|
2011-01-25 03:54:26 +01:00
|
|
|
|
|
|
|
|
2012-03-20 15:14:40 -07:00
|
|
|
Interface.prototype.clearLine = function() {
|
|
|
|
this._moveCursor(+Infinity);
|
2014-09-22 13:21:11 -07:00
|
|
|
this._writeToOutput('\r\n');
|
2012-03-20 15:14:40 -07:00
|
|
|
this.line = '';
|
|
|
|
this.cursor = 0;
|
|
|
|
this.prevRows = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
Interface.prototype._line = function() {
|
|
|
|
var line = this._addHistory();
|
2012-03-20 15:14:40 -07:00
|
|
|
this.clearLine();
|
2011-01-14 03:44:05 +01:00
|
|
|
this._onLine(line);
|
|
|
|
};
|
|
|
|
|
2011-01-24 10:55:30 -08:00
|
|
|
|
2010-12-01 18:07:20 -08:00
|
|
|
Interface.prototype._historyNext = function() {
|
2010-06-07 16:43:50 -07:00
|
|
|
if (this.historyIndex > 0) {
|
|
|
|
this.historyIndex--;
|
2010-06-07 21:19:25 -07:00
|
|
|
this.line = this.history[this.historyIndex];
|
2019-03-22 03:44:26 +01:00
|
|
|
this.cursor = this.line.length; // Set cursor to end of line.
|
2010-06-07 16:43:50 -07:00
|
|
|
this._refreshLine();
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2010-06-07 16:43:50 -07:00
|
|
|
} else if (this.historyIndex === 0) {
|
|
|
|
this.historyIndex = -1;
|
|
|
|
this.cursor = 0;
|
2010-06-07 21:19:25 -07:00
|
|
|
this.line = '';
|
2010-06-07 16:43:50 -07:00
|
|
|
this._refreshLine();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-24 10:55:30 -08:00
|
|
|
|
2010-12-01 18:07:20 -08:00
|
|
|
Interface.prototype._historyPrev = function() {
|
2010-06-07 16:43:50 -07:00
|
|
|
if (this.historyIndex + 1 < this.history.length) {
|
|
|
|
this.historyIndex++;
|
2010-06-07 21:19:25 -07:00
|
|
|
this.line = this.history[this.historyIndex];
|
2019-03-22 03:44:26 +01:00
|
|
|
this.cursor = this.line.length; // Set cursor to end of line.
|
2010-06-07 16:43:50 -07:00
|
|
|
|
|
|
|
this._refreshLine();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-13 16:04:33 -08:00
|
|
|
|
2013-03-15 16:18:30 -10:00
|
|
|
// Returns the last character's display position of the given string
|
|
|
|
Interface.prototype._getDisplayPos = function(str) {
|
|
|
|
var offset = 0;
|
|
|
|
var col = this.columns;
|
2014-03-09 14:46:54 +08:00
|
|
|
var row = 0;
|
2013-03-15 16:18:30 -10:00
|
|
|
var code;
|
2013-06-04 17:01:14 +02:00
|
|
|
str = stripVTControlCharacters(str);
|
2013-03-15 16:18:30 -10:00
|
|
|
for (var i = 0, len = str.length; i < len; i++) {
|
2015-02-12 23:09:34 +03:00
|
|
|
code = str.codePointAt(i);
|
2013-03-15 16:18:30 -10:00
|
|
|
if (code >= 0x10000) { // surrogates
|
|
|
|
i++;
|
|
|
|
}
|
2014-03-09 14:46:54 +08:00
|
|
|
if (code === 0x0a) { // new line \n
|
|
|
|
offset = 0;
|
|
|
|
row += 1;
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-26 14:34:21 +08:00
|
|
|
const width = getStringWidth(code);
|
|
|
|
if (width === 0 || width === 1) {
|
|
|
|
offset += width;
|
|
|
|
} else { // width === 2
|
2013-03-15 16:18:30 -10:00
|
|
|
if ((offset + 1) % col === 0) {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var cols = offset % col;
|
2014-03-09 14:46:54 +08:00
|
|
|
var rows = row + (offset - cols) / col;
|
2017-07-10 20:55:21 -04:00
|
|
|
return { cols: cols, rows: rows };
|
2013-03-15 16:18:30 -10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-20 15:14:40 -07:00
|
|
|
// Returns current cursor's position and line
|
|
|
|
Interface.prototype._getCursorPos = function() {
|
|
|
|
var columns = this.columns;
|
2013-03-15 16:18:30 -10:00
|
|
|
var strBeforeCursor = this._prompt + this.line.substring(0, this.cursor);
|
2013-06-04 17:01:14 +02:00
|
|
|
var dispPos = this._getDisplayPos(stripVTControlCharacters(strBeforeCursor));
|
2013-03-15 16:18:30 -10:00
|
|
|
var cols = dispPos.cols;
|
|
|
|
var rows = dispPos.rows;
|
|
|
|
// If the cursor is on a full-width character which steps over the line,
|
|
|
|
// move the cursor to the beginning of the next line.
|
|
|
|
if (cols + 1 === columns &&
|
|
|
|
this.cursor < this.line.length &&
|
2015-02-12 23:09:34 +03:00
|
|
|
isFullWidthCodePoint(this.line.codePointAt(this.cursor))) {
|
2013-03-15 16:18:30 -10:00
|
|
|
rows++;
|
|
|
|
cols = 0;
|
|
|
|
}
|
2017-07-10 20:55:21 -04:00
|
|
|
return { cols: cols, rows: rows };
|
2012-03-20 15:14:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This function moves cursor dx places to the right
|
|
|
|
// (-dx for left) and refreshes the line if it is needed
|
|
|
|
Interface.prototype._moveCursor = function(dx) {
|
|
|
|
var oldcursor = this.cursor;
|
|
|
|
var oldPos = this._getCursorPos();
|
|
|
|
this.cursor += dx;
|
|
|
|
|
|
|
|
// bounds check
|
|
|
|
if (this.cursor < 0) this.cursor = 0;
|
2013-03-15 16:18:30 -10:00
|
|
|
else if (this.cursor > this.line.length) this.cursor = this.line.length;
|
2012-03-20 15:14:40 -07:00
|
|
|
|
|
|
|
var newPos = this._getCursorPos();
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Check if cursors are in the same line
|
2012-03-23 11:24:06 -07:00
|
|
|
if (oldPos.rows === newPos.rows) {
|
2013-03-15 16:18:30 -10:00
|
|
|
var diffCursor = this.cursor - oldcursor;
|
|
|
|
var diffWidth;
|
|
|
|
if (diffCursor < 0) {
|
|
|
|
diffWidth = -getStringWidth(
|
2017-07-05 09:21:40 -07:00
|
|
|
this.line.substring(this.cursor, oldcursor)
|
|
|
|
);
|
2013-03-15 16:18:30 -10:00
|
|
|
} else if (diffCursor > 0) {
|
|
|
|
diffWidth = getStringWidth(
|
2017-07-05 09:21:40 -07:00
|
|
|
this.line.substring(this.cursor, oldcursor)
|
|
|
|
);
|
2013-03-15 16:18:30 -10:00
|
|
|
}
|
2017-04-29 13:33:35 -07:00
|
|
|
moveCursor(this.output, diffWidth, 0);
|
2012-03-20 15:14:40 -07:00
|
|
|
this.prevRows = newPos.rows;
|
|
|
|
} else {
|
|
|
|
this._refreshLine();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 16:07:43 +04:00
|
|
|
function _ttyWriteDumb(s, key) {
|
|
|
|
key = key || {};
|
|
|
|
|
|
|
|
if (key.name === 'escape') return;
|
|
|
|
|
|
|
|
if (this._sawReturnAt && key.name !== 'enter')
|
|
|
|
this._sawReturnAt = 0;
|
|
|
|
|
|
|
|
if (key.ctrl && key.name === 'c') {
|
|
|
|
if (this.listenerCount('SIGINT') > 0) {
|
|
|
|
this.emit('SIGINT');
|
|
|
|
} else {
|
|
|
|
// This readline instance is finished
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key.name) {
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'return': // Carriage return, i.e. \r
|
2019-02-22 16:07:43 +04:00
|
|
|
this._sawReturnAt = Date.now();
|
|
|
|
this._line();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'enter':
|
|
|
|
// When key interval > crlfDelay
|
|
|
|
if (this._sawReturnAt === 0 ||
|
|
|
|
Date.now() - this._sawReturnAt > this.crlfDelay) {
|
|
|
|
this._line();
|
|
|
|
}
|
|
|
|
this._sawReturnAt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (typeof s === 'string' && s) {
|
|
|
|
this.line += s;
|
|
|
|
this.cursor += s.length;
|
|
|
|
this._writeToOutput(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-20 15:14:40 -07:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Handle a write from the tty
|
2011-01-14 03:44:05 +01:00
|
|
|
Interface.prototype._ttyWrite = function(s, key) {
|
2016-07-15 23:33:16 +02:00
|
|
|
const previousKey = this._previousKey;
|
2011-01-14 03:44:05 +01:00
|
|
|
key = key || {};
|
2016-07-15 23:33:16 +02:00
|
|
|
this._previousKey = key;
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2017-12-31 18:06:40 +01:00
|
|
|
// Ignore escape key, fixes
|
|
|
|
// https://github.com/nodejs/node-v0.x-archive/issues/2876.
|
2017-01-31 00:40:19 -05:00
|
|
|
if (key.name === 'escape') return;
|
2012-03-06 01:22:51 -05:00
|
|
|
|
2011-01-25 03:54:26 +01:00
|
|
|
if (key.ctrl && key.shift) {
|
|
|
|
/* Control and shift pressed */
|
|
|
|
switch (key.name) {
|
2011-07-29 10:03:05 -07:00
|
|
|
case 'backspace':
|
2011-01-25 03:54:26 +01:00
|
|
|
this._deleteLineLeft();
|
|
|
|
break;
|
|
|
|
|
2011-07-29 10:03:05 -07:00
|
|
|
case 'delete':
|
2011-01-25 03:54:26 +01:00
|
|
|
this._deleteLineRight();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (key.ctrl) {
|
2011-01-14 03:44:05 +01:00
|
|
|
/* Control key pressed */
|
2010-12-01 18:07:20 -08:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
switch (key.name) {
|
|
|
|
case 'c':
|
2015-08-12 00:01:50 +05:30
|
|
|
if (this.listenerCount('SIGINT') > 0) {
|
2011-01-14 03:44:05 +01:00
|
|
|
this.emit('SIGINT');
|
|
|
|
} else {
|
2012-04-17 11:31:07 -07:00
|
|
|
// This readline instance is finished
|
|
|
|
this.close();
|
2011-01-14 03:44:05 +01:00
|
|
|
}
|
|
|
|
break;
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'h': // delete left
|
|
|
|
this._deleteLeft();
|
|
|
|
break;
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'd': // delete right or EOF
|
|
|
|
if (this.cursor === 0 && this.line.length === 0) {
|
2012-04-17 11:31:07 -07:00
|
|
|
// This readline instance is finished
|
|
|
|
this.close();
|
2011-01-14 03:44:05 +01:00
|
|
|
} else if (this.cursor < this.line.length) {
|
|
|
|
this._deleteRight();
|
|
|
|
}
|
|
|
|
break;
|
2010-12-01 18:07:20 -08:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
case 'u': // Delete from current to start of line
|
2018-05-12 12:46:33 +05:30
|
|
|
this._deleteLineLeft();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-06-07 16:43:50 -07:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
case 'k': // Delete from current to end of line
|
2011-01-25 03:54:26 +01:00
|
|
|
this._deleteLineRight();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-03 21:53:53 -07:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'a': // Go to the start of the line
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(-Infinity);
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'e': // Go to the end of the line
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(+Infinity);
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'b': // back one character
|
2019-01-25 21:15:06 -05:00
|
|
|
this._moveCursor(-charLengthLeft(this.line, this.cursor));
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'f': // Forward one character
|
2019-01-25 21:15:06 -05:00
|
|
|
this._moveCursor(+charLengthAt(this.line, this.cursor));
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'l': // Clear the whole screen
|
2017-04-29 13:33:35 -07:00
|
|
|
cursorTo(this.output, 0, 0);
|
|
|
|
clearScreenDown(this.output);
|
2013-06-15 15:48:36 +08:00
|
|
|
this._refreshLine();
|
|
|
|
break;
|
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'n': // next history item
|
|
|
|
this._historyNext();
|
|
|
|
break;
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'p': // Previous history item
|
2011-01-14 03:44:05 +01:00
|
|
|
this._historyPrev();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'z':
|
2017-01-31 00:40:19 -05:00
|
|
|
if (process.platform === 'win32') break;
|
2015-08-12 00:01:50 +05:30
|
|
|
if (this.listenerCount('SIGTSTP') > 0) {
|
2012-02-15 09:08:26 -05:00
|
|
|
this.emit('SIGTSTP');
|
|
|
|
} else {
|
2018-07-14 18:52:49 +05:30
|
|
|
process.once('SIGCONT', (function continueProcess(self) {
|
2012-02-15 09:08:26 -05:00
|
|
|
return function() {
|
|
|
|
// Don't raise events if stream has already been abandoned.
|
|
|
|
if (!self.paused) {
|
|
|
|
// Stream must be paused and resumed after SIGCONT to catch
|
|
|
|
// SIGINT, SIGTSTP, and EOF.
|
|
|
|
self.pause();
|
|
|
|
self.emit('SIGCONT');
|
|
|
|
}
|
2018-12-10 13:27:32 +01:00
|
|
|
// Explicitly re-enable "raw mode" and move the cursor to
|
2012-06-11 07:48:02 -07:00
|
|
|
// the correct position.
|
|
|
|
// See https://github.com/joyent/node/issues/3295.
|
2012-05-21 19:43:26 -03:00
|
|
|
self._setRawMode(true);
|
|
|
|
self._refreshLine();
|
2012-02-15 09:08:26 -05:00
|
|
|
};
|
|
|
|
})(this));
|
2012-05-21 19:43:26 -03:00
|
|
|
this._setRawMode(false);
|
2012-02-15 09:08:26 -05:00
|
|
|
process.kill(process.pid, 'SIGTSTP');
|
|
|
|
}
|
2012-02-17 08:53:24 -05:00
|
|
|
break;
|
2011-01-25 03:54:26 +01:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
case 'w': // Delete backwards to a word boundary
|
2011-01-25 03:54:26 +01:00
|
|
|
case 'backspace':
|
|
|
|
this._deleteWordLeft();
|
|
|
|
break;
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
case 'delete': // Delete forward to a word boundary
|
2011-01-25 03:54:26 +01:00
|
|
|
this._deleteWordRight();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'left':
|
|
|
|
this._wordLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'right':
|
|
|
|
this._wordRight();
|
2012-02-17 08:53:24 -05:00
|
|
|
break;
|
2011-01-14 03:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (key.meta) {
|
|
|
|
/* Meta key pressed */
|
|
|
|
|
|
|
|
switch (key.name) {
|
|
|
|
case 'b': // backward word
|
2011-01-25 03:54:26 +01:00
|
|
|
this._wordLeft();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f': // forward word
|
2011-01-25 03:54:26 +01:00
|
|
|
this._wordRight();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': // delete forward word
|
2011-01-24 16:09:41 -08:00
|
|
|
case 'delete':
|
2011-01-25 03:54:26 +01:00
|
|
|
this._deleteWordRight();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2011-01-24 16:09:41 -08:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
case 'backspace': // Delete backwards to a word boundary
|
2011-01-25 03:54:26 +01:00
|
|
|
this._deleteWordLeft();
|
2011-01-24 16:09:41 -08:00
|
|
|
break;
|
2011-01-14 03:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* No modifier keys used */
|
|
|
|
|
2013-01-29 17:50:44 -08:00
|
|
|
// \r bookkeeping is only relevant if a \n comes right after.
|
2016-08-15 17:37:01 +05:30
|
|
|
if (this._sawReturnAt && key.name !== 'enter')
|
|
|
|
this._sawReturnAt = 0;
|
2013-01-29 17:50:44 -08:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
switch (key.name) {
|
2019-03-22 03:44:26 +01:00
|
|
|
case 'return': // Carriage return, i.e. \r
|
2018-02-04 13:43:21 -05:00
|
|
|
this._sawReturnAt = Date.now();
|
2011-01-14 03:44:05 +01:00
|
|
|
this._line();
|
|
|
|
break;
|
|
|
|
|
2013-01-29 17:50:44 -08:00
|
|
|
case 'enter':
|
2016-08-15 17:37:01 +05:30
|
|
|
// When key interval > crlfDelay
|
|
|
|
if (this._sawReturnAt === 0 ||
|
2018-02-04 13:43:21 -05:00
|
|
|
Date.now() - this._sawReturnAt > this.crlfDelay) {
|
2013-01-29 17:50:44 -08:00
|
|
|
this._line();
|
2016-08-15 17:37:01 +05:30
|
|
|
}
|
|
|
|
this._sawReturnAt = 0;
|
2013-01-29 17:50:44 -08:00
|
|
|
break;
|
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'backspace':
|
|
|
|
this._deleteLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
this._deleteRight();
|
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'left':
|
2019-03-07 01:03:53 +01:00
|
|
|
// Obtain the code point to the left
|
2019-01-25 21:15:06 -05:00
|
|
|
this._moveCursor(-charLengthLeft(this.line, this.cursor));
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'right':
|
2019-01-25 21:15:06 -05:00
|
|
|
this._moveCursor(+charLengthAt(this.line, this.cursor));
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'home':
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(-Infinity);
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'end':
|
2012-03-20 15:14:40 -07:00
|
|
|
this._moveCursor(+Infinity);
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'up':
|
2010-06-07 16:43:50 -07:00
|
|
|
this._historyPrev();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
case 'down':
|
2010-06-07 16:43:50 -07:00
|
|
|
this._historyNext();
|
2011-01-14 03:44:05 +01:00
|
|
|
break;
|
2010-09-12 21:47:56 -07:00
|
|
|
|
2015-05-20 21:17:10 -07:00
|
|
|
case 'tab':
|
|
|
|
// If tab completion enabled, do that...
|
2016-05-13 10:54:31 +03:00
|
|
|
if (typeof this.completer === 'function' && this.isCompletionEnabled) {
|
2016-07-15 23:33:16 +02:00
|
|
|
const lastKeypressWasTab = previousKey && previousKey.name === 'tab';
|
|
|
|
this._tabComplete(lastKeypressWasTab);
|
2015-05-20 21:17:10 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// falls through
|
|
|
|
|
2011-01-14 03:44:05 +01:00
|
|
|
default:
|
2019-02-22 16:07:43 +04:00
|
|
|
if (typeof s === 'string' && s) {
|
2011-01-14 03:44:05 +01:00
|
|
|
var lines = s.split(/\r\n|\n|\r/);
|
|
|
|
for (var i = 0, len = lines.length; i < len; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
this._line();
|
|
|
|
}
|
|
|
|
this._insertString(lines[i]);
|
|
|
|
}
|
2010-08-09 02:18:32 -07:00
|
|
|
}
|
2011-01-14 03:44:05 +01:00
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-26 17:53:04 -07:00
|
|
|
Interface.prototype[Symbol.asyncIterator] = function() {
|
|
|
|
emitExperimentalWarning('readline Interface [Symbol.asyncIterator]');
|
|
|
|
|
|
|
|
if (this[kLineObjectStream] === undefined) {
|
|
|
|
if (Readable === undefined) {
|
|
|
|
Readable = require('stream').Readable;
|
|
|
|
}
|
|
|
|
const readable = new Readable({
|
|
|
|
objectMode: true,
|
|
|
|
read: () => {
|
|
|
|
this.resume();
|
|
|
|
},
|
|
|
|
destroy: (err, cb) => {
|
|
|
|
this.off('line', lineListener);
|
|
|
|
this.off('close', closeListener);
|
|
|
|
this.close();
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const lineListener = (input) => {
|
|
|
|
if (!readable.push(input)) {
|
|
|
|
this.pause();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const closeListener = () => {
|
|
|
|
readable.push(null);
|
|
|
|
};
|
|
|
|
this.on('line', lineListener);
|
|
|
|
this.on('close', closeListener);
|
|
|
|
this[kLineObjectStream] = readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this[kLineObjectStream][Symbol.asyncIterator]();
|
|
|
|
};
|
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
/**
|
|
|
|
* accepts a readable Stream instance and makes it emit "keypress" events
|
|
|
|
*/
|
|
|
|
|
2016-05-13 10:54:31 +03:00
|
|
|
function emitKeypressEvents(stream, iface) {
|
2015-05-03 18:11:33 +00:00
|
|
|
if (stream[KEYPRESS_DECODER]) return;
|
2018-05-06 23:08:00 +02:00
|
|
|
|
|
|
|
if (StringDecoder === undefined)
|
|
|
|
StringDecoder = require('string_decoder').StringDecoder;
|
2015-05-03 18:11:33 +00:00
|
|
|
stream[KEYPRESS_DECODER] = new StringDecoder('utf8');
|
|
|
|
|
|
|
|
stream[ESCAPE_DECODER] = emitKeys(stream);
|
|
|
|
stream[ESCAPE_DECODER].next();
|
2012-03-26 15:21:25 -07:00
|
|
|
|
2016-06-23 08:42:57 +05:30
|
|
|
const escapeCodeTimeout = () => stream[ESCAPE_DECODER].next('');
|
|
|
|
let timeoutId;
|
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
function onData(b) {
|
2015-08-12 00:01:50 +05:30
|
|
|
if (stream.listenerCount('keypress') > 0) {
|
2015-05-03 18:11:33 +00:00
|
|
|
var r = stream[KEYPRESS_DECODER].write(b);
|
|
|
|
if (r) {
|
2016-06-23 08:42:57 +05:30
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
2016-08-24 00:05:59 +05:30
|
|
|
if (iface) {
|
|
|
|
iface._sawKeyPress = r.length === 1;
|
|
|
|
}
|
|
|
|
|
2015-05-03 18:11:33 +00:00
|
|
|
for (var i = 0; i < r.length; i++) {
|
2016-05-13 10:54:31 +03:00
|
|
|
if (r[i] === '\t' && typeof r[i + 1] === 'string' && iface) {
|
|
|
|
iface.isCompletionEnabled = false;
|
|
|
|
}
|
|
|
|
|
2015-07-05 19:16:47 +03:00
|
|
|
try {
|
|
|
|
stream[ESCAPE_DECODER].next(r[i]);
|
2016-06-23 08:42:57 +05:30
|
|
|
// Escape letter at the tail position
|
2017-04-29 17:05:04 -07:00
|
|
|
if (r[i] === kEscape && i + 1 === r.length) {
|
2018-04-04 00:00:35 +04:30
|
|
|
timeoutId = setTimeout(
|
|
|
|
escapeCodeTimeout,
|
|
|
|
iface ? iface.escapeCodeTimeout : ESCAPE_CODE_TIMEOUT
|
|
|
|
);
|
2016-06-23 08:42:57 +05:30
|
|
|
}
|
2015-07-05 19:16:47 +03:00
|
|
|
} catch (err) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// If the generator throws (it could happen in the `keypress`
|
2015-07-05 19:16:47 +03:00
|
|
|
// event), we need to restart it.
|
|
|
|
stream[ESCAPE_DECODER] = emitKeys(stream);
|
|
|
|
stream[ESCAPE_DECODER].next();
|
|
|
|
throw err;
|
2016-05-13 10:54:31 +03:00
|
|
|
} finally {
|
|
|
|
if (iface) {
|
|
|
|
iface.isCompletionEnabled = true;
|
|
|
|
}
|
2015-07-05 19:16:47 +03:00
|
|
|
}
|
2015-05-03 18:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-26 15:21:25 -07:00
|
|
|
} else {
|
|
|
|
// Nobody's watching anyway
|
|
|
|
stream.removeListener('data', onData);
|
|
|
|
stream.on('newListener', onNewListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onNewListener(event) {
|
2017-01-31 00:40:19 -05:00
|
|
|
if (event === 'keypress') {
|
2012-03-26 15:21:25 -07:00
|
|
|
stream.on('data', onData);
|
|
|
|
stream.removeListener('newListener', onNewListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:01:50 +05:30
|
|
|
if (stream.listenerCount('keypress') > 0) {
|
2012-03-26 15:21:25 -07:00
|
|
|
stream.on('data', onData);
|
|
|
|
} else {
|
|
|
|
stream.on('newListener', onNewListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* moves the cursor to the x and y coordinate on the given stream
|
|
|
|
*/
|
|
|
|
|
|
|
|
function cursorTo(stream, x, y) {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (stream === null || stream === undefined)
|
2014-09-22 13:21:11 -07:00
|
|
|
return;
|
|
|
|
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof x !== 'number' && typeof y !== 'number')
|
2012-03-26 15:21:25 -07:00
|
|
|
return;
|
|
|
|
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof x !== 'number')
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_CURSOR_POS();
|
2012-03-26 15:21:25 -07:00
|
|
|
|
2015-01-28 20:05:53 -05:00
|
|
|
if (typeof y !== 'number') {
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(CSI`${x + 1}G`);
|
2012-03-26 15:21:25 -07:00
|
|
|
} else {
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(CSI`${y + 1};${x + 1}H`);
|
2012-03-26 15:21:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* moves the cursor relative to its current location
|
|
|
|
*/
|
|
|
|
|
|
|
|
function moveCursor(stream, dx, dy) {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (stream === null || stream === undefined)
|
2014-09-22 13:21:11 -07:00
|
|
|
return;
|
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
if (dx < 0) {
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(CSI`${-dx}D`);
|
2012-03-26 15:21:25 -07:00
|
|
|
} else if (dx > 0) {
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(CSI`${dx}C`);
|
2012-03-26 15:21:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dy < 0) {
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(CSI`${-dy}A`);
|
2012-03-26 15:21:25 -07:00
|
|
|
} else if (dy > 0) {
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(CSI`${dy}B`);
|
2012-03-26 15:21:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clears the current line the cursor is on:
|
|
|
|
* -1 for left of the cursor
|
|
|
|
* +1 for right of the cursor
|
|
|
|
* 0 for the entire line
|
|
|
|
*/
|
|
|
|
|
|
|
|
function clearLine(stream, dir) {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (stream === null || stream === undefined)
|
2014-09-22 13:21:11 -07:00
|
|
|
return;
|
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
if (dir < 0) {
|
|
|
|
// to the beginning
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(kClearToBeginning);
|
2012-03-26 15:21:25 -07:00
|
|
|
} else if (dir > 0) {
|
|
|
|
// to the end
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(kClearToEnd);
|
2012-03-26 15:21:25 -07:00
|
|
|
} else {
|
|
|
|
// entire line
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(kClearLine);
|
2012-03-26 15:21:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clears the screen from the current position of the cursor down
|
|
|
|
*/
|
|
|
|
|
|
|
|
function clearScreenDown(stream) {
|
2015-01-28 20:05:53 -05:00
|
|
|
if (stream === null || stream === undefined)
|
2014-09-22 13:21:11 -07:00
|
|
|
return;
|
|
|
|
|
2017-04-29 17:05:04 -07:00
|
|
|
stream.write(kClearScreenDown);
|
2012-03-26 15:21:25 -07:00
|
|
|
}
|
2017-04-29 13:33:35 -07:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Interface,
|
|
|
|
clearLine,
|
|
|
|
clearScreenDown,
|
|
|
|
createInterface,
|
|
|
|
cursorTo,
|
|
|
|
emitKeypressEvents,
|
|
|
|
moveCursor
|
|
|
|
};
|