|
| 1 | +import type { Key } from 'node:readline'; |
| 2 | +import { styleText } from 'node:util'; |
| 3 | +import { findTextCursor } from '../utils/cursor.js'; |
| 4 | +import { type Action, settings } from '../utils/index.js'; |
| 5 | +import Prompt, { type PromptOptions } from './prompt.js'; |
| 6 | + |
| 7 | +export interface MultiLineOptions extends PromptOptions<string, MultiLinePrompt> { |
| 8 | + placeholder?: string; |
| 9 | + defaultValue?: string; |
| 10 | + showSubmit?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export default class MultiLinePrompt extends Prompt<string> { |
| 14 | + #lastKeyWasReturn = false; |
| 15 | + #showSubmit: boolean; |
| 16 | + public focused: 'editor' | 'submit' = 'editor'; |
| 17 | + |
| 18 | + get userInputWithCursor() { |
| 19 | + if (this.state === 'submit') { |
| 20 | + return this.userInput; |
| 21 | + } |
| 22 | + const userInput = this.userInput; |
| 23 | + if (this.cursor >= userInput.length) { |
| 24 | + return `${userInput}█`; |
| 25 | + } |
| 26 | + const s1 = userInput.slice(0, this.cursor); |
| 27 | + const s2 = userInput[this.cursor]; |
| 28 | + const s3 = userInput.slice(this.cursor + 1); |
| 29 | + if (s2 === '\n') return `${s1}█\n${s3}`; |
| 30 | + return `${s1}${styleText('inverse', s2)}${s3}`; |
| 31 | + } |
| 32 | + get cursor() { |
| 33 | + return this._cursor; |
| 34 | + } |
| 35 | + #insertAtCursor(char: string) { |
| 36 | + if (this.userInput.length === 0) { |
| 37 | + this._setUserInput(char); |
| 38 | + return; |
| 39 | + } |
| 40 | + this._setUserInput( |
| 41 | + this.userInput.slice(0, this.cursor) + char + this.userInput.slice(this.cursor) |
| 42 | + ); |
| 43 | + } |
| 44 | + #handleCursor(key?: Action) { |
| 45 | + const text = this.value ?? ''; |
| 46 | + switch (key) { |
| 47 | + case 'up': |
| 48 | + this._cursor = findTextCursor(this._cursor, 0, -1, text); |
| 49 | + return; |
| 50 | + case 'down': |
| 51 | + this._cursor = findTextCursor(this._cursor, 0, 1, text); |
| 52 | + return; |
| 53 | + case 'left': |
| 54 | + this._cursor = findTextCursor(this._cursor, -1, 0, text); |
| 55 | + return; |
| 56 | + case 'right': |
| 57 | + this._cursor = findTextCursor(this._cursor, 1, 0, text); |
| 58 | + return; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected override _shouldSubmit(_char: string | undefined, _key: Key): boolean { |
| 63 | + if (this.#showSubmit) { |
| 64 | + if (this.focused === 'submit') { |
| 65 | + return true; |
| 66 | + } |
| 67 | + this.#insertAtCursor('\n'); |
| 68 | + this._cursor++; |
| 69 | + return false; |
| 70 | + } |
| 71 | + const wasReturn = this.#lastKeyWasReturn; |
| 72 | + this.#lastKeyWasReturn = true; |
| 73 | + if (wasReturn) { |
| 74 | + if (this.userInput[this.cursor - 1] === '\n') { |
| 75 | + this._setUserInput( |
| 76 | + this.userInput.slice(0, this.cursor - 1) + this.userInput.slice(this.cursor) |
| 77 | + ); |
| 78 | + this._cursor--; |
| 79 | + } |
| 80 | + return true; |
| 81 | + } |
| 82 | + this.#insertAtCursor('\n'); |
| 83 | + this._cursor++; |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + constructor(opts: MultiLineOptions) { |
| 88 | + super(opts, false); |
| 89 | + this.#showSubmit = opts.showSubmit ?? false; |
| 90 | + |
| 91 | + this.on('key', (char, key) => { |
| 92 | + if (key?.name && settings.actions.has(key.name as Action)) { |
| 93 | + this.#handleCursor(key.name as Action); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (char === '\t' && this.#showSubmit) { |
| 97 | + this.focused = this.focused === 'editor' ? 'submit' : 'editor'; |
| 98 | + return; |
| 99 | + } |
| 100 | + if (key?.name === 'return') { |
| 101 | + return; |
| 102 | + } |
| 103 | + this.#lastKeyWasReturn = false; |
| 104 | + if (key?.name === 'backspace' && this.cursor > 0) { |
| 105 | + this._setUserInput( |
| 106 | + this.userInput.slice(0, this.cursor - 1) + this.userInput.slice(this.cursor) |
| 107 | + ); |
| 108 | + this._cursor--; |
| 109 | + return; |
| 110 | + } |
| 111 | + if (key?.name === 'delete' && this.cursor < this.userInput.length) { |
| 112 | + this._setUserInput( |
| 113 | + this.userInput.slice(0, this.cursor) + this.userInput.slice(this.cursor + 1) |
| 114 | + ); |
| 115 | + return; |
| 116 | + } |
| 117 | + if (char) { |
| 118 | + if (this.#showSubmit && this.focused === 'submit') { |
| 119 | + this.focused = 'editor'; |
| 120 | + } |
| 121 | + this.#insertAtCursor(char ?? ''); |
| 122 | + this._cursor++; |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + this.on('userInput', (input) => { |
| 127 | + this._setValue(input); |
| 128 | + }); |
| 129 | + this.on('finalize', () => { |
| 130 | + if (!this.value) { |
| 131 | + this.value = opts.defaultValue; |
| 132 | + } |
| 133 | + if (this.value === undefined) { |
| 134 | + this.value = ''; |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
0 commit comments