@codecovevienna/gittt-cli
Version:
Tracking time with CLI into a git repository
30 lines (29 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChartHelper = void 0;
class ChartHelper {
}
exports.ChartHelper = ChartHelper;
ChartHelper.bar = (value, maxValue, maxBarLength) => {
const fractions = ["▏", "▎", "▍", "▋", "▊", "▉"];
const barLength = value * maxBarLength / maxValue;
const wholeNumberPart = Math.floor(barLength);
const fractionalPart = barLength - wholeNumberPart;
let bar = fractions[fractions.length - 1].repeat(wholeNumberPart);
if (fractionalPart > 0) {
bar += fractions[Math.floor(fractionalPart * fractions.length)];
}
return bar;
};
ChartHelper.chart = (data, showValue = false, maxBarLength = 100, sort = true, postValue = "") => {
const formatted = Object.keys(data).map((key) => ({ key, value: data[key] }));
const sorted = !sort ? formatted : formatted.sort((a, b) => b.value - a.value);
const maxValue = Math.max(...sorted.map((item) => item.value));
const maxKeyNameLength = Math.max(...sorted.map((item) => item.key.length));
return sorted.map((item) => {
const prefix = item.key + " ".repeat(maxKeyNameLength - item.key.length + 1);
const barText = ChartHelper.bar(item.value, maxValue, maxBarLength);
const suffix = showValue ? ` ${item.value}${postValue}` : "";
return prefix + barText + suffix;
}).join("\n");
};