Skip to content

Commit 0e4ddc9

Browse files
kaigritunKai Gritun
andauthored
fix: respect withGuide option in password and path prompts (#460)
Co-authored-by: Kai Gritun <kai@kaigritun.com>
1 parent de1fc4c commit 0e4ddc9

File tree

6 files changed

+111
-98
lines changed

6 files changed

+111
-98
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": patch
3+
---
4+
5+
Fix `withGuide` option being ignored in password and path prompts

‎packages/prompts/src/autocomplete.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AutocompletePrompt } from '@clack/core';
1+
import { AutocompletePrompt, settings } from '@clack/core';
22
import color from 'picocolors';
33
import {
44
type CommonOptions,
@@ -95,8 +95,11 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
9595
output: opts.output,
9696
validate: opts.validate,
9797
render() {
98+
const hasGuide = opts.withGuide ?? settings.withGuide;
9899
// Title and message display
99-
const headings = [`${color.gray(S_BAR)}`, `${symbol(this.state)} ${opts.message}`];
100+
const headings = hasGuide
101+
? [`${color.gray(S_BAR)}`, `${symbol(this.state)} ${opts.message}`]
102+
: [`${symbol(this.state)} ${opts.message}`];
100103
const userInput = this.userInput;
101104
const options = this.options;
102105
const placeholder = opts.placeholder;
@@ -109,17 +112,20 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
109112
const selected = getSelectedOptions(this.selectedValues, options);
110113
const label =
111114
selected.length > 0 ? ` ${color.dim(selected.map(getLabel).join(', '))}` : '';
112-
return `${headings.join('\n')}\n${color.gray(S_BAR)}${label}`;
115+
const submitPrefix = hasGuide ? color.gray(S_BAR) : '';
116+
return `${headings.join('\n')}\n${submitPrefix}${label}`;
113117
}
114118

115119
case 'cancel': {
116120
const userInputText = userInput ? ` ${color.strikethrough(color.dim(userInput))}` : '';
117-
return `${headings.join('\n')}\n${color.gray(S_BAR)}${userInputText}`;
121+
const cancelPrefix = hasGuide ? color.gray(S_BAR) : '';
122+
return `${headings.join('\n')}\n${cancelPrefix}${userInputText}`;
118123
}
119124

120125
default: {
121-
const guidePrefix = `${(this.state === 'error' ? color.yellow : color.cyan)(S_BAR)} `;
122-
const guidePrefixEnd = (this.state === 'error' ? color.yellow : color.cyan)(S_BAR_END);
126+
const barColor = this.state === 'error' ? color.yellow : color.cyan;
127+
const guidePrefix = hasGuide ? `${barColor(S_BAR)} ` : '';
128+
const guidePrefixEnd = hasGuide ? barColor(S_BAR_END) : '';
123129
// Display cursor position - show plain text in navigation mode
124130
let searchText = '';
125131
if (this.isNavigating || showPlaceholder) {
@@ -146,8 +152,10 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
146152
const validationError =
147153
this.state === 'error' ? [`${guidePrefix}${color.yellow(this.error)}`] : [];
148154

155+
if (hasGuide) {
156+
headings.push(`${guidePrefix.trimEnd()}`);
157+
}
149158
headings.push(
150-
`${guidePrefix.trimEnd()}`,
151159
`${guidePrefix}${color.dim('Search:')}${searchText}${matches}`,
152160
...noResults,
153161
...validationError
@@ -161,8 +169,8 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
161169
];
162170

163171
const footers = [
164-
`${guidePrefix}${color.dim(instructions.join(' • '))}`,
165-
`${guidePrefixEnd}`,
172+
`${guidePrefix}${instructions.join(' • ')}`,
173+
guidePrefixEnd,
166174
];
167175

168176
// Render options with selection
@@ -172,7 +180,7 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
172180
: limitOptions({
173181
cursor: this.cursor,
174182
options: this.filteredOptions,
175-
columnPadding: 3, // for `| `
183+
columnPadding: hasGuide ? 3 : 0, // for `| ` when guide is shown
176184
rowPadding: headings.length + footers.length,
177185
style: (option, active) => {
178186
const label = getLabel(option);
@@ -318,7 +326,7 @@ export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOpti
318326
...errorMessage,
319327
];
320328
const footerLines = [
321-
`${barColor(S_BAR)} ${color.dim(instructions.join(' • '))}`,
329+
`${barColor(S_BAR)} ${instructions.join(' • ')}`,
322330
`${barColor(S_BAR_END)}`,
323331
];
324332

‎packages/prompts/src/password.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ export const password = (opts: PasswordOptions) => {
2323

2424
switch (this.state) {
2525
case 'error': {
26-
const maskedText = masked ? ` ${masked}` : '';
26+
const errorPrefix = hasGuide ? `${color.yellow(S_BAR)} ` : '';
27+
const errorPrefixEnd = hasGuide ? `${color.yellow(S_BAR_END)} ` : '';
28+
const maskedText = masked ?? '';
2729
if (opts.clearOnError) {
2830
this.clear();
2931
}
30-
const errorPrefix = hasGuide ? `${color.yellow(S_BAR)}` : '';
31-
const errorPrefixEnd = hasGuide ? color.yellow(S_BAR_END) : '';
32-
return `${title.trim()}\n${errorPrefix}${maskedText}\n${errorPrefixEnd} ${color.yellow(this.error)}\n`;
32+
return `${title.trim()}\n${errorPrefix}${maskedText}\n${errorPrefixEnd}${color.yellow(this.error)}\n`;
3333
}
3434
case 'submit': {
35-
const maskedText = masked ? ` ${color.dim(masked)}` : '';
36-
const submitPrefix = hasGuide ? color.gray(S_BAR) : '';
35+
const submitPrefix = hasGuide ? `${color.gray(S_BAR)} ` : '';
36+
const maskedText = masked ? color.dim(masked) : '';
3737
return `${title}${submitPrefix}${maskedText}`;
3838
}
3939
case 'cancel': {
40-
const maskedText = masked ? ` ${color.strikethrough(color.dim(masked))}` : '';
41-
const cancelPrefix = hasGuide ? color.gray(S_BAR) : '';
40+
const cancelPrefix = hasGuide ? `${color.gray(S_BAR)} ` : '';
41+
const maskedText = masked ? color.strikethrough(color.dim(masked)) : '';
4242
return `${title}${cancelPrefix}${maskedText}${
4343
masked && hasGuide ? `\n${color.gray(S_BAR)}` : ''
4444
}`;

0 commit comments

Comments
 (0)