-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
321 lines (284 loc) · 6.67 KB
/
index.js
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
'use strict';
/**
* Module dependencies.
*
*/
var eventEmitter = require('events').EventEmitter;
var encode = require('./encode');
var colors = require('colors');
var readline = require('readline');
var stream = process.stdin;
var processOut = process.stdout;
process.on('exit', function () {
processOut.write(encode('[?25h'));
})
/**
* Expose function invoke
*/
module.exports = function(conf) {
return new Select(conf);
};
/**
* Initialize a new Select.
*
* @param {Object} conf
* @api public
*/
var Select = function (conf){
this.config = {
pointer: '> ',
pointerColor: 'white',
checked: ' ✓',
unchecked: '',
checkedColor: 'green',
uncheckedColor: 'green',
msgCancel: 'No selected options!',
msgCancelColor: 'red',
multiSelect: true,
inverse: false,
prepend: false,
disableInput: true
};
this.options = [];
this.optionsLength = 0;
this.pointerPosition = 0;
this.optionsSelected = [];
this.currentoption = undefined;
this.select = undefined;
this.keypress = this.keypress.bind(this);
this.onData = this.onData.bind(this);
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
for ( var c in conf ) {
this.config[c] = conf[c];
}
stream.on('error', function (err) {
console.error(err);
process.exit();
})
stream.on('keypress', this.keypress);
stream.on('data', this.onData);
};
/**
* Disable input
*
* @api private
*/
Select.prototype.onData = function () {
if (this.config.disabledInput) {
readline.clearLine(processOut);
}
}
/**
* Inherit from `EventEmitter.prototype`.
*/
Select.prototype.__proto__ = eventEmitter.prototype;
/**
* @event select
* Fires after press return
*
* @param {Object/Object[]} option/options
*
* @event cancel
* Fires after press esc and cancel select process
*
* @param {Object/Object[]} option/options
*/
/**
* Render options.
*
* @api private
*/
Select.prototype.render = function () {
var me = this;
var indent = me.config.pointer.replace(/./g, ' ');
var checkedChar = me.config.checked[ me.config.checkedColor ];
var uncheckedChar = me.config.unchecked[ me.config.uncheckedColor ];
me.options.forEach(function(option, position){
var isHighlighted = (position === me.pointerPosition);
var prefix = isHighlighted ? me.config.pointer : indent;
var checked = me.config.multiSelect ?
me.optionsSelected.indexOf(option) !== -1 ? checkedChar : uncheckedChar
: '';
me.currentoption = isHighlighted ? option : me.currentoption;
console.log( prefix[ me.config.pointerColor ] +
(me.config.prepend ? checked : '') +
(position === me.pointerPosition && me.config.inverse ? option.text[ 'inverse' ] : option.text) +
(me.config.prepend ? '' : checked)
);
});
processOut.write(encode('[?25l'));
};
/**
* Clean the terminal options and render options.
*
* @api private
*/
Select.prototype.changeSelected = function () {
this.clearList();
this.render();
};
/**
* Clean list
*
* @api private
*/
Select.prototype.clearList = function (postionCursor) {
readline.cursorTo(processOut, 0);
readline.moveCursor(processOut, 0, -this.optionsLength);
readline.clearScreenDown(processOut);
};
/**
* Change pointerPosition
*
* @api private
*/
Select.prototype.next = function () {
this.pointerPosition += ( this.pointerPosition < (this.optionsLength - 1) ) ? 1 : 0;
this.changeSelected();
};
/**
* Change pointerPosition
*
* @api private
*/
Select.prototype.prev = function () {
this.pointerPosition -= ( this.pointerPosition > 0 ) ? 1 : 0;
this.changeSelected();
};
/**
* Check the option
*
* @api private
*/
Select.prototype.checkoption = function () {
var optionPosition = this.optionsSelected.indexOf(this.currentoption);
if ( optionPosition === -1 ) {
if ( this.config.multiSelect ) {
this.optionsSelected.push(this.options[this.pointerPosition]);
} else {
this.optionsSelected.splice(0, 1, this.options[this.pointerPosition]);
}
this.changeSelected();
}
};
/**
* Uncheck the option
*
* @api private
*/
Select.prototype.uncheckoption = function () {
var optionPosition = this.optionsSelected.indexOf(this.currentoption);
if ( optionPosition !== -1 ) {
this.optionsSelected.splice(optionPosition, 1);
this.changeSelected();
}
};
/**
* Toggle the option
*
* @api private
*/
Select.prototype.toggleoption = function () {
var optionPosition = this.optionsSelected.indexOf(this.currentoption);
if ( this.config.multiSelect ) {
if ( optionPosition !== -1 ) {
this.uncheckoption();
} else {
this.checkoption();
}
} else {
// if in single select mode, just select the option
this.checkoption();
readline.moveCursor(processOut, 0, -1);/* remove new line */
this.selectoption();
}
};
/**
* Add options in select list
*
* @return {Object/Class} Select
* @api public
*/
Select.prototype.option = function (text, value) {
value = value !== undefined ? value : text;
this.options.push({ text: text, value: value });
this.optionsLength = this.options.length;
return this;
};
/**
* Show the options in the terminal
*
* @return {Object/Class} Select
* @api public
*/
Select.prototype.list = function (onSelect) {
this.render();
this.select = onSelect || function(){};
return this;
};
/**
* Finish the select-shell
*
* @api private
*/
Select.prototype.close = function () {
this.rl.close();
stream.removeListener('keypress', this.keypress);
processOut.write(encode('[?25h'));
};
/**
* Select the options and finish
*
* @api private
*/
Select.prototype.selectoption = function () {
if(!this.config.multiSelect){
this.checkoption();
}
var r = this.optionsSelected;
this.close();
this.select(r);
this.emit('select', r);
};
Select.prototype.cancel = function () {
var r = this.config.multiSelect ? this.optionsSelected : this.optionsSelected[0];
this.close();
if (this.config.msgCancel) console.log(this.config.msgCancel[this.config.msgCancelColor]);
this.emit('cancel', this.optionsSelected);
};
/**
* Event maneger events on keypress
*
* @api private
*/
Select.prototype.keypress = function (ch, key) {
key = key || {};
switch(key.name) {
case 'up':
this.prev();
break;
case 'down':
this.next();
break;
case 'right':
this.checkoption();
break;
case 'left':
this.uncheckoption();
break;
case 'space':
this.toggleoption();
break;
case 'return':
readline.moveCursor(processOut, 0, -1);/* remove new line */
this.selectoption();
break;
case 'escape':
this.cancel();
break;
default: break;
}
};