Skip to content

Commit

Permalink
feat(msar): Improved progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
battis committed Jan 4, 2025
1 parent d6aee1d commit 44ffeca
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/msar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@battis/typescript-tricks": "^0.7.4",
"@oauth2-cli/sky-api": "^0.1.0",
"async-mutex": "^0.5.0",
"chalk": "^5.4.1",
"cli-progress": "^3.12.0",
"content-disposition": "^0.5.4",
"datadirect": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/msar/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './common/CustomError.js';
export * as Debug from './common/Debug.js';
export * as Output from './common/Output.js';
export { oxfordComma } from './common/oxfordComma.js';
export { ProgressBar } from './common/ProgressBar.js';
export * as PuppeteerSession from './common/PuppeteerSession.js';
export * as Workflow from './common/Workflow.js';
49 changes: 49 additions & 0 deletions packages/msar/src/common/ProgressBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import chalk from 'chalk';
import cliProgress from 'cli-progress';

type Options = {
value?: number;
max: number;
};

export class ProgressBar {
private multibar: cliProgress.MultiBar;
private bar: cliProgress.SingleBar;

public constructor({ value = 0, max }: Options) {
this.multibar = new cliProgress.MultiBar({
format: `{bar} {percentage}% {eta_formatted} ${chalk.yellow('{caption}')}`,
barCompleteChar: '█',
barIncompleteChar: '█',
autopadding: true,
autopaddingChar: ' ',
formatBar: (progress, options: cliProgress.Options) => {
const completeSize = Math.round(progress * options.barsize!);
const incompleteSize = options.barsize! - completeSize;
return (
chalk.green(options.barCompleteString!.substring(0, completeSize)) +
options.barGlue +
chalk.gray.dim(
options.barIncompleteString!.substring(0, incompleteSize)
)
);
}
});
this.bar = this.multibar.create(max, value);
this.bar.update({ caption: '' });
}

public increment() {
this.bar.increment();
}

public caption(caption: string) {
this.bar.update({
caption: caption.substring(0, 24) + (caption.length > 24 ? '…' : '')
});
}

public stop() {
this.multibar.stop();
}
}
14 changes: 7 additions & 7 deletions packages/msar/src/workflows/Snapshot/Manager/All.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cli from '@battis/qui-cli';
import cliProgress from 'cli-progress';
import { api, PuppeteerSession } from 'datadirect-puppeteer';
import crypto from 'node:crypto';
import fs from 'node:fs/promises';
Expand Down Expand Up @@ -47,10 +46,13 @@ export async function snapshot({
const { ignoreErrors, concurrentThreads } = options;
const { outputPath, pretty } = outputOptions;

const spinner = cli.spinner();
spinner.start('Waiting for authentication…');
const session = await PuppeteerSession.Fetchable.init(url, {
credentials,
...puppeteerOptions
});
spinner.succeed('Authentication complete.');
cli.log.info(
`Snapshot temporary files will be saved to ${cli.colors.url(TEMP)}`
);
Expand All @@ -73,8 +75,8 @@ export async function snapshot({
))
);
cli.log.info(`${groups.length} groups match filters`);
const progressBars = new cliProgress.MultiBar({});
const progress = progressBars.create(groups.length, 0);

const progress = new common.ProgressBar({ max: groups.length });
if (groupsPath) {
groupsPath = common.Output.filePathFromOutputPath(
groupsPath,
Expand Down Expand Up @@ -106,9 +108,7 @@ export async function snapshot({
// TODO Configurable snapshot --all temp directory
// TODO Optional snapshot --all temp files
common.Output.writeJSON(tempPath, snapshot);
progressBars.log(
`Wrote snapshot ${snapshot?.SectionInfo?.Teacher}'s ${snapshot?.SectionInfo?.SchoolYear} ${snapshot?.SectionInfo?.GroupName} ${snapshot?.SectionInfo?.Block} to ${cli.colors.url(outputPath)}\n`
);
progress.caption(snapshot?.SectionInfo?.GroupName || '');
} catch (error) {
if (ignoreErrors) {
common.Debug.errorWithGroupId(
Expand Down Expand Up @@ -167,7 +167,7 @@ export async function snapshot({
cli.log.error(`Errors output to ${cli.colors.url(errorsPath)}`);
}
await fs.rm(TEMP, { recursive: true });
progressBars.stop();
progress.stop();

if (quit) {
session.close();
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44ffeca

Please sign in to comment.