Skip to content

Commit 5bb7e10

Browse files
KayLeungjoaolucasl
authored andcommitted
Fix: use simpler charaters for progress bar to avoid overflows (yarnpkg#4317)
**Summary** Fixes yarnpkg#2530. This patch replaces the 2-byte progress bar characters with `-` and `#` wrapped in a pair of `[` and `]` symbols to make it looks like a progress bar on the console with "simple", one-byte characters. The reason for preferring one-byte characters is the inconsistent width calculation on certain terminal emulators causing the calculated progress bar width to overflow the available terminal width, causing the progress bar to split into multiple lines. It now looks like this: ![new progress bar chars](https://i.imgur.com/d8XA4yS.gif) **Test plan** Manual verification and updating of existing test snapshots.
1 parent 32b5f5b commit 5bb7e10

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

__tests__/reporters/__snapshots__/console-reporter.js.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Object {
5858

5959
exports[`ConsoleReporter.progress 1`] = `
6060
Object {
61-
"stderr": "[2K[1G[1G░░ 0/2[1G█░ 1/2[2K[1G",
61+
"stderr": "[2K[1G[1G[--] 0/2[1G[#-] 1/2[2K[1G",
6262
"stdout": "",
6363
}
6464
`;
@@ -132,11 +132,11 @@ Object {
132132
}
133133
`;
134134
135-
exports[`ProgressBar 1`] = `"[2K[1G[1G░░ 0/2"`;
135+
exports[`ProgressBar 1`] = `"[2K[1G[1G[--] 0/2"`;
136136
137-
exports[`ProgressBar 2`] = `"[2K[1G[1G░░ 0/2[1G█░ 1/2"`;
137+
exports[`ProgressBar 2`] = `"[2K[1G[1G[--] 0/2[1G[#-] 1/2"`;
138138
139-
exports[`ProgressBar 3`] = `"[2K[1G[1G░░ 0/2[1G█░ 1/2[1G██ 2/2"`;
139+
exports[`ProgressBar 3`] = `"[2K[1G[1G[--] 0/2[1G[#-] 1/2[1G[##] 2/2"`;
140140
141141
exports[`Spinner 1`] = `"⠁ "`;
142142
@@ -148,7 +148,7 @@ exports[`Spinner 4`] = `"⠁ ⠂ foo⠄ bar"`;
148148
149149
exports[`close 1`] = `
150150
Object {
151-
"stderr": "[2K[1G[1G░░ 0/2[1G█░ 1/2[1G⠁ [0K[2K[1G[2K[1G",
151+
"stderr": "[2K[1G[1G[--] 0/2[1G[#-] 1/2[1G⠁ [0K[2K[1G[2K[1G",
152152
"stdout": "",
153153
}
154154
`;

src/reporters/console/progress-bar.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class ProgressBar {
2323
id: ?number;
2424
_callback: ?(progressBar: ProgressBar) => void;
2525

26-
static bars = [['', '']];
26+
static bars = [['#', '-']];
2727

2828
tick() {
2929
if (this.curr >= this.total) {
@@ -68,12 +68,12 @@ export default class ProgressBar {
6868

6969
// calculate size of actual bar
7070
// $FlowFixMe: investigate process.stderr.columns flow error
71-
const availableSpace = Math.max(0, this.stdout.columns - bar.length - 1);
71+
const availableSpace = Math.max(0, this.stdout.columns - bar.length - 3);
7272
const width = Math.min(this.total, availableSpace);
7373
const completeLength = Math.round(width * ratio);
7474
const complete = this.chars[0].repeat(completeLength);
7575
const incomplete = this.chars[1].repeat(width - completeLength);
76-
bar = `${complete}${incomplete}${bar}`;
76+
bar = `[${complete}${incomplete}]${bar}`;
7777

7878
toStartOfLine(this.stdout);
7979
this.stdout.write(bar);

0 commit comments

Comments
 (0)