Skip to content

Commit 69b0ca0

Browse files
committed
fix: implement raw option
1 parent 2cc1bff commit 69b0ca0

File tree

3 files changed

+184
-2
lines changed

3 files changed

+184
-2
lines changed

packages/prompts/src/__snapshots__/index.test.ts.snap

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,70 @@ exports[`prompts (isCI = false) > taskLog > message > enforces limit if set 1`]
16071607
]
16081608
`;
16091609

1610+
exports[`prompts (isCI = false) > taskLog > message > raw = true appends message text until newline 1`] = `
1611+
[
1612+
"│
1613+
",
1614+
"◇ foo
1615+
",
1616+
"│
1617+
",
1618+
"",
1619+
"│ line 0
1620+
",
1621+
"",
1622+
"│ line 0still line 0
1623+
",
1624+
"",
1625+
"│ line 0still line 0
1626+
│ line 1
1627+
",
1628+
]
1629+
`;
1630+
1631+
exports[`prompts (isCI = false) > taskLog > message > raw = true works when mixed with non-raw messages 1`] = `
1632+
[
1633+
"│
1634+
",
1635+
"◇ foo
1636+
",
1637+
"│
1638+
",
1639+
"",
1640+
"│ line 0
1641+
",
1642+
"",
1643+
"│ line 0still line 0
1644+
",
1645+
"",
1646+
"│ line 0still line 0
1647+
│ line 1
1648+
",
1649+
]
1650+
`;
1651+
1652+
exports[`prompts (isCI = false) > taskLog > message > raw = true works when started with non-raw messages 1`] = `
1653+
[
1654+
"│
1655+
",
1656+
"◇ foo
1657+
",
1658+
"│
1659+
",
1660+
"",
1661+
"│ line 0
1662+
",
1663+
"",
1664+
"│ line 0
1665+
│ line 1
1666+
",
1667+
"",
1668+
"│ line 0
1669+
│ line 1still line 1
1670+
",
1671+
]
1672+
`;
1673+
16101674
exports[`prompts (isCI = false) > taskLog > success > clears output and renders message 1`] = `
16111675
[
16121676
"│
@@ -3458,6 +3522,70 @@ exports[`prompts (isCI = true) > taskLog > message > enforces limit if set 1`] =
34583522
]
34593523
`;
34603524

3525+
exports[`prompts (isCI = true) > taskLog > message > raw = true appends message text until newline 1`] = `
3526+
[
3527+
"│
3528+
",
3529+
"◇ foo
3530+
",
3531+
"│
3532+
",
3533+
"",
3534+
"│ line 0
3535+
",
3536+
"",
3537+
"│ line 0still line 0
3538+
",
3539+
"",
3540+
"│ line 0still line 0
3541+
│ line 1
3542+
",
3543+
]
3544+
`;
3545+
3546+
exports[`prompts (isCI = true) > taskLog > message > raw = true works when mixed with non-raw messages 1`] = `
3547+
[
3548+
"│
3549+
",
3550+
"◇ foo
3551+
",
3552+
"│
3553+
",
3554+
"",
3555+
"│ line 0
3556+
",
3557+
"",
3558+
"│ line 0still line 0
3559+
",
3560+
"",
3561+
"│ line 0still line 0
3562+
│ line 1
3563+
",
3564+
]
3565+
`;
3566+
3567+
exports[`prompts (isCI = true) > taskLog > message > raw = true works when started with non-raw messages 1`] = `
3568+
[
3569+
"│
3570+
",
3571+
"◇ foo
3572+
",
3573+
"│
3574+
",
3575+
"",
3576+
"│ line 0
3577+
",
3578+
"",
3579+
"│ line 0
3580+
│ line 1
3581+
",
3582+
"",
3583+
"│ line 0
3584+
│ line 1still line 1
3585+
",
3586+
]
3587+
`;
3588+
34613589
exports[`prompts (isCI = true) > taskLog > success > clears output and renders message 1`] = `
34623590
[
34633591
"│

packages/prompts/src/index.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,48 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
12331233

12341234
expect(output.buffer).toMatchSnapshot();
12351235
});
1236+
1237+
test('raw = true appends message text until newline', async () => {
1238+
const log = prompts.taskLog({
1239+
input,
1240+
output,
1241+
message: 'foo',
1242+
});
1243+
1244+
log.message('line 0', { raw: true });
1245+
log.message('still line 0', { raw: true });
1246+
log.message('\nline 1', { raw: true });
1247+
1248+
expect(output.buffer).toMatchSnapshot();
1249+
});
1250+
1251+
test('raw = true works when mixed with non-raw messages', async () => {
1252+
const log = prompts.taskLog({
1253+
input,
1254+
output,
1255+
message: 'foo',
1256+
});
1257+
1258+
log.message('line 0', { raw: true });
1259+
log.message('still line 0', { raw: true });
1260+
log.message('line 1');
1261+
1262+
expect(output.buffer).toMatchSnapshot();
1263+
});
1264+
1265+
test('raw = true works when started with non-raw messages', async () => {
1266+
const log = prompts.taskLog({
1267+
input,
1268+
output,
1269+
message: 'foo',
1270+
});
1271+
1272+
log.message('line 0');
1273+
log.message('line 1', { raw: true });
1274+
log.message('still line 1', { raw: true });
1275+
1276+
expect(output.buffer).toMatchSnapshot();
1277+
});
12361278
});
12371279

12381280
describe('error', () => {

packages/prompts/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,10 @@ export interface TaskLogOptions extends CommonOptions {
10161016
limit?: number;
10171017
}
10181018

1019+
export interface TaskLogMessageOptions {
1020+
raw?: boolean;
1021+
}
1022+
10191023
/**
10201024
* Renders a log which clears on success and remains on failure
10211025
*/
@@ -1028,6 +1032,7 @@ export const taskLog = (opts: TaskLogOptions) => {
10281032
output.write(`${color.dim(S_BAR)}\n`);
10291033

10301034
const buffer: string[] = [];
1035+
let lastMessageWasRaw = false;
10311036

10321037
const clear = (clearTitle: boolean): void => {
10331038
const bufferHeight = buffer.reduce((count, line) => {
@@ -1038,9 +1043,16 @@ export const taskLog = (opts: TaskLogOptions) => {
10381043
};
10391044

10401045
return {
1041-
message(msg: string) {
1046+
message(msg: string, mopts?: TaskLogMessageOptions) {
10421047
clear(false);
1043-
const lines = msg.split('\n');
1048+
let lines: string[];
1049+
if (mopts?.raw && buffer.length > 0 && lastMessageWasRaw) {
1050+
const lastBuffer = buffer.pop();
1051+
lines = (lastBuffer + msg).split('\n');
1052+
} else {
1053+
lines = msg.split('\n');
1054+
}
1055+
lastMessageWasRaw = mopts?.raw === true;
10441056
for (const line of lines) {
10451057
buffer.push(line);
10461058
}

0 commit comments

Comments
 (0)