Skip to content

Commit 586002a

Browse files
committed
chore: extract wrap function to utils
1 parent 63f3147 commit 586002a

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export { default as SelectPrompt } from './prompts/select.js';
88
export { default as SelectKeyPrompt } from './prompts/select-key.js';
99
export { default as TextPrompt } from './prompts/text.js';
1010
export type { ClackState as State } from './types.js';
11-
export { block, getColumns, getRows, isCancel } from './utils/index.js';
11+
export { block, getColumns, getRows, isCancel, wrapTextWithPrefix } from './utils/index.js';
1212
export type { ClackSettings } from './utils/settings.js';
1313
export { settings, updateSettings } from './utils/settings.js';

packages/core/src/utils/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Key } from 'node:readline';
33
import * as readline from 'node:readline';
44
import type { Readable, Writable } from 'node:stream';
55
import { ReadStream } from 'node:tty';
6+
import { wrapAnsi } from 'fast-wrap-ansi';
67
import { cursor } from 'sisteransi';
78
import { isActionKey } from './settings.js';
89

@@ -96,3 +97,23 @@ export const getRows = (output: Writable): number => {
9697
}
9798
return 20;
9899
};
100+
101+
export function wrapTextWithPrefix(
102+
output: Writable | undefined,
103+
text: string,
104+
prefix: string,
105+
startPrefix: string = prefix
106+
): string {
107+
const columns = getColumns(output ?? stdout);
108+
const wrapped = wrapAnsi(text, columns - prefix.length, {
109+
hard: true,
110+
trim: false,
111+
});
112+
const lines = wrapped
113+
.split('\n')
114+
.map((line, index) => {
115+
return `${index === 0 ? startPrefix : prefix}${line}`;
116+
})
117+
.join('\n');
118+
return lines;
119+
}

packages/prompts/src/select.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { Writable } from 'node:stream';
2-
import { getColumns, SelectPrompt } from '@clack/core';
3-
import { wrapAnsi } from 'fast-wrap-ansi';
1+
import { SelectPrompt, wrapTextWithPrefix } from '@clack/core';
42
import color from 'picocolors';
53
import {
64
type CommonOptions,
@@ -105,51 +103,33 @@ export const select = <Value>(opts: SelectOptions<Value>) => {
105103
output: opts.output,
106104
initialValue: opts.initialValue,
107105
render() {
108-
const output: Writable = opts?.output ?? process.stdout;
109-
const columns = getColumns(output);
110106
const titlePrefix = `${symbol(this.state)} `;
111107
const titlePrefixBar = `${symbolBar(this.state)} `;
112-
const wrappedMessage = wrapAnsi(
108+
const messageLines = wrapTextWithPrefix(
109+
opts.output,
113110
opts.message,
114-
columns - Math.max(titlePrefix.length, titlePrefixBar.length),
115-
{
116-
hard: true,
117-
trim: false,
118-
}
111+
titlePrefixBar,
112+
titlePrefix
119113
);
120-
const messageLines = wrappedMessage
121-
.split('\n')
122-
.map((line, index) => {
123-
return `${index === 0 ? titlePrefix : titlePrefixBar}${line}`;
124-
})
125-
.join('\n');
126114
const title = `${color.gray(S_BAR)}\n${messageLines}\n`;
127115

128116
switch (this.state) {
129117
case 'submit': {
130118
const submitPrefix = `${color.gray(S_BAR)} `;
131-
const wrappedOption = wrapAnsi(
119+
const wrappedLines = wrapTextWithPrefix(
120+
opts.output,
132121
opt(this.options[this.cursor], 'selected'),
133-
columns - submitPrefix.length,
134-
{ hard: true, trim: false }
122+
submitPrefix
135123
);
136-
const wrappedLines = wrappedOption
137-
.split('\n')
138-
.map((line) => `${submitPrefix}${line}`)
139-
.join('\n');
140124
return `${title}${wrappedLines}`;
141125
}
142126
case 'cancel': {
143127
const cancelPrefix = `${color.gray(S_BAR)} `;
144-
const wrappedOption = wrapAnsi(
128+
const wrappedLines = wrapTextWithPrefix(
129+
opts.output,
145130
opt(this.options[this.cursor], 'cancelled'),
146-
columns - cancelPrefix.length,
147-
{ hard: true, trim: false }
131+
cancelPrefix
148132
);
149-
const wrappedLines = wrappedOption
150-
.split('\n')
151-
.map((line) => `${cancelPrefix}${line}`)
152-
.join('\n');
153133
return `${title}${wrappedLines}\n${color.gray(S_BAR)}`;
154134
}
155135
default: {

packages/prompts/test/select.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe.each(['true', 'false'])('select (isCI = %s)', (isCI) => {
184184

185185
input.emit('keypress', '', { name: 'return' });
186186

187-
const value = await result;
187+
await result;
188188

189189
expect(output.buffer).toMatchSnapshot();
190190
});
@@ -207,7 +207,7 @@ describe.each(['true', 'false'])('select (isCI = %s)', (isCI) => {
207207

208208
input.emit('keypress', 'escape', { name: 'escape' });
209209

210-
const value = await result;
210+
await result;
211211

212212
expect(output.buffer).toMatchSnapshot();
213213
});

0 commit comments

Comments
 (0)