-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
84 lines (67 loc) · 1.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
'use strict';
const Radio = require('prompt-radio');
const cyan = require('ansi-cyan');
const dim = require('ansi-dim');
/**
* List prompt
*/
function List(question, answers, ui) {
Radio.apply(this, arguments);
this.listInitialized = false;
this.question.type = 'list';
this.on('ask', this.onAsk.bind(this));
this.on('render', () => {
if (this.contextHistory.length > 0) this.helpMessage = '';
});
}
/**
* Inherit Radio
*/
Radio.extend(List);
/**
* Render a choice.
*/
List.prototype.onAsk = function() {
if (this.listInitialized) return;
this.listInitialized = true;
this.helpMessage = this.options.helpMessage || dim('(Use arrow keys)');
this.choices.options.checkbox = false;
this.choices.options.format = this.renderChoice(this.choices);
};
/**
* Render a choice.
*/
List.prototype.renderChoice = function(choices) {
return function(line) {
return choices.position === choices.index ? cyan(line) : line;
};
};
/**
* Render final selected answer when "line" ("enter" keypress)
* is emitted
*/
List.prototype.renderAnswer = function() {
return cyan(this.choices.get(this.position, 'value'));
};
/**
* Get selected list item
*/
List.prototype.getAnswer = function() {
return this.choices.key(this.position);
};
/**
* overriding "when" function to avoid setting a value when there
* is not a default value and the question was not asked
*/
List.prototype.when = function() {
var that = this;
return Promise.resolve(Radio.prototype.when.apply(this, arguments))
.then(function(when) {
that.position = that.choices.getIndex(that.options.default);
return when;
});
};
/**
* Module exports
*/
module.exports = List;