-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
105 lines (87 loc) · 2.31 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
'use strict';
var Prompt = require('prompt-list');
var isNumber = require('is-number');
var cyan = require('ansi-cyan');
var dim = require('ansi-dim');
var red = require('ansi-red');
/**
* Constructor
*/
function RawList(question, answers, rl) {
Prompt.apply(this, arguments);
var prompt = this;
this.choices.paginator.footer = '';
this.options.footer = dim('\n(Move up and down to reveal more choices)');
this.errorMessage = red(' >> Please enter a valid index');
this.rawDefault = 0;
if (this.choices.isValidIndex(this.question.default)) {
this.position = this.rawDefault = Number(this.question.default);
this.question.default = null;
this.initialDefault = null;
}
if (typeof this.options.validate !== 'function') {
this.options.validate = function(val) {
this.rl.history = [];
if (this.status !== 'submitted') {
return true;
}
return val != null;
};
}
this.on('render', function(context) {
if (context.status !== 'answered') {
context.message += '\n Answer: ' + prompt.rl.line;
context.message += prompt.options.footer;
}
if (context.status === 'interacted') {
prompt.options.footer = '';
}
});
this.action('number', function(pos) {
if (prompt.rl.line === '') {
return this.position(pos);
}
var n = Number(prompt.rl.line) - 1;
if (this.choices.isValidIndex(n)) {
return n;
}
return -1;
});
this.choices.options.format = function(line) {
line = this.index + 1 + ') ' + line;
return this.position === this.index ? cyan(line) : line;
};
}
/**
* Inherit `Prompt`
*/
Prompt.extend(RawList);
/**
* Render the answer value
*/
RawList.prototype.renderAnswer = function(input) {
return cyan(this.answer);
};
/**
* Get the selected answer
* @param {String} `input`
* @param {Object} `key`
*/
RawList.prototype.getAnswer = function(input, key) {
if (key && key.name === 'line') {
if (key.value === '') {
input = this.position;
} else if (isNumber(key.value)) {
input = Number(key.value) - 1;
} else if (input.trim()) {
return (this.answer = this.choices.key(input));
}
if (input <= this.choices.length && input >= 0) {
return (this.answer = this.choices.key(input));
}
}
};
/**
* Module exports
*/
module.exports = RawList;