Skip to content

Commit acc4c3a

Browse files
authored
feat: add withGuide option (#409)
1 parent 4ba2d78 commit acc4c3a

File tree

15 files changed

+696
-177
lines changed

15 files changed

+696
-177
lines changed

.changeset/tall-keys-allow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clack/prompts": patch
3+
"@clack/core": patch
4+
---
5+
6+
Add a new `withGuide` option to all prompts to disable the default clack border

packages/core/src/utils/settings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface InternalClackSettings {
99
cancel: string;
1010
error: string;
1111
};
12+
withGuide: boolean;
1213
}
1314

1415
export const settings: InternalClackSettings = {
@@ -27,6 +28,7 @@ export const settings: InternalClackSettings = {
2728
cancel: 'Canceled',
2829
error: 'Something went wrong',
2930
},
31+
withGuide: true,
3032
};
3133

3234
export interface ClackSettings {
@@ -54,6 +56,8 @@ export interface ClackSettings {
5456
*/
5557
error?: string;
5658
};
59+
60+
withGuide?: boolean;
5761
}
5862

5963
export function updateSettings(updates: ClackSettings) {
@@ -81,6 +85,10 @@ export function updateSettings(updates: ClackSettings) {
8185
settings.messages.error = messages.error;
8286
}
8387
}
88+
89+
if (updates.withGuide !== undefined) {
90+
settings.withGuide = updates.withGuide !== false;
91+
}
8492
}
8593

8694
/**

packages/core/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"include": ["src"]
3+
"include": ["src", "test"]
44
}

packages/prompts/src/box.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Writable } from 'node:stream';
2-
import { getColumns } from '@clack/core';
2+
import { getColumns, settings } from '@clack/core';
33
import stringWidth from 'fast-string-width';
44
import { wrapAnsi } from 'fast-wrap-ansi';
55
import {
@@ -35,7 +35,6 @@ export interface BoxOptions extends CommonOptions {
3535
titlePadding?: number;
3636
contentPadding?: number;
3737
rounded?: boolean;
38-
includePrefix?: boolean;
3938
formatBorder?: (text: string) => string;
4039
}
4140

@@ -68,7 +67,8 @@ export const box = (message = '', title = '', opts?: BoxOptions) => {
6867
const titlePadding = opts?.titlePadding ?? 1;
6968
const contentPadding = opts?.contentPadding ?? 2;
7069
const width = opts?.width === undefined || opts.width === 'auto' ? 1 : Math.min(1, opts.width);
71-
const linePrefix = opts?.includePrefix ? `${S_BAR} ` : '';
70+
const hasGuide = (opts?.withGuide ?? settings.withGuide) !== false;
71+
const linePrefix = !hasGuide ? '' : `${S_BAR} `;
7272
const formatBorder = opts?.formatBorder ?? defaultFormatBorder;
7373
const symbols = (opts?.rounded ? roundedSymbols : squareSymbols).map(formatBorder);
7474
const hSymbol = formatBorder(S_BAR_H);

packages/prompts/src/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ export interface CommonOptions {
7171
input?: Readable;
7272
output?: Writable;
7373
signal?: AbortSignal;
74+
withGuide?: boolean;
7475
}

packages/prompts/src/log.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { settings } from '@clack/core';
12
import color from 'picocolors';
23
import {
34
type CommonOptions,
@@ -23,25 +24,32 @@ export const log = {
2324
secondarySymbol = color.gray(S_BAR),
2425
output = process.stdout,
2526
spacing = 1,
27+
withGuide,
2628
}: LogMessageOptions = {}
2729
) => {
2830
const parts: string[] = [];
31+
const hasGuide = (withGuide ?? settings.withGuide) !== false;
32+
const spacingString = !hasGuide ? '' : secondarySymbol;
33+
const prefix = !hasGuide ? '' : `${symbol} `;
34+
const secondaryPrefix = !hasGuide ? '' : `${secondarySymbol} `;
35+
2936
for (let i = 0; i < spacing; i++) {
30-
parts.push(`${secondarySymbol}`);
37+
parts.push(spacingString);
3138
}
39+
3240
const messageParts = Array.isArray(message) ? message : message.split('\n');
3341
if (messageParts.length > 0) {
3442
const [firstLine, ...lines] = messageParts;
3543
if (firstLine.length > 0) {
36-
parts.push(`${symbol} ${firstLine}`);
44+
parts.push(`${prefix}${firstLine}`);
3745
} else {
38-
parts.push(symbol);
46+
parts.push(hasGuide ? '' : symbol);
3947
}
4048
for (const ln of lines) {
4149
if (ln.length > 0) {
42-
parts.push(`${secondarySymbol} ${ln}`);
50+
parts.push(`${secondaryPrefix}${ln}`);
4351
} else {
44-
parts.push(secondarySymbol);
52+
parts.push(hasGuide ? '' : secondarySymbol);
4553
}
4654
}
4755
}

packages/prompts/src/text.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TextPrompt } from '@clack/core';
1+
import { settings, TextPrompt } from '@clack/core';
22
import color from 'picocolors';
33
import { type CommonOptions, S_BAR, S_BAR_END, symbol } from './common.js';
44

@@ -20,7 +20,9 @@ export const text = (opts: TextOptions) => {
2020
signal: opts.signal,
2121
input: opts.input,
2222
render() {
23-
const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`;
23+
const hasGuide = (opts?.withGuide ?? settings.withGuide) !== false;
24+
const titlePrefix = `${hasGuide ? `${color.gray(S_BAR)}\n` : ''}${symbol(this.state)} `;
25+
const title = `${titlePrefix}${opts.message}\n`;
2426
const placeholder = opts.placeholder
2527
? color.inverse(opts.placeholder[0]) + color.dim(opts.placeholder.slice(1))
2628
: color.inverse(color.hidden('_'));
@@ -30,20 +32,25 @@ export const text = (opts: TextOptions) => {
3032
switch (this.state) {
3133
case 'error': {
3234
const errorText = this.error ? ` ${color.yellow(this.error)}` : '';
33-
return `${title.trim()}\n${color.yellow(S_BAR)} ${userInput}\n${color.yellow(
34-
S_BAR_END
35-
)}${errorText}\n`;
35+
const errorPrefix = hasGuide ? `${color.yellow(S_BAR)} ` : '';
36+
const errorPrefixEnd = hasGuide ? color.yellow(S_BAR_END) : '';
37+
return `${title.trim()}\n${errorPrefix}${userInput}\n${errorPrefixEnd}${errorText}\n`;
3638
}
3739
case 'submit': {
3840
const valueText = value ? ` ${color.dim(value)}` : '';
39-
return `${title}${color.gray(S_BAR)}${valueText}`;
41+
const submitPrefix = hasGuide ? color.gray(S_BAR) : '';
42+
return `${title}${submitPrefix}${valueText}`;
4043
}
4144
case 'cancel': {
4245
const valueText = value ? ` ${color.strikethrough(color.dim(value))}` : '';
43-
return `${title}${color.gray(S_BAR)}${valueText}${value.trim() ? `\n${color.gray(S_BAR)}` : ''}`;
46+
const cancelPrefix = hasGuide ? color.gray(S_BAR) : '';
47+
return `${title}${cancelPrefix}${valueText}${value.trim() ? `\n${cancelPrefix}` : ''}`;
48+
}
49+
default: {
50+
const defaultPrefix = hasGuide ? `${color.cyan(S_BAR)} ` : '';
51+
const defaultPrefixEnd = hasGuide ? color.cyan(S_BAR_END) : '';
52+
return `${title}${defaultPrefix}${userInput}\n${defaultPrefixEnd}\n`;
4453
}
45-
default:
46-
return `${title}${color.cyan(S_BAR)} ${userInput}\n${color.cyan(S_BAR_END)}\n`;
4754
}
4855
},
4956
}).prompt() as Promise<string | symbol>;

0 commit comments

Comments
 (0)