Skip to content

Commit

Permalink
feat(stats): highlight values when updating
Browse files Browse the repository at this point in the history
  • Loading branch information
zaldih committed May 26, 2024
1 parent f3c8feb commit 0ad221c
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions src/ui/components/header/stats.ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ import { ResultsService } from '../../../services/results.service.js';
import { LoggerService } from '../../../services/logger.service.js';
import colors from 'colors';
import { IConfig } from 'src/interfaces/config.interface.js';
import { IPosition } from 'src/interfaces/ui-positions.interface.js';

interface ShowStatProps {
description: string;
value: string;
lastValueKey: 'totalSpace' | 'spaceReleased';
position: IPosition;
updateColor: 'green' | 'yellow';
}

export class StatsUi extends BaseUi {
private lastValues = {
totalSpace: '',
spaceReleased: '',
};

private timeouts = {
totalSpace: setTimeout(() => {}),
spaceReleased: setTimeout(() => {}),
};

constructor(
private readonly config: IConfig,
private readonly resultsService: ResultsService,
Expand All @@ -17,20 +36,64 @@ export class StatsUi extends BaseUi {
render(): void {
const { totalSpace, spaceReleased } = this.resultsService.getStats();

const totalSpacePosition = { ...UI_POSITIONS.TOTAL_SPACE };
const spaceReleasedPosition = { ...UI_POSITIONS.SPACE_RELEASED };

totalSpacePosition.x += INFO_MSGS.TOTAL_SPACE.length;
spaceReleasedPosition.x += INFO_MSGS.SPACE_RELEASED.length;
this.showStat({
description: INFO_MSGS.TOTAL_SPACE,
value: totalSpace,
lastValueKey: 'totalSpace',
position: UI_POSITIONS.TOTAL_SPACE,
updateColor: 'yellow',
});

this.printAt(totalSpace, totalSpacePosition);
this.printAt(spaceReleased, spaceReleasedPosition);
this.showStat({
description: INFO_MSGS.SPACE_RELEASED,
value: spaceReleased,
lastValueKey: 'spaceReleased',
position: UI_POSITIONS.SPACE_RELEASED,
updateColor: 'green',
});

if (this.config.showErrors) {
this.showErrorsCount();
}
}

/** Print the value of the stat and if it is a different value from the
* previous run, highlight it for a while.
*/
private showStat({
description,
value,
lastValueKey,
position,
updateColor,
}: ShowStatProps): void {
if (value === this.lastValues[lastValueKey]) {
return;
}

const statPosition = { ...position };
statPosition.x += description.length;

// If is first render, initialize.
if (!this.lastValues[lastValueKey]) {
this.printAt(value, statPosition);
this.lastValues[lastValueKey] = value;
return;
}

this.printAt(colors[updateColor](`${value} 鈻瞏), statPosition);

if (this.timeouts[lastValueKey]) {
clearTimeout(this.timeouts[lastValueKey]);
}

this.timeouts[lastValueKey] = setTimeout(() => {
this.printAt(value + ' ', statPosition);
}, 700);

this.lastValues[lastValueKey] = value;
}

private showErrorsCount(): void {
const errors = this.logger.get('error').length;

Expand Down

0 comments on commit 0ad221c

Please sign in to comment.