fix: Fix progress bar for non-tty (#1319)

This commit is contained in:
adityapk00
2023-09-01 13:54:13 -07:00
committed by GitHub
parent bab7bba93f
commit 40e017feed
3 changed files with 25 additions and 15 deletions

View File

@@ -8,7 +8,6 @@ version: '3.9'
services:
hubble:
image: farcasterxyz/hubble:latest
tty: true
restart: unless-stopped
command: [
"node", "--no-warnings", "build/cli.js", "start",

View File

@@ -1,27 +1,33 @@
import cliProgress, { SingleBar } from "cli-progress";
import { logger } from "./logger.js";
// The global multibar, to which all progress bars are added
const multiBar = new cliProgress.MultiBar(
{
format: " {bar} {percentage}% | {name} | {value}/{total} | ETA: {eta_formatted}",
hideCursor: true,
clearOnComplete: false,
etaBuffer: 1_000,
autopadding: true,
},
cliProgress.Presets.shades_grey,
);
const allBars: SingleBar[] = [];
let finished = false;
// Add a progress bar to the console. Returns undefined if the progress bar
// cannot be added (e.g. if the process is shutting down).
// Call finishAllProgressBars() to stop all progress bars.
export function addProgressBar(name: string, total: number, options?: cliProgress.Options): SingleBar | undefined {
export function addProgressBar(name: string, total: number): SingleBar | undefined {
if (finished) {
return undefined;
}
return multiBar.create(total, 0, { name, ...options });
const bar = new cliProgress.SingleBar(
{
format: ` {bar} {percentage}% | ${name} | {value}/{total} | ETA: {eta_formatted}`,
hideCursor: true,
clearOnComplete: false,
etaBuffer: 1_000,
autopadding: true,
noTTYOutput: true,
notTTYSchedule: 3000,
},
cliProgress.Presets.shades_grey,
);
bar.start(total, 0);
allBars.push(bar);
return bar;
}
// Finish all progress bars. This should be called when the process is shutting
@@ -44,7 +50,7 @@ export function finishAllProgressBars(showDelay = false): void {
finished = true;
}
multiBar.stop();
allBars.forEach((bar) => bar.stop());
logger.flush();
})();
}