Add ability to ask question from readline
This commit is contained in:
parent
54b1f8028a
commit
e3ce73a214
@ -83,7 +83,13 @@ Interface.prototype.__defineGetter__('columns', function() {
|
|||||||
|
|
||||||
Interface.prototype.setPrompt = function(prompt, length) {
|
Interface.prototype.setPrompt = function(prompt, length) {
|
||||||
this._prompt = prompt;
|
this._prompt = prompt;
|
||||||
this._promptLength = length ? length : Buffer.byteLength(prompt);
|
if (length) {
|
||||||
|
this._promptLength = length;
|
||||||
|
} else {
|
||||||
|
var lines = prompt.split(/[\r\n]/);
|
||||||
|
var lastLine = lines[lines.length - 1];
|
||||||
|
this._promptLength = Buffer.byteLength(lastLine);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -97,6 +103,28 @@ Interface.prototype.prompt = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Interface.prototype.question = function(query, cb) {
|
||||||
|
if (cb) {
|
||||||
|
this._oldPrompt = this._prompt;
|
||||||
|
this.setPrompt(query);
|
||||||
|
this._questionCallback = cb;
|
||||||
|
this.prompt();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Interface.prototype._onLine = function(line) {
|
||||||
|
if (this._questionCallback) {
|
||||||
|
var cb = this._questionCallback;
|
||||||
|
this._questionCallback = null;
|
||||||
|
this.setPrompt(this._oldPrompt);
|
||||||
|
cb(line)
|
||||||
|
} else {
|
||||||
|
this.emit('line', line);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Interface.prototype._addHistory = function() {
|
Interface.prototype._addHistory = function() {
|
||||||
if (this.line.length === 0) return '';
|
if (this.line.length === 0) return '';
|
||||||
|
|
||||||
@ -149,7 +177,7 @@ Interface.prototype.write = function(d) {
|
|||||||
Interface.prototype._normalWrite = function(b) {
|
Interface.prototype._normalWrite = function(b) {
|
||||||
// Very simple implementation right now. Should try to break on
|
// Very simple implementation right now. Should try to break on
|
||||||
// new lines.
|
// new lines.
|
||||||
this.emit('line', b.toString());
|
this._onLine(b.toString());
|
||||||
};
|
};
|
||||||
|
|
||||||
Interface.prototype._insertString = function(c) {
|
Interface.prototype._insertString = function(c) {
|
||||||
@ -304,7 +332,7 @@ Interface.prototype._ttyWrite = function(b) {
|
|||||||
case 13: /* enter */
|
case 13: /* enter */
|
||||||
var line = this._addHistory();
|
var line = this._addHistory();
|
||||||
this.output.write('\r\n');
|
this.output.write('\r\n');
|
||||||
this.emit('line', line);
|
this._onLine(line);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 127: /* backspace */
|
case 127: /* backspace */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user