Skip to content

Commit

Permalink
Refactor: reformat the DAT processing progress bar (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Sep 22, 2023
1 parent 4ee5583 commit 4701b8a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"robloach-datfile": "2.4.0",
"semver": "7.5.4",
"simple-statistics": "7.8.3",
"strip-ansi": "7.1.0",
"tar": "6.1.15",
"term-size": "3.0.2",
"trash": "8.1.1",
Expand Down Expand Up @@ -124,7 +125,6 @@
"eslint-plugin-unicorn": "48.0.1",
"husky": "8.0.3",
"jest": "29.6.4",
"strip-ansi": "7.1.0",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "5.2.2",
Expand Down
3 changes: 2 additions & 1 deletion src/console/progressBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import LogLevel from './logLevel.js';
* @see https://www.fileformat.info/info/unicode/font/lucida_console/grid.htm (win32)
*/
export const ProgressBarSymbol = {
NONE: '',
WAITING: chalk.grey(process.platform === 'win32' ? '…' : '⋯'),
PROCESSING: chalk.cyan(process.platform === 'win32' ? '¤' : '⚙'),
// Files
DOWNLOADING: chalk.bold('↓'),
DOWNLOADING: chalk.magenta('↓'),
SEARCHING: chalk.magenta(process.platform === 'win32' ? '○' : '↻'),
HASHING: chalk.magenta('#'),
INDEXING: chalk.magenta('#'),
Expand Down
30 changes: 15 additions & 15 deletions src/console/singleBarFormatted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
MultiBar, Options, Params, SingleBar,
} from 'cli-progress';
import { linearRegression, linearRegressionLine } from 'simple-statistics';
import stripAnsi from 'strip-ansi';

import ProgressBarPayload from './progressBarPayload.js';

Expand Down Expand Up @@ -34,7 +35,7 @@ export default class SingleBarFormatted {
this.multiBar = multiBar;
this.singleBar = this.multiBar.create(initialTotal, 0, initialPayload, {
format: (options, params, payload: ProgressBarPayload): string => {
this.lastOutput = `${`${SingleBarFormatted.getSymbol(payload)} ${SingleBarFormatted.getName(payload)}`.trim()} | ${this.getProgress(options, params, payload)}`.trim();
this.lastOutput = `${SingleBarFormatted.getSymbolAndName(payload)} | ${this.getProgress(options, params, payload)}`.trim();
return this.lastOutput;
},
});
Expand All @@ -48,22 +49,21 @@ export default class SingleBarFormatted {
return this.lastOutput;
}

private static getSymbol(payload: ProgressBarPayload): string {
if (!payload.symbol) {
return '';
}
return chalk.bold(payload.symbol);
}
private static getSymbolAndName(payload: ProgressBarPayload): string {
const symbol = chalk.bold(payload.symbol ?? '');
const name = payload.name ?? '';

private static getName(payload: ProgressBarPayload): string {
if (!payload.name) {
return '';
}
const namePadded = `${name} ${'·'.repeat(SingleBarFormatted.MAX_NAME_LENGTH)}`.trim();
const symbolAndName = `${symbol} ${namePadded}`;

const excessLength = stripAnsi(symbolAndName).trimStart().length
- SingleBarFormatted.MAX_NAME_LENGTH;
const nameTrimmed = namePadded.slice(
0,
namePadded.length - (excessLength > 0 ? excessLength : 0),
);

const payloadName = payload.name.slice(0, SingleBarFormatted.MAX_NAME_LENGTH);
return payloadName.length > SingleBarFormatted.MAX_NAME_LENGTH - 1
? payloadName.padEnd(SingleBarFormatted.MAX_NAME_LENGTH, ' ')
: `${payloadName} ${'·'.repeat(SingleBarFormatted.MAX_NAME_LENGTH - 1 - payloadName.length)}`;
return `${symbol} ${nameTrimmed}`.trimStart();
}

private getProgress(options: Options, params: Params, payload: ProgressBarPayload): string {
Expand Down
3 changes: 2 additions & 1 deletion src/igir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import async from 'async';
import chalk from 'chalk';
import isAdmin from 'is-admin';

import Logger from './console/logger.js';
Expand Down Expand Up @@ -72,7 +73,7 @@ export default class Igir {
const patches = await this.processPatchScanner();

// Set up progress bar and input for DAT processing
const datProcessProgressBar = await this.logger.addProgressBar('Processing DATs', ProgressBarSymbol.PROCESSING, dats.length);
const datProcessProgressBar = await this.logger.addProgressBar(chalk.underline('Processing DATs'), ProgressBarSymbol.NONE, dats.length);
if (!dats.length) {
dats = new DATGameInferrer(datProcessProgressBar).infer(roms);
}
Expand Down
8 changes: 4 additions & 4 deletions test/console/singleBarFormatted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ describe('getLastOutput', () => {
testSingleBarFormatted(100, {}, (singleBarFormatted) => {
singleBarFormatted.getSingleBar().render();

expect(singleBarFormatted.getLastOutput()).toEqual('| ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 0/100');
expect(singleBarFormatted.getLastOutput()).toEqual('······························ | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 0/100');
});
});
});

describe('format', () => {
test.each([
[{}, '| ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{ symbol: '@' }, '@ | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{ symbol: '@', name: 'name' }, '@ name ························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{}, '······························ | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{ symbol: '@' }, '@ ···························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{ symbol: '@', name: 'name' }, '@ name ······················· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{ name: 'name' }, 'name ························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'],
[{ name: 'name', waitingMessage: 'waiting' }, 'name ························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100 | waiting'],
[{ name: 'name', finishedMessage: 'done' }, 'name ························· | done'],
Expand Down

0 comments on commit 4701b8a

Please sign in to comment.