UNPKG

pkg-stats

Version:
34 lines (33 loc) 1.61 kB
import chalk from 'chalk'; import { getColors } from './colors.js'; import { formatDownloads } from './format.js'; export function printChart(items, options) { const maxLabelLength = Math.max(...items.map((item) => item.label.length)); const maxValue = Math.max(...items.map((item) => item.value)); const maxValueLength = formatDownloads(maxValue, maxValue).length; const maxExtraLength = Math.max(...items.map((item) => item.extra?.length ?? 0)); const colors = getColors(items.length, options.colorScheme); const indent = options.indent ?? 0; const chartWidth = getTerminalWidth() - indent - maxLabelLength - maxValueLength - maxExtraLength - 5; items.forEach((item, i) => { const color = chalk.hex(colors[i]); const label = ' '.repeat(indent) + item.label.padStart(maxLabelLength); const bar = formatBar(item.value / maxValue, { width: clamp(chartWidth, 30, 60) }); const value = formatDownloads(item.value, maxValue).padStart(maxValueLength); const extra = item.extra ? chalk.dim(` ${item.extra}`.padStart(maxExtraLength + 1)) : ''; console.log(`${label} ${color(bar)} ${color(value)}${extra}`); }); } export function formatBar(value, { width = 50 } = {}) { const filledChars = Math.round(value * width); if (filledChars === 0) { return '▏' + ' '.repeat(width - 1); } return '█'.repeat(filledChars) + ' '.repeat(width - filledChars); } export function getTerminalWidth() { return process.stdout.columns; } function clamp(value, min, max) { return Math.min(Math.max(value, min), max); }