|
| 1 | +import type { Key } from 'node:readline'; |
| 2 | +import color from 'picocolors'; |
| 3 | +import Prompt, { type PromptOptions } from './prompt.js'; |
| 4 | + |
| 5 | +interface OptionLike { |
| 6 | + value: unknown; |
| 7 | + label?: string; |
| 8 | +} |
| 9 | + |
| 10 | +type FilterFunction<T extends OptionLike> = (search: string, opt: T) => boolean; |
| 11 | + |
| 12 | +function getCursorForValue<T extends OptionLike>( |
| 13 | + selected: T['value'] | undefined, |
| 14 | + items: T[] |
| 15 | +): number { |
| 16 | + if (selected === undefined) { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + |
| 20 | + const currLength = items.length; |
| 21 | + |
| 22 | + // If filtering changed the available options, update cursor |
| 23 | + if (currLength === 0) { |
| 24 | + return 0; |
| 25 | + } |
| 26 | + |
| 27 | + // Try to maintain the same selected item |
| 28 | + const index = items.findIndex((item) => item.value === selected); |
| 29 | + return index !== -1 ? index : 0; |
| 30 | +} |
| 31 | + |
| 32 | +function defaultFilter<T extends OptionLike>(input: string, option: T): boolean { |
| 33 | + const label = option.label ?? String(option.value); |
| 34 | + return label.toLowerCase().includes(input.toLowerCase()); |
| 35 | +} |
| 36 | + |
| 37 | +function normalisedValue<T>(multiple: boolean, values: T[] | undefined): T | T[] | undefined { |
| 38 | + if (!values) { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + if (multiple) { |
| 42 | + return values; |
| 43 | + } |
| 44 | + return values[0]; |
| 45 | +} |
| 46 | + |
| 47 | +export interface AutocompleteOptions<T extends OptionLike> |
| 48 | + extends PromptOptions<AutocompletePrompt<T>> { |
| 49 | + options: T[]; |
| 50 | + filter?: FilterFunction<T>; |
| 51 | + multiple?: boolean; |
| 52 | +} |
| 53 | + |
| 54 | +export default class AutocompletePrompt<T extends OptionLike> extends Prompt { |
| 55 | + options: T[]; |
| 56 | + filteredOptions: T[]; |
| 57 | + multiple: boolean; |
| 58 | + isNavigating = false; |
| 59 | + selectedValues: Array<T['value']> = []; |
| 60 | + |
| 61 | + focusedValue: T['value'] | undefined; |
| 62 | + #cursor = 0; |
| 63 | + #lastValue: T['value'] | undefined; |
| 64 | + #filterFn: FilterFunction<T>; |
| 65 | + |
| 66 | + get cursor(): number { |
| 67 | + return this.#cursor; |
| 68 | + } |
| 69 | + |
| 70 | + get valueWithCursor() { |
| 71 | + if (!this.value) { |
| 72 | + return color.inverse(color.hidden('_')); |
| 73 | + } |
| 74 | + if (this._cursor >= this.value.length) { |
| 75 | + return `${this.value}█`; |
| 76 | + } |
| 77 | + const s1 = this.value.slice(0, this._cursor); |
| 78 | + const [s2, ...s3] = this.value.slice(this._cursor); |
| 79 | + return `${s1}${color.inverse(s2)}${s3.join('')}`; |
| 80 | + } |
| 81 | + |
| 82 | + constructor(opts: AutocompleteOptions<T>) { |
| 83 | + super(opts); |
| 84 | + |
| 85 | + this.options = opts.options; |
| 86 | + this.filteredOptions = [...this.options]; |
| 87 | + this.multiple = opts.multiple === true; |
| 88 | + this._usePlaceholderAsValue = false; |
| 89 | + this.#filterFn = opts.filter ?? defaultFilter; |
| 90 | + let initialValues: unknown[] | undefined; |
| 91 | + if (opts.initialValue && Array.isArray(opts.initialValue)) { |
| 92 | + if (this.multiple) { |
| 93 | + initialValues = opts.initialValue; |
| 94 | + } else { |
| 95 | + initialValues = opts.initialValue.slice(0, 1); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (initialValues) { |
| 100 | + this.selectedValues = initialValues; |
| 101 | + for (const selectedValue of initialValues) { |
| 102 | + const selectedIndex = this.options.findIndex((opt) => opt.value === selectedValue); |
| 103 | + if (selectedIndex !== -1) { |
| 104 | + this.toggleSelected(selectedValue); |
| 105 | + this.#cursor = selectedIndex; |
| 106 | + this.focusedValue = this.options[this.#cursor]?.value; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + this.on('finalize', () => { |
| 112 | + if (!this.value) { |
| 113 | + this.value = normalisedValue(this.multiple, initialValues); |
| 114 | + } |
| 115 | + |
| 116 | + if (this.state === 'submit') { |
| 117 | + this.value = normalisedValue(this.multiple, this.selectedValues); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + this.on('key', (char, key) => this.#onKey(char, key)); |
| 122 | + this.on('value', (value) => this.#onValueChanged(value)); |
| 123 | + } |
| 124 | + |
| 125 | + protected override _isActionKey(char: string | undefined, key: Key): boolean { |
| 126 | + return ( |
| 127 | + char === '\t' || |
| 128 | + (this.multiple && |
| 129 | + this.isNavigating && |
| 130 | + key.name === 'space' && |
| 131 | + char !== undefined && |
| 132 | + char !== '') |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + #onKey(_char: string | undefined, key: Key): void { |
| 137 | + const isUpKey = key.name === 'up'; |
| 138 | + const isDownKey = key.name === 'down'; |
| 139 | + |
| 140 | + // Start navigation mode with up/down arrows |
| 141 | + if (isUpKey || isDownKey) { |
| 142 | + this.#cursor = Math.max( |
| 143 | + 0, |
| 144 | + Math.min(this.#cursor + (isUpKey ? -1 : 1), this.filteredOptions.length - 1) |
| 145 | + ); |
| 146 | + this.focusedValue = this.filteredOptions[this.#cursor]?.value; |
| 147 | + if (!this.multiple) { |
| 148 | + this.selectedValues = [this.focusedValue]; |
| 149 | + } |
| 150 | + this.isNavigating = true; |
| 151 | + } else { |
| 152 | + if ( |
| 153 | + this.multiple && |
| 154 | + this.focusedValue !== undefined && |
| 155 | + (key.name === 'tab' || (this.isNavigating && key.name === 'space')) |
| 156 | + ) { |
| 157 | + this.toggleSelected(this.focusedValue); |
| 158 | + } else { |
| 159 | + this.isNavigating = false; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + toggleSelected(value: T['value']) { |
| 165 | + if (this.filteredOptions.length === 0) { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if (this.multiple) { |
| 170 | + if (this.selectedValues.includes(value)) { |
| 171 | + this.selectedValues = this.selectedValues.filter((v) => v !== value); |
| 172 | + } else { |
| 173 | + this.selectedValues = [...this.selectedValues, value]; |
| 174 | + } |
| 175 | + } else { |
| 176 | + this.selectedValues = [value]; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + #onValueChanged(value: string | undefined): void { |
| 181 | + if (value !== this.#lastValue) { |
| 182 | + this.#lastValue = value; |
| 183 | + |
| 184 | + if (value) { |
| 185 | + this.filteredOptions = this.options.filter((opt) => this.#filterFn(value, opt)); |
| 186 | + } else { |
| 187 | + this.filteredOptions = [...this.options]; |
| 188 | + } |
| 189 | + this.#cursor = getCursorForValue(this.focusedValue, this.filteredOptions); |
| 190 | + this.focusedValue = this.filteredOptions[this.#cursor]?.value; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments