Skip to content

Commit 053d22c

Browse files
fix(cli): apps with many resources scroll resource output offscreen (#19742)
When a stack contains a lot of resources, the RewritableBlock size becomes larger than the terminal screen, pushing the output offscreen and making it so the user has to scroll to see new output. This change adjusts the size of the RewritableBlock so that the maximum height it can be is the window height (with one line padding) so that the output never moves offscreen. It also removes the extra lines at the end of the output so that the deployment summary doesn't have a bunch of blank lines before it. Fixes #19160 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a2ac36e commit 053d22c

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

packages/aws-cdk/lib/api/util/cloudformation/stack-activity-monitor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ export class CurrentActivityPrinter extends ActivityPrinterBase {
665665

666666
// Display in the same block space, otherwise we're going to have silly empty lines.
667667
this.block.displayLines(lines);
668+
this.block.removeEmptyLines(lines);
668669
}
669670

670671
private progressBar(width: number) {

packages/aws-cdk/lib/api/util/display.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ export class RewritableBlock {
1616
return this.stream.columns;
1717
}
1818

19+
public get height() {
20+
// Might get changed if the user resizes the terminal
21+
return this.stream.rows;
22+
}
23+
1924
public displayLines(lines: string[]) {
20-
lines = terminalWrap(this.width, expandNewlines(lines));
25+
lines = terminalWrap(this.width, expandNewlines(lines)).slice(0, getMaxBlockHeight(this.height, this.lastHeight, lines));
2126

2227
this.stream.write(cursorUp(this.lastHeight));
2328
for (const line of lines) {
@@ -31,6 +36,10 @@ export class RewritableBlock {
3136
// The block can only ever get bigger
3237
this.lastHeight = Math.max(this.lastHeight, lines.length);
3338
}
39+
40+
public removeEmptyLines(lines: string[]) {
41+
this.stream.write(cursorUp(this.lastHeight - lines.length));
42+
}
3443
}
3544

3645
const ESC = '\u001b';
@@ -73,4 +82,9 @@ function expandNewlines(lines: string[]): string[] {
7382
ret.push(...line.split('\n'));
7483
}
7584
return ret;
85+
}
86+
87+
function getMaxBlockHeight(windowHeight: number | undefined, lastHeight: number, lines: string[]): number {
88+
if (windowHeight === undefined) { return Math.max(lines.length, lastHeight); }
89+
return lines.length < windowHeight ? lines.length : windowHeight - 1;
7690
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { RewritableBlock } from '../../../lib/api/util/display';
2+
import { stderr } from '../console-listener';
3+
4+
5+
describe('Rewritable Block Tests', () => {
6+
let block: RewritableBlock;
7+
beforeEach(() => {
8+
block = new RewritableBlock(process.stderr);
9+
process.stderr.rows = 80;
10+
});
11+
12+
test('displayLines writes maximum lines based on rows if there are more lines than rows', () => {
13+
const lines = Array.from(Array(100).keys()).map(line => line.toString());
14+
const output = stderr.inspectSync(() => {
15+
block.displayLines(lines);
16+
});
17+
18+
expect(output.length).toEqual(block.height!);
19+
});
20+
21+
test('displayLines writes maximum lines based on lines length if there are less lines than rows', () => {
22+
const lines = Array.from(Array(45).keys()).map(line => line.toString());
23+
const output = stderr.inspectSync(() => {
24+
block.displayLines(lines);
25+
});
26+
27+
expect(output.length).toEqual(46);
28+
});
29+
30+
test('displayLines writes maximum lines based on lines length if rows is undefined', () => {
31+
const lines = Array.from(Array(5).keys()).map(line => line.toString());
32+
process.stderr.rows = undefined;
33+
const output = stderr.inspectSync(() => {
34+
block.displayLines(lines);
35+
});
36+
37+
expect(output.length).toEqual(6);
38+
});
39+
});

0 commit comments

Comments
 (0)