-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (61 loc) · 2.01 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
const chalk = require('chalk')
const InputPrompt = require('inquirer/lib/prompts/input')
const isFunction = require('lodash.isfunction')
const partialRight = require('lodash.partialright')
const identity = i => i
class MaxLengthInputPrompt extends InputPrompt {
constructor(...args) {
super(...args)
if (!this.opt.maxLength) {
this.throwParamError('maxLength')
}
this.opt.validate = this.createValidator(this.opt.validate)
}
measureInput(input) {
const { filter } = this.opt
return filter(input, this.answers)
}
render(error) {
const { maxLength, transformer } = this.opt
const answered = this.status === 'answered'
const transform = partialRight(
isFunction(transformer) ? transformer : identity,
this.answers
)
let bottomContent = ''
let message = this.getQuestion()
const { length } = this.measureInput(this.rl.line)
if (answered) {
message += chalk.cyan(this.answer)
} else {
message += transform(this.rl.line)
}
if (error) {
bottomContent = chalk.red('>> ') + error
} else if (!answered) {
const percent = length / maxLength * 100
const color =
percent < 80 ? chalk.green : percent > 100 ? chalk.red : chalk.yellow
bottomContent = `(${color(length)}/${this.opt.maxLength} characters)`
}
this.screen.render(message, bottomContent)
}
createValidator(validate) {
const { filter, maxLength } = this.opt
return (...args) => {
const [input, answers] = args
const didInput =
(input && !filter) || (input && filter('', answers) !== input)
const { length } = input
if (length > maxLength) {
this.rl.line = this.rl.history[0]
this.rl.cursor = this.rl.line.length
return this.constructor.VALIDATION_ERROR_MESSAGE
}
return validate(...args, didInput ? this.rl.history[0] : '')
}
}
}
MaxLengthInputPrompt.VALIDATION_ERROR_MESSAGE =
'Input contains too many characters!'
module.exports = MaxLengthInputPrompt