|
| 1 | +/** |
| 2 | + * @description: inquirer plugin - Search Checkbox |
| 3 | + * @author: @Zhengqbbb (zhengqbbb@gmail.com) |
| 4 | + * @license MIT |
| 5 | + */ |
| 6 | + |
| 7 | +import Base from "inquirer/lib/prompts/base"; |
| 8 | +import observe from "inquirer/lib/utils/events"; |
| 9 | +import { map, takeUntil } from "rxjs/operators"; |
| 10 | +import type { Interface as ReadlineInterface } from "readline"; |
| 11 | +import type { Answers, Question } from "inquirer"; |
| 12 | +import { ansiEscapes, style } from "../shared"; |
| 13 | +import type { CompletePromptQuestionOptions } from "../shared"; |
| 14 | + |
| 15 | +export type { CompletePromptQuestionOptions } from "../shared"; |
| 16 | +export class CompleteInput extends Base { |
| 17 | + private completeValue?: string; |
| 18 | + private answer?: string; |
| 19 | + private state?: string; |
| 20 | + private done: any; |
| 21 | + |
| 22 | + constructor(questions: Question, readline: ReadlineInterface, answers: Answers) { |
| 23 | + super(questions, readline, answers); |
| 24 | + const { completeValue } = this.opt as unknown as CompletePromptQuestionOptions; |
| 25 | + if (typeof completeValue === "string") this.completeValue = completeValue; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @description: Start the Inquiry session |
| 30 | + * @param {Function} cb Callback when prompt is done |
| 31 | + */ |
| 32 | + _run(cb: any): this { |
| 33 | + this.done = cb; |
| 34 | + |
| 35 | + // Once user confirm (enter key) |
| 36 | + const events = observe(this.rl); |
| 37 | + const submit = events.line.pipe(map(this.filterInput.bind(this))); |
| 38 | + |
| 39 | + const validation = this.handleSubmitEvents(submit); |
| 40 | + validation.success.forEach(this.onEnd.bind(this)); |
| 41 | + validation.error.forEach(this.onError.bind(this)); |
| 42 | + |
| 43 | + events.keypress.pipe(takeUntil(validation.success)).forEach(this.onKeypress.bind(this)); |
| 44 | + |
| 45 | + // Init the prompt |
| 46 | + this.render(); |
| 47 | + |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @description: render screen |
| 53 | + */ |
| 54 | + render(error?: string) { |
| 55 | + let bottomContent = ""; |
| 56 | + let appendContent = ""; |
| 57 | + let message = this.getQuestion(); |
| 58 | + const { transformer, completeValue } = this.opt as unknown as CompletePromptQuestionOptions; |
| 59 | + const isFinal = this.status === "answered"; |
| 60 | + |
| 61 | + if (isFinal) { |
| 62 | + appendContent = this.answer || ""; |
| 63 | + } else { |
| 64 | + appendContent = this.rl.line; |
| 65 | + } |
| 66 | + |
| 67 | + if (transformer) { |
| 68 | + message += transformer(appendContent, this.answers, { isFinal }); |
| 69 | + } else { |
| 70 | + message += isFinal ? style.cyan(appendContent) : appendContent; |
| 71 | + } |
| 72 | + if (!this.state && !isFinal && completeValue) { |
| 73 | + message += style.gray(completeValue); |
| 74 | + bottomContent = style.gray("Press <tab>|<right> complete;Press <Enter> submit value"); |
| 75 | + } |
| 76 | + |
| 77 | + if (error) { |
| 78 | + bottomContent = style.red(">> ") + error; |
| 79 | + } |
| 80 | + |
| 81 | + this.screen.render(message, bottomContent); |
| 82 | + } |
| 83 | + |
| 84 | + filterInput(input?: string) { |
| 85 | + if (!input) { |
| 86 | + return this.completeValue || ""; |
| 87 | + } |
| 88 | + return input; |
| 89 | + } |
| 90 | + |
| 91 | + onEnd(state: { value?: string }) { |
| 92 | + this.answer = state.value; |
| 93 | + this.status = "answered"; |
| 94 | + |
| 95 | + // Re-render prompt |
| 96 | + this.render(); |
| 97 | + |
| 98 | + this.screen.done(); |
| 99 | + this.done(state.value); |
| 100 | + } |
| 101 | + |
| 102 | + onError({ value = "", isValid }: any) { |
| 103 | + // @ts-ignore |
| 104 | + this.rl.line += value; |
| 105 | + // @ts-ignore |
| 106 | + this.rl.cursor += value.length; |
| 107 | + this.render(isValid); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @description: Search <any key> |
| 112 | + */ |
| 113 | + onKeypress(e: { key: { name?: string; ctrl?: boolean }; value: string }) { |
| 114 | + if (!this.state && (e.key.name === "tab" || e.key.name === "right")) { |
| 115 | + this.rl.write(ansiEscapes.cursorLeft); |
| 116 | + this.rl.write(ansiEscapes.cursorForward(this.completeValue?.length)); |
| 117 | + // @ts-ignore |
| 118 | + this.rl.line = this.completeValue; |
| 119 | + // @ts-ignore |
| 120 | + this.rl.cursor = this.completeValue?.length; |
| 121 | + } |
| 122 | + this.state = "touched"; |
| 123 | + |
| 124 | + this.render(); |
| 125 | + } |
| 126 | +} |
0 commit comments