Skip to content

Commit

Permalink
fix(cli): apps with many resources scroll resource output offscreen (#…
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
TheRealAmazonKendra authored Apr 5, 2022
1 parent a2ac36e commit 053d22c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ export class CurrentActivityPrinter extends ActivityPrinterBase {

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

private progressBar(width: number) {
Expand Down
16 changes: 15 additions & 1 deletion packages/aws-cdk/lib/api/util/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ export class RewritableBlock {
return this.stream.columns;
}

public get height() {
// Might get changed if the user resizes the terminal
return this.stream.rows;
}

public displayLines(lines: string[]) {
lines = terminalWrap(this.width, expandNewlines(lines));
lines = terminalWrap(this.width, expandNewlines(lines)).slice(0, getMaxBlockHeight(this.height, this.lastHeight, lines));

this.stream.write(cursorUp(this.lastHeight));
for (const line of lines) {
Expand All @@ -31,6 +36,10 @@ export class RewritableBlock {
// The block can only ever get bigger
this.lastHeight = Math.max(this.lastHeight, lines.length);
}

public removeEmptyLines(lines: string[]) {
this.stream.write(cursorUp(this.lastHeight - lines.length));
}
}

const ESC = '\u001b';
Expand Down Expand Up @@ -73,4 +82,9 @@ function expandNewlines(lines: string[]): string[] {
ret.push(...line.split('\n'));
}
return ret;
}

function getMaxBlockHeight(windowHeight: number | undefined, lastHeight: number, lines: string[]): number {
if (windowHeight === undefined) { return Math.max(lines.length, lastHeight); }
return lines.length < windowHeight ? lines.length : windowHeight - 1;
}
39 changes: 39 additions & 0 deletions packages/aws-cdk/test/api/util/display.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { RewritableBlock } from '../../../lib/api/util/display';
import { stderr } from '../console-listener';


describe('Rewritable Block Tests', () => {
let block: RewritableBlock;
beforeEach(() => {
block = new RewritableBlock(process.stderr);
process.stderr.rows = 80;
});

test('displayLines writes maximum lines based on rows if there are more lines than rows', () => {
const lines = Array.from(Array(100).keys()).map(line => line.toString());
const output = stderr.inspectSync(() => {
block.displayLines(lines);
});

expect(output.length).toEqual(block.height!);
});

test('displayLines writes maximum lines based on lines length if there are less lines than rows', () => {
const lines = Array.from(Array(45).keys()).map(line => line.toString());
const output = stderr.inspectSync(() => {
block.displayLines(lines);
});

expect(output.length).toEqual(46);
});

test('displayLines writes maximum lines based on lines length if rows is undefined', () => {
const lines = Array.from(Array(5).keys()).map(line => line.toString());
process.stderr.rows = undefined;
const output = stderr.inspectSync(() => {
block.displayLines(lines);
});

expect(output.length).toEqual(6);
});
});

0 comments on commit 053d22c

Please sign in to comment.