forked from commitizen/cz-conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 62
/
LimitedInputPrompt.js
79 lines (64 loc) · 1.87 KB
/
LimitedInputPrompt.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
const chalk = require('chalk');
const InputPrompt = require('inquirer/lib/prompts/input');
class LimitedInputPrompt extends InputPrompt {
constructor(...args) {
super(...args);
if (!this.opt.maxLength) {
this.throwParamError('maxLength');
}
this.originalMessage = this.opt.message;
this.spacer = new Array(this.opt.maxLength).fill('-').join('');
if (this.opt.leadingLabel) {
if (typeof this.opt.leadingLabel === 'function') {
this.leadingLabel = ' ' + this.opt.leadingLabel(this.answers);
} else {
this.leadingLabel = ' ' + this.opt.leadingLabel;
}
} else {
this.leadingLabel = '';
}
this.leadingLength = this.leadingLabel.length;
}
remainingChar() {
return this.opt.maxLength - this.leadingLength - this.rl.line.length;
}
onKeypress() {
if (this.rl.line.length > this.opt.maxLength - this.leadingLength) {
this.rl.line = this.rl.line.slice(
0,
this.opt.maxLength - this.leadingLength
);
this.rl.cursor--;
}
this.render();
}
getCharsLeftText() {
const chars = this.remainingChar();
if (chars > 15) {
return chalk.green(`${chars} chars left`);
} else if (chars > 5) {
return chalk.yellow(`${chars} chars left`);
} else {
return chalk.red(`${chars} chars left`);
}
}
render(error) {
let bottomContent = '';
let message = this.getQuestion();
let appendContent = '';
const isFinal = this.status === 'answered';
if (isFinal) {
appendContent = this.answer;
} else {
appendContent = this.rl.line;
}
message = `${message}
[${this.spacer}] ${this.getCharsLeftText()}
${this.leadingLabel} ${appendContent}`;
if (error) {
bottomContent = chalk.red('>> ') + error;
}
this.screen.render(message, bottomContent);
}
}
module.exports = LimitedInputPrompt;