Skip to content

Commit 13df34d

Browse files
authored
fix: limit dependency install's output window size (#307)
* limit window size * changeset * fix on windows and account for line wraps
1 parent 39e2022 commit 13df34d

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

.changeset/modern-emus-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix: limit window height of dependency install's output

packages/clack-prompts/index.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -614,37 +614,49 @@ export const taskLog = (title: string) => {
614614
process.stdout.write(`${ACTIVE} ${title}\n`);
615615

616616
let output = '';
617+
let frame = '';
617618

618619
// clears previous output
619-
const clear = (buffer = 0): void => {
620-
if (!output) return;
621-
const lines = output.split('\n').length + buffer;
622-
process.stdout.write(erase.lines(lines + 1));
620+
const clear = (eraseTitle = false): void => {
621+
if (!frame) return;
622+
const terminalWidth = process.stdout.columns;
623+
const frameHeight = frame.split('\n').reduce((height, line) => {
624+
// accounts for line wraps
625+
height += Math.ceil(line.length / terminalWidth);
626+
return height;
627+
}, 0);
628+
const lines = frameHeight + (eraseTitle ? 1 : 0);
629+
630+
process.stdout.write(cursor.up(lines));
631+
process.stdout.write(erase.down());
623632
};
624633

625634
// logs the output
626-
const print = (): void => {
627-
const lines = output.split('\n');
635+
const print = (limit = 0): void => {
636+
const lines = output.split('\n').slice(-limit);
637+
// reset frame
638+
frame = '';
628639
for (const line of lines) {
629-
const msg = color.dim(`${BAR} ${line}\n`);
630-
process.stdout.write(msg);
640+
frame += `${BAR} ${line}\n`;
631641
}
642+
process.stdout.write(color.dim(frame));
632643
};
633644

634645
return {
635646
set text(data: string) {
636647
clear();
637648
output += data;
638-
print();
649+
// half the height of the terminal
650+
const frameHeight = Math.ceil(process.stdout.rows / 2);
651+
print(frameHeight);
639652
},
640653
fail(message: string): void {
641-
clear(1); // includes clearing the `title`
654+
clear(true);
642655
process.stdout.write(`${ERROR} ${message}\n`);
643-
// log the output on failure
644-
print();
656+
print(); // log the output on failure
645657
},
646658
success(message: string): void {
647-
clear(1); // includes clearing the `title`
659+
clear(true);
648660
process.stdout.write(`${SUCCESS} ${message}\n`);
649661
}
650662
};

0 commit comments

Comments
 (0)