Skip to content

Commit bee79dd

Browse files
committed
feat: add withBorder to text input
1 parent 3d40389 commit bee79dd

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

packages/prompts/src/text.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 withBorder = opts.withBorder !== false;
24+
const titlePrefix = withBorder ? `${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,26 @@ 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 = withBorder ? `${color.yellow(S_BAR)} ` : '';
36+
const errorPrefixEnd = withBorder ? 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 = withBorder ? 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 = withBorder ? color.gray(S_BAR) : '';
47+
return `${title}${cancelPrefix}${valueText}${value.trim() ? `\n${cancelPrefix}` : ''}`;
48+
}
49+
default: {
50+
const defaultPrefix = withBorder ? `${color.cyan(S_BAR)} ` : '';
51+
const defaultPrefixEnd = withBorder ? color.cyan(S_BAR_END) : '';
52+
color.cyan(S_BAR_END);
53+
return `${title}${defaultPrefix}${userInput}\n${defaultPrefixEnd}\n`;
4454
}
45-
default:
46-
return `${title}${color.cyan(S_BAR)} ${userInput}\n${color.cyan(S_BAR_END)}\n`;
4755
}
4856
},
4957
}).prompt() as Promise<string | symbol>;

packages/prompts/test/__snapshots__/text.test.ts.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ exports[`text (isCI = false) > validation errors render and clear 1`] = `
263263
]
264264
`;
265265
266+
exports[`text (isCI = false) > withBorder: false removes borders 1`] = `
267+
[
268+
"<cursor.hide>",
269+
"foo
270+
_
271+
272+
",
273+
"<cursor.backward count=999><cursor.up count=3>",
274+
"<cursor.down count=1>",
275+
"<erase.down>",
276+
"",
277+
"
278+
",
279+
"<cursor.show>",
280+
]
281+
`;
282+
266283
exports[`text (isCI = true) > can be aborted by a signal 1`] = `
267284
[
268285
"<cursor.hide>",
@@ -525,3 +542,20 @@ exports[`text (isCI = true) > validation errors render and clear 1`] = `
525542
"<cursor.show>",
526543
]
527544
`;
545+
546+
exports[`text (isCI = true) > withBorder: false removes borders 1`] = `
547+
[
548+
"<cursor.hide>",
549+
"foo
550+
_
551+
552+
",
553+
"<cursor.backward count=999><cursor.up count=3>",
554+
"<cursor.down count=1>",
555+
"<erase.down>",
556+
"",
557+
"
558+
",
559+
"<cursor.show>",
560+
]
561+
`;

packages/prompts/test/text.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,19 @@ describe.each(['true', 'false'])('text (isCI = %s)', (isCI) => {
205205
expect(prompts.isCancel(value)).toBe(true);
206206
expect(output.buffer).toMatchSnapshot();
207207
});
208+
209+
test('withBorder: false removes borders', async () => {
210+
const result = prompts.text({
211+
message: 'foo',
212+
withBorder: false,
213+
input,
214+
output,
215+
});
216+
217+
input.emit('keypress', '', { name: 'return' });
218+
219+
await result;
220+
221+
expect(output.buffer).toMatchSnapshot();
222+
});
208223
});

0 commit comments

Comments
 (0)