MCPcopy Create free account
hub / github.com/bombshell-dev/clack / autocomplete

Function autocomplete

packages/prompts/src/autocomplete.ts:123–264  ·  view source on GitHub ↗
(opts: AutocompleteOptions<Value>)

Source from the content-addressed store, hash-verified

121 * ```
122 */
123export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
124 const prompt = new AutocompletePrompt({
125 options: opts.options,
126 initialValue: opts.initialValue ? [opts.initialValue] : undefined,
127 initialUserInput: opts.initialUserInput,
128 placeholder: opts.placeholder,
129 filter:
130 opts.filter ??
131 ((search: string, opt: Option<Value>) => {
132 return getFilteredOption(search, opt);
133 }),
134 signal: opts.signal,
135 input: opts.input,
136 output: opts.output,
137 validate: opts.validate,
138 render() {
139 const hasGuide = opts.withGuide ?? settings.withGuide;
140 // Title and message display
141 const headings = hasGuide
142 ? [`${styleText('gray', S_BAR)}`, `${symbol(this.state)} ${opts.message}`]
143 : [`${symbol(this.state)} ${opts.message}`];
144 const userInput = this.userInput;
145 const options = this.options;
146 const placeholder = opts.placeholder;
147 const showPlaceholder = userInput === '' && placeholder !== undefined;
148 const opt = (option: Option<Value>, state: 'inactive' | 'active' | 'disabled') => {
149 const label = getLabel(option);
150 const hint =
151 option.hint && option.value === this.focusedValue
152 ? styleText('dim', ` (${option.hint})`)
153 : '';
154 switch (state) {
155 case 'active':
156 return `${styleText('green', S_RADIO_ACTIVE)} ${label}${hint}`;
157 case 'inactive':
158 return `${styleText('dim', S_RADIO_INACTIVE)} ${styleText('dim', label)}`;
159 case 'disabled':
160 return `${styleText('gray', S_RADIO_INACTIVE)} ${styleText(['strikethrough', 'gray'], label)}`;
161 }
162 };
163
164 // Handle different states
165 switch (this.state) {
166 case 'submit': {
167 // Show selected value
168 const selected = getSelectedOptions(this.selectedValues, options);
169 const label =
170 selected.length > 0 ? ` ${styleText('dim', selected.map(getLabel).join(', '))}` : '';
171 const submitPrefix = hasGuide ? styleText('gray', S_BAR) : '';
172 return `${headings.join('\n')}\n${submitPrefix}${label}`;
173 }
174
175 case 'cancel': {
176 const userInputText = userInput
177 ? ` ${styleText(['strikethrough', 'dim'], userInput)}`
178 : '';
179 const cancelPrefix = hasGuide ? styleText('gray', S_BAR) : '';
180 return `${headings.join('\n')}\n${cancelPrefix}${userInputText}`;

Callers 2

pathFunction · 0.85

Calls 2

getFilteredOptionFunction · 0.85
promptMethod · 0.80

Tested by

no test coverage detected