|
| 1 | +/// <reference path="./inquirer.d.ts" /> |
| 2 | +import chalk from 'chalk'; |
| 3 | +import inquirer from 'inquirer'; |
| 4 | +import InputPrompt from 'inquirer/lib/prompts/input'; |
| 5 | +import observe from 'inquirer/lib/utils/events'; |
| 6 | +import {Interface as ReadlineInterface, Key} from 'readline'; |
| 7 | +import type {Subscription} from 'rxjs/internal/Subscription'; |
| 8 | + |
| 9 | +import Answers = inquirer.Answers; |
| 10 | +import InputCustomOptions = inquirer.InputCustomOptions; |
| 11 | +import SuccessfulPromptStateData = inquirer.prompts.SuccessfulPromptStateData; |
| 12 | + |
| 13 | +interface KeyDescriptor { |
| 14 | + value: string; |
| 15 | + key: Key; |
| 16 | +} |
| 17 | + |
| 18 | +export default class InputCustomPrompt< |
| 19 | + TQuestion extends InputCustomOptions = InputCustomOptions |
| 20 | +> extends InputPrompt<TQuestion> { |
| 21 | + private lineSubscription: Subscription; |
| 22 | + private readonly tabCompletion: string[]; |
| 23 | + |
| 24 | + constructor( |
| 25 | + question: TQuestion, |
| 26 | + readLine: ReadlineInterface, |
| 27 | + answers: Answers |
| 28 | + ) { |
| 29 | + super(question, readLine, answers); |
| 30 | + |
| 31 | + if (this.opt.log) { |
| 32 | + this.rl.write(this.opt.log(answers)); |
| 33 | + } |
| 34 | + |
| 35 | + if (!this.opt.maxLength) { |
| 36 | + this.throwParamError('maxLength'); |
| 37 | + } |
| 38 | + |
| 39 | + const events = observe(this.rl); |
| 40 | + this.lineSubscription = events.keypress.subscribe( |
| 41 | + this.onKeyPress2.bind(this) |
| 42 | + ); |
| 43 | + this.tabCompletion = (this.opt.tabCompletion || []) |
| 44 | + .map((item) => item.value) |
| 45 | + .sort((a, b) => a.localeCompare(b)); |
| 46 | + } |
| 47 | + |
| 48 | + onEnd(state: SuccessfulPromptStateData): void { |
| 49 | + this.lineSubscription.unsubscribe(); |
| 50 | + super.onEnd(state); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @see https://nodejs.org/api/readline.html#readline_rl_write_data_key |
| 55 | + * @see https://nodejs.org/api/readline.html#readline_rl_line |
| 56 | + */ |
| 57 | + updateLine(line: string): void { |
| 58 | + this.rl.write(null as any, {ctrl: true, name: 'b'}); |
| 59 | + this.rl.write(null as any, {ctrl: true, name: 'd'}); |
| 60 | + this.rl.write(line.substr(this.rl.line.length)); |
| 61 | + } |
| 62 | + |
| 63 | + onKeyPress2(e: KeyDescriptor): void { |
| 64 | + if (e.key.name === 'tab' && this.tabCompletion.length > 0) { |
| 65 | + let line = this.rl.line.trim(); |
| 66 | + if (line.length > 0) { |
| 67 | + for (const item of this.tabCompletion) { |
| 68 | + if (item.startsWith(line) && item !== line) { |
| 69 | + line = item; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + this.updateLine(line); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + measureInput(input: string): number { |
| 79 | + if (this.opt.filter) { |
| 80 | + return this.opt.filter(input).length; |
| 81 | + } |
| 82 | + return input.length; |
| 83 | + } |
| 84 | + |
| 85 | + render(error?: string): void { |
| 86 | + const answered = this.status === 'answered'; |
| 87 | + |
| 88 | + let message = this.getQuestion(); |
| 89 | + const length = this.measureInput(this.rl.line); |
| 90 | + |
| 91 | + if (answered) { |
| 92 | + message += chalk.cyan(this.answer); |
| 93 | + } else if (this.opt.transformer) { |
| 94 | + message += this.opt.transformer(this.rl.line, this.answers, {}); |
| 95 | + } |
| 96 | + |
| 97 | + let bottomContent = ''; |
| 98 | + |
| 99 | + if (error) { |
| 100 | + bottomContent = chalk.red('>> ') + error; |
| 101 | + } else if (!answered) { |
| 102 | + const maxLength = this.opt.maxLength(this.answers); |
| 103 | + if (maxLength < Infinity) { |
| 104 | + const lengthRemaining = maxLength - length; |
| 105 | + const color = |
| 106 | + lengthRemaining <= 5 |
| 107 | + ? chalk.red |
| 108 | + : lengthRemaining <= 10 |
| 109 | + ? chalk.yellow |
| 110 | + : chalk.grey; |
| 111 | + bottomContent = color(`${lengthRemaining} characters left`); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + this.screen.render(message, bottomContent); |
| 116 | + } |
| 117 | +} |
0 commit comments