|
| 1 | +import color from 'picocolors'; |
| 2 | +import type { ValueWithCursorPart } from '../types.js'; |
| 3 | +import Prompt, { type PromptOptions } from './prompt.js'; |
| 4 | + |
| 5 | +interface SuggestionOptions extends PromptOptions<SuggestionPrompt> { |
| 6 | + suggest: (value: string) => Array<string>; |
| 7 | + initialValue: string; |
| 8 | +} |
| 9 | + |
| 10 | +export default class SuggestionPrompt extends Prompt { |
| 11 | + value: string; |
| 12 | + protected suggest: (value: string) => Array<string>; |
| 13 | + private selectionIndex = 0; |
| 14 | + private nextItems: Array<string> = []; |
| 15 | + |
| 16 | + constructor(opts: SuggestionOptions) { |
| 17 | + super(opts); |
| 18 | + |
| 19 | + this.value = opts.initialValue; |
| 20 | + this.suggest = opts.suggest; |
| 21 | + this.getNextItems(); |
| 22 | + this.selectionIndex = 0; |
| 23 | + this._cursor = this.value.length; |
| 24 | + |
| 25 | + this.on('cursor', (key) => { |
| 26 | + switch (key) { |
| 27 | + case 'up': |
| 28 | + this.selectionIndex = Math.max( |
| 29 | + 0, |
| 30 | + this.selectionIndex === 0 ? this.nextItems.length - 1 : this.selectionIndex - 1 |
| 31 | + ); |
| 32 | + break; |
| 33 | + case 'down': |
| 34 | + this.selectionIndex = |
| 35 | + this.nextItems.length === 0 ? 0 : (this.selectionIndex + 1) % this.nextItems.length; |
| 36 | + break; |
| 37 | + } |
| 38 | + }); |
| 39 | + this.on('key', (key, info) => { |
| 40 | + if (info.name === 'tab' && this.nextItems.length > 0) { |
| 41 | + const delta = this.nextItems[this.selectionIndex].substring(this.value.length); |
| 42 | + this.value = this.nextItems[this.selectionIndex]; |
| 43 | + this.rl?.write(delta); |
| 44 | + this._cursor = this.value.length; |
| 45 | + this.selectionIndex = 0; |
| 46 | + this.getNextItems(); |
| 47 | + } |
| 48 | + }); |
| 49 | + this.on('value', () => { |
| 50 | + this.getNextItems(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + get displayValue(): Array<ValueWithCursorPart> { |
| 55 | + const result: Array<ValueWithCursorPart> = []; |
| 56 | + if (this._cursor > 0) { |
| 57 | + result.push({ |
| 58 | + text: this.value.substring(0, this._cursor), |
| 59 | + type: 'value', |
| 60 | + }); |
| 61 | + } |
| 62 | + if (this._cursor < this.value.length) { |
| 63 | + result.push({ |
| 64 | + text: this.value.substring(this._cursor, this._cursor + 1), |
| 65 | + type: 'cursor_on_value', |
| 66 | + }); |
| 67 | + const left = this.value.substring(this._cursor + 1); |
| 68 | + if (left.length > 0) { |
| 69 | + result.push({ |
| 70 | + text: left, |
| 71 | + type: 'value', |
| 72 | + }); |
| 73 | + } |
| 74 | + if (this.suggestion.length > 0) { |
| 75 | + result.push({ |
| 76 | + text: this.suggestion, |
| 77 | + type: 'suggestion', |
| 78 | + }); |
| 79 | + } |
| 80 | + return result; |
| 81 | + } |
| 82 | + if (this.suggestion.length === 0) { |
| 83 | + result.push({ |
| 84 | + text: '\u00A0', |
| 85 | + type: 'cursor_on_value', |
| 86 | + }); |
| 87 | + return result; |
| 88 | + } |
| 89 | + result.push( |
| 90 | + { |
| 91 | + text: this.suggestion[0], |
| 92 | + type: 'cursor_on_suggestion', |
| 93 | + }, |
| 94 | + { |
| 95 | + text: this.suggestion.substring(1), |
| 96 | + type: 'suggestion', |
| 97 | + } |
| 98 | + ); |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + get suggestion(): string { |
| 103 | + return this.nextItems[this.selectionIndex]?.substring(this.value.length) ?? ''; |
| 104 | + } |
| 105 | + |
| 106 | + private getNextItems(): void { |
| 107 | + this.nextItems = this.suggest(this.value).filter((item) => { |
| 108 | + return item.startsWith(this.value) && item !== this.value; |
| 109 | + }); |
| 110 | + if (this.selectionIndex > this.nextItems.length) { |
| 111 | + this.selectionIndex = 0; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments