-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
301 lines (248 loc) · 6.6 KB
/
Copy pathindex.js
File metadata and controls
301 lines (248 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict';
var util = require('util');
var debug = require('debug')('readline-ui');
var Emitter = require('component-emitter');
var stringWidth = require('string-width');
var utils = require('readline-utils');
var cached;
/**
* Create a readline interface to use in prompts
*/
function UI(options) {
if (!(this instanceof UI)) {
var ui = Object.create(UI.prototype);
UI.apply(ui, arguments);
return ui;
}
debug('initializing from <%s>', __filename);
this.options = utils.createOptions(options);
this.appendedLines = 0;
this.height = 0;
this.initInterface();
}
/**
* Decorate emitter methods
*/
Emitter(UI.prototype);
/**
* Initialize settings and events.
*/
UI.prototype.initInterface = function() {
if (this.initialized) return;
this.initialized = true;
var self = this;
if (typeof this.rl === 'undefined') {
this.rl = utils.createInterface(this.options);
}
this.forceClose = this.forceClose.bind(this);
this.onKeypress = this.onKeypress.bind(this);
this.rl.input.on('keypress', self.onKeypress);
this.resume();
this.rl.on('line', function(line) {
setImmediate(function() {
self.emit('line', line, {name: 'line', value: line});
})
});
this.rl.on('SIGINT', this.forceClose);
process.on('exit', this.forceClose);
};
/**
* Handle `keypress` events.
*
* @param {String} `str`
* @param {Object} `key`
* @return {undefined}
* @api public
*/
UI.prototype.onKeypress = function(input, key) {
utils.emitKeypress(this, input, key);
};
/**
* Render the prompt with the given `input` and optional `footer`.
* @param {String} `input`
* @param {String} `footer` (optional)
* @return {undefined}
* @api public
*/
UI.prototype.render = function(input, footer) {
this.rl.output.unmute();
this.clearLines(this.appendedLines);
// Write message to screen and setPrompt to control backspace
var line = utils.lastLine(input);
var rawLine = utils.unstyle(line);
// Remove the last line from our prompt. We can't rely
// on the input of rl.line (mainly because of the password
// prompt), so just rely on it's length.
var prompt = line;
if (this.rl.line.length) {
prompt = prompt.slice(0, -this.rl.line.length);
}
this.rl.setPrompt(prompt);
// setPrompt will change cursor position, now we can get correct value
var cursorPos = this.cacheCursorPos();
var width = utils.cliWidth(this.rl);
input = utils.forceLineReturn(input, width);
if (footer) {
footer = utils.forceLineReturn(footer, width);
}
// Manually insert an extra line if we're at the end of
// the line. This prevents the cursor from appearing at
// the beginning of the current line.
if (rawLine.length % width === 0) {
input += '\n';
}
var fullContent = input + (footer ? '\n' + String(footer) : '');
this.rl.output.write(fullContent);
// We need to consider parts of the prompt under the
// cursor as part of the bottom string in order to
// correctly cleanup and re-render.
var promptLineUpDiff = Math.floor(rawLine.length / width) - cursorPos.rows;
var footerHeight = promptLineUpDiff + (footer ? utils.height(footer) : 0);
if (footerHeight > 0) {
utils.up(this.rl, footerHeight);
}
// Reset cursor at the beginning of the line
var lastLine = utils.lastLine(fullContent);
utils.left(this.rl, stringWidth(lastLine));
// Adjust cursor on the right
var newPos = utils.unstyle(input).length;
utils.right(this.rl, newPos);
// Set up state for next re-rendering
this.appendedLines = footerHeight;
this.height = utils.height(fullContent);
this.rl.output.mute();
};
/**
* Remove `n` lines from the bottom of the terminal
* @param {Number} `lines` Number of lines to remove
* @api public
*/
UI.prototype.clearLines = function(n) {
if (n) utils.down(this.rl, n);
utils.clearLine(this.rl, this.height);
};
/**
* Cache the current cursor's column and line position.
* @returns {Object} UI instance.
* @api public
*/
UI.prototype.cacheCursorPos = function() {
this.cursorPos = utils.cursorPosition(this.rl);
return this.cursorPos;
};
/**
* Restore the cursor position to the cached column and line.
* @return {Object} UI instance.
* @api public
*/
UI.prototype.restoreCursorPos = function() {
utils.restoreCursorPos(this.rl, this.cursorPos);
return this;
};
/**
* Resume the input stream.
* @api public
*/
UI.prototype.resume = function() {
this.rl.resume();
};
/**
* Pause the input stream, allowing it to be resumed later if necessary.
* @api public
*/
UI.prototype.pause = function() {
this.rl.pause();
this.emit('pause');
};
/**
* Close the `readline.Interface` instance and relinquish
* control over the input and output streams. Also removes
* event listeners, and restores/unmutes prompt functionality.
* @api public
*/
UI.prototype.close = function() {
utils.close(this.rl);
this.emit('close');
};
/**
* Close the interface when the keypress is `^C`
* @api public
*/
UI.prototype.forceClose = function() {
utils.forceClose(this.rl);
};
/**
* Returns an "indentity" function that calls `.close()`,
* which can be used as the final `.then()` function with
* promises.
* @api public
*/
UI.prototype.finish = function() {
var ui = this;
return function(val) {
ui.close();
ui.emit('finish');
return val;
};
};
/**
* Default method for writing a prompt to the terminal.
* This can be overridden.
* @api public
*/
UI.prototype.end = function(newline) {
this.rl.setPrompt('');
this.rl.output.unmute();
this.rl.output.write(newline !== false ? '\n' : '');
utils.cursorShow(this.rl);
};
/**
* Mutes the output stream that was used to create the
* readline interface, and returns a function for unmuting the
* stream. This is useful in unit tests.
*
* ```js
* // mute the stream
* var unmute = ui.mute();
*
* // unmute the stream
* unmute();
* ```
* @return {Function}
* @api public
*/
UI.prototype.mute = function() {
var rl = this.rl;
var unmute = rl.output.unmute;
rl.output.unmute = function() {};
rl.output.mute();
return function() {
rl.output.unmute = unmute;
unmute();
};
};
/**
* Unmute then write to the output stream that was used
* to create the readline interface, then re-mute the stream.
* Useful for debugging prompts.
*
* @api public
*/
UI.prototype.log = function(input) {
this.rl.output.unmute();
this.rl.output.write(util.inspect.apply(util, arguments));
this.rl.output.mute();
};
/**
* Expose `UI`
*/
module.exports = UI;
/**
* Expose `UI.create` for using a single instance across
* multiple prompts.
*/
module.exports.create = function(options) {
if (cached) return cached;
cached = new UI(options);
return cached;
};