@commitlint/prompt
Version:
commitizen prompt using commitlint.config.js
86 lines • 3.14 kB
JavaScript
/// <reference path="./inquirer.d.ts" />
import chalk from 'chalk';
import InputPrompt from 'inquirer/lib/prompts/input.js';
import observe from 'inquirer/lib/utils/events.js';
export default class InputCustomPrompt extends InputPrompt {
lineSubscription;
tabCompletion;
constructor(question, readLine, answers) {
super(question, readLine, answers);
if (this.opt.log) {
this.rl.write(this.opt.log(answers));
}
if (!this.opt.maxLength) {
this.throwParamError('maxLength');
}
const events = observe(this.rl);
this.lineSubscription = events.keypress.subscribe(this.onKeyPress2.bind(this));
this.tabCompletion = (this.opt.tabCompletion || [])
.map((item) => item.value)
.sort((a, b) => a.localeCompare(b));
}
onEnd(state) {
this.lineSubscription.unsubscribe();
// Add or remove leading blank if rule is active.
state.value = this.opt.forceLeadingBlankFn(state.value);
super.onEnd(state);
}
/**
* @see https://nodejs.org/api/readline.html#readline_rl_write_data_key
* @see https://nodejs.org/api/readline.html#readline_rl_line
*/
updateLine(line) {
this.rl.write(null, { ctrl: true, name: 'b' });
this.rl.write(null, { ctrl: true, name: 'd' });
this.rl.write(line.substr(this.rl.line.length));
}
onKeyPress2(e) {
if (e.key.name === 'tab' && this.tabCompletion.length > 0) {
let line = this.rl.line.trim();
if (line.length > 0) {
for (const item of this.tabCompletion) {
if (item.startsWith(line) && item !== line) {
line = item;
break;
}
}
}
this.updateLine(line);
}
}
measureInput(input) {
if (this.opt.filter) {
return this.opt.filter(input, this.answers).length;
}
return input.length;
}
render(error) {
const answered = this.status === 'answered';
let message = this.getQuestion();
const length = this.measureInput(this.rl.line);
if (answered) {
message += chalk.cyan(this.answer);
}
else if (this.opt.transformer) {
message += this.opt.transformer(this.rl.line, this.answers, {});
}
let bottomContent = '';
if (error) {
bottomContent = chalk.red('>> ') + error;
}
else if (!answered) {
const maxLength = this.opt.maxLength(this.answers);
if (maxLength < Infinity) {
const lengthRemaining = maxLength - length;
const color = lengthRemaining <= 5
? chalk.red
: lengthRemaining <= 10
? chalk.yellow
: chalk.grey;
bottomContent = color(`${lengthRemaining} characters left`);
}
}
this.screen.render(message, bottomContent);
}
}
//# sourceMappingURL=InputCustomPrompt.js.map