Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): apps with many resources scroll resource output offscreen #19742

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});