|
| 1 | +import color from 'picocolors'; |
| 2 | +import { type Action, settings } from '../utils/index.js'; |
| 3 | +import Prompt from './prompt.js'; |
| 4 | +import type { TextOptions } from './text.js'; |
| 5 | + |
| 6 | +export default class MultiLinePrompt extends Prompt { |
| 7 | + get valueWithCursor() { |
| 8 | + if (this.state === 'submit') { |
| 9 | + return this.value; |
| 10 | + } |
| 11 | + if (this.cursor >= this.value.length) { |
| 12 | + return `${this.value}█`; |
| 13 | + } |
| 14 | + const s1 = this.value.slice(0, this.cursor); |
| 15 | + const [s2, ...s3] = this.value.slice(this.cursor); |
| 16 | + return `${s1}${color.inverse(s2)}${s3.join('')}`; |
| 17 | + } |
| 18 | + get cursor() { |
| 19 | + return this._cursor; |
| 20 | + } |
| 21 | + insertAtCursor(char: string) { |
| 22 | + if (!this.value || this.value.length === 0) { |
| 23 | + this.value = char; |
| 24 | + return; |
| 25 | + } |
| 26 | + this.value = this.value.substr(0, this.cursor) + char + this.value.substr(this.cursor); |
| 27 | + } |
| 28 | + handleCursor(key?: Action) { |
| 29 | + const text = this.value ?? ''; |
| 30 | + const lines = text.split('\n'); |
| 31 | + const beforeCursor = text.substr(0, this.cursor); |
| 32 | + const currentLine = beforeCursor.split('\n').length - 1; |
| 33 | + const lineStart = beforeCursor.lastIndexOf('\n'); |
| 34 | + const cursorOffet = this.cursor - lineStart; |
| 35 | + switch (key) { |
| 36 | + case 'up': |
| 37 | + if (currentLine === 0) { |
| 38 | + this._cursor = 0; |
| 39 | + return; |
| 40 | + } |
| 41 | + this._cursor += |
| 42 | + -cursorOffet - |
| 43 | + lines[currentLine - 1].length + |
| 44 | + Math.min(lines[currentLine - 1].length, cursorOffet - 1); |
| 45 | + return; |
| 46 | + case 'down': |
| 47 | + if (currentLine === lines.length - 1) { |
| 48 | + this._cursor = text.length; |
| 49 | + return; |
| 50 | + } |
| 51 | + this._cursor += |
| 52 | + -cursorOffet + |
| 53 | + 1 + |
| 54 | + lines[currentLine].length + |
| 55 | + Math.min(lines[currentLine + 1].length + 1, cursorOffet); |
| 56 | + return; |
| 57 | + case 'left': |
| 58 | + this._cursor = Math.max(0, this._cursor - 1); |
| 59 | + return; |
| 60 | + case 'right': |
| 61 | + this._cursor = Math.min(text.length, this._cursor + 1); |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + constructor(opts: TextOptions) { |
| 66 | + super(opts, false); |
| 67 | + |
| 68 | + this.on('rawKey', (char, key) => { |
| 69 | + if (settings.actions.has(key?.name)) { |
| 70 | + this.handleCursor(key?.name); |
| 71 | + } |
| 72 | + if (char === '\r') { |
| 73 | + this.insertAtCursor('\n'); |
| 74 | + this._cursor++; |
| 75 | + return; |
| 76 | + } |
| 77 | + if (char === '\u0004') { |
| 78 | + return; |
| 79 | + } |
| 80 | + if (key?.name === 'backspace' && this.cursor > 0) { |
| 81 | + this.value = this.value.substr(0, this.cursor - 1) + this.value.substr(this.cursor); |
| 82 | + this._cursor--; |
| 83 | + return; |
| 84 | + } |
| 85 | + if (key?.name === 'delete' && this.cursor < this.value.length) { |
| 86 | + this.value = this.value.substr(0, this.cursor) + this.value.substr(this.cursor + 1); |
| 87 | + return; |
| 88 | + } |
| 89 | + if (char) { |
| 90 | + this.insertAtCursor(char ?? ''); |
| 91 | + this._cursor++; |
| 92 | + } |
| 93 | + }); |
| 94 | + this.on('finalize', () => { |
| 95 | + if (!this.value) { |
| 96 | + this.value = opts.defaultValue; |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
0 commit comments