| 61 | } |
| 62 | |
| 63 | export default class AutocompletePrompt<T extends OptionLike> extends Prompt< |
| 64 | T['value'] | T['value'][] |
| 65 | > { |
| 66 | filteredOptions: T[]; |
| 67 | multiple: boolean; |
| 68 | isNavigating = false; |
| 69 | selectedValues: Array<T['value']> = []; |
| 70 | |
| 71 | focusedValue: T['value'] | undefined; |
| 72 | #cursor = 0; |
| 73 | #lastUserInput = ''; |
| 74 | #filterFn: FilterFunction<T> | undefined; |
| 75 | #options: T[] | (() => T[]); |
| 76 | #placeholder: string | undefined; |
| 77 | |
| 78 | get cursor(): number { |
| 79 | return this.#cursor; |
| 80 | } |
| 81 | |
| 82 | get userInputWithCursor() { |
| 83 | if (!this.userInput) { |
| 84 | return styleText(['inverse', 'hidden'], '_'); |
| 85 | } |
| 86 | if (this._cursor >= this.userInput.length) { |
| 87 | return `${this.userInput}█`; |
| 88 | } |
| 89 | const s1 = this.userInput.slice(0, this._cursor); |
| 90 | const [s2, ...s3] = this.userInput.slice(this._cursor); |
| 91 | return `${s1}${styleText('inverse', s2)}${s3.join('')}`; |
| 92 | } |
| 93 | |
| 94 | get options(): T[] { |
| 95 | if (typeof this.#options === 'function') { |
| 96 | return this.#options(); |
| 97 | } |
| 98 | return this.#options; |
| 99 | } |
| 100 | |
| 101 | constructor(opts: AutocompleteOptions<T>) { |
| 102 | super(opts); |
| 103 | |
| 104 | this.#options = opts.options; |
| 105 | this.#placeholder = opts.placeholder; |
| 106 | const options = this.options; |
| 107 | this.filteredOptions = [...options]; |
| 108 | this.multiple = opts.multiple === true; |
| 109 | this.#filterFn = |
| 110 | typeof opts.options === 'function' ? opts.filter : (opts.filter ?? defaultFilter); |
| 111 | let initialValues: unknown[] | undefined; |
| 112 | if (opts.initialValue && Array.isArray(opts.initialValue)) { |
| 113 | if (this.multiple) { |
| 114 | initialValues = opts.initialValue; |
| 115 | } else { |
| 116 | initialValues = opts.initialValue.slice(0, 1); |
| 117 | } |
| 118 | } else { |
| 119 | if (!this.multiple && this.options.length > 0) { |
| 120 | initialValues = [this.options[0].value]; |
nothing calls this directly
no outgoing calls
no test coverage detected