Skip to content

Commit 775dacb

Browse files
committed
chore: rework to compute prefix once
1 parent 56b0adc commit 775dacb

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

packages/prompts/src/autocomplete.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
112112
}
113113

114114
default: {
115-
const barColor = this.state === "error" ? color.yellow : color.cyan;
115+
const guidePrefix = `${(this.state === 'error' ? color.yellow : color.cyan)(S_BAR)} `;
116+
const guidePrefixEnd = (this.state === 'error' ? color.yellow : color.cyan)(S_BAR_END);
116117
// Display cursor position - show plain text in navigation mode
117118
let searchText = '';
118119
if (this.isNavigating || showPlaceholder) {
@@ -133,15 +134,15 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
133134
// No matches message
134135
const noResults =
135136
this.filteredOptions.length === 0 && userInput
136-
? [`${barColor(S_BAR)} ${color.yellow('No matches found')}`]
137+
? [`${guidePrefix}${color.yellow('No matches found')}`]
137138
: [];
138139

139140
const validationError =
140-
this.state === 'error' ? [`${barColor(S_BAR)} ${color.yellow(this.error)}`] : [];
141+
this.state === 'error' ? [`${guidePrefix}${color.yellow(this.error)}`] : [];
141142

142143
headings.push(
143-
`${barColor(S_BAR)}`,
144-
`${barColor(S_BAR)} ${color.dim('Search:')}${searchText}${matches}`,
144+
`${guidePrefix.trimEnd()}`,
145+
`${guidePrefix}${color.dim('Search:')}${searchText}${matches}`,
145146
...noResults,
146147
...validationError
147148
);
@@ -154,8 +155,8 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
154155
];
155156

156157
const footers = [
157-
`${barColor(S_BAR)} ${color.dim(instructions.join(' • '))}`,
158-
`${barColor(S_BAR_END)}`,
158+
`${guidePrefix}${color.dim(instructions.join(' • '))}`,
159+
`${guidePrefixEnd}`,
159160
];
160161

161162
// Render options with selection
@@ -185,7 +186,7 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
185186
// Return the formatted prompt
186187
return [
187188
...headings,
188-
...displayOptions.map((option) => `${barColor(S_BAR)} ${option}`),
189+
...displayOptions.map((option) => `${guidePrefix}${option}`),
189190
...footers,
190191
].join('\n');
191192
}
@@ -283,7 +284,7 @@ export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOpti
283284
return `${title}${color.gray(S_BAR)} ${color.strikethrough(color.dim(userInput))}`;
284285
}
285286
default: {
286-
const barColor = this.state === "error" ? color.yellow : color.cyan;
287+
const barColor = this.state === 'error' ? color.yellow : color.cyan;
287288
// Instructions
288289
const instructions = [
289290
`${color.dim('↑/↓')} to navigate`,

0 commit comments

Comments
 (0)