Skip to content

Commit

Permalink
fix(cli): crash on tiny reported terminal width (aws#8675)
Browse files Browse the repository at this point in the history
Fix a monitor crash that happens when a narrow terminal width is
reported, leading the calculation for progress bar filler to go negative.

Fixes aws#8667.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored Jun 22, 2020
1 parent 06afc80 commit a186c24
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export class CurrentActivityPrinter extends ActivityPrinterBase {
const lines = [];

// Add a progress bar at the top
const progressWidth = Math.min((this.block.width ?? 80) - PROGRESSBAR_EXTRA_SPACE - 1, MAX_PROGRESSBAR_WIDTH);
const progressWidth = Math.max(Math.min((this.block.width ?? 80) - PROGRESSBAR_EXTRA_SPACE - 1, MAX_PROGRESSBAR_WIDTH), MIN_PROGRESSBAR_WIDTH);
const prog = this.progressBar(progressWidth);
if (prog) {
lines.push(' ' + prog, '');
Expand Down Expand Up @@ -550,12 +550,13 @@ export class CurrentActivityPrinter extends ActivityPrinterBase {
private progressBar(width: number) {
if (!this.resourcesTotal) { return ''; }
const fraction = Math.min(this.resourcesDone / this.resourcesTotal, 1);
const chars = (width - 2) * fraction;
const innerWidth = Math.max(1, width - 2);
const chars = innerWidth * fraction;
const remainder = chars - Math.floor(chars);

const fullChars = FULL_BLOCK.repeat(Math.floor(chars));
const partialChar = PARTIAL_BLOCK[Math.floor(remainder * PARTIAL_BLOCK.length)];
const filler = '·'.repeat(width - 2 - Math.floor(chars) - (partialChar ? 1 : 0));
const filler = '·'.repeat(innerWidth - Math.floor(chars) - (partialChar ? 1 : 0));

const color = this.rollingBack ? colors.yellow : colors.green;

Expand All @@ -572,6 +573,7 @@ export class CurrentActivityPrinter extends ActivityPrinterBase {
const FULL_BLOCK = '█';
const PARTIAL_BLOCK = ['', '▏', '▎', '▍', '▌', '▋', '▊', '▉'];
const MAX_PROGRESSBAR_WIDTH = 60;
const MIN_PROGRESSBAR_WIDTH = 10;
const PROGRESSBAR_EXTRA_SPACE = 2 /* leading spaces */ + 2 /* brackets */ + 4 /* progress number decoration */ + 6 /* 2 progress numbers up to 999 */;

function colorFromStatusResult(status?: string) {
Expand Down

0 comments on commit a186c24

Please sign in to comment.