chore: show small progress bar in downloader without tty (#13690)
This commit is contained in:
parent
317b649f78
commit
39525878ab
|
|
@ -115,12 +115,16 @@ export async function download(
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDownloadProgress(progressBarName: string): OnProgressCallback {
|
function getDownloadProgress(progressBarName: string): OnProgressCallback {
|
||||||
|
if (process.stdout.isTTY)
|
||||||
|
return _getAnimatedDownloadProgress(progressBarName);
|
||||||
|
return _getBasicDownloadProgress(progressBarName);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _getAnimatedDownloadProgress(progressBarName: string): OnProgressCallback {
|
||||||
let progressBar: ProgressBar;
|
let progressBar: ProgressBar;
|
||||||
let lastDownloadedBytes = 0;
|
let lastDownloadedBytes = 0;
|
||||||
|
|
||||||
return (downloadedBytes: number, totalBytes: number) => {
|
return (downloadedBytes: number, totalBytes: number) => {
|
||||||
if (!process.stderr.isTTY)
|
|
||||||
return;
|
|
||||||
if (!progressBar) {
|
if (!progressBar) {
|
||||||
progressBar = new ProgressBar(
|
progressBar = new ProgressBar(
|
||||||
`Downloading ${progressBarName} - ${toMegabytes(
|
`Downloading ${progressBarName} - ${toMegabytes(
|
||||||
|
|
@ -140,6 +144,24 @@ function getDownloadProgress(progressBarName: string): OnProgressCallback {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _getBasicDownloadProgress(progressBarName: string): OnProgressCallback {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`Downloading ${progressBarName}...`);
|
||||||
|
const totalRows = 10;
|
||||||
|
const stepWidth = 8;
|
||||||
|
let lastRow = -1;
|
||||||
|
return (downloadedBytes: number, totalBytes: number) => {
|
||||||
|
const percentage = downloadedBytes / totalBytes;
|
||||||
|
const row = Math.floor(totalRows * percentage);
|
||||||
|
if (row > lastRow) {
|
||||||
|
lastRow = row;
|
||||||
|
const percentageString = String(percentage * 100 | 0).padStart(3);
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`|${'■'.repeat(row * stepWidth)}${' '.repeat((totalRows - row) * stepWidth)}| ${percentageString}% of ${toMegabytes(totalBytes)}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function toMegabytes(bytes: number) {
|
function toMegabytes(bytes: number) {
|
||||||
const mb = bytes / 1024 / 1024;
|
const mb = bytes / 1024 / 1024;
|
||||||
return `${Math.round(mb * 10) / 10} Mb`;
|
return `${Math.round(mb * 10) / 10} Mb`;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue