/** * Progress bar rendering with sub-character precision. * * Two styles: * - continuous: █ + partial-width glyphs ▏▎▍▌▋▊▉ * - blocks: discrete partial-height glyphs ▁▂▃▄▅▆▇█ on dim background */ import type { Theme, ThemeColor } from "@mariozechner/pi-coding-agent"; export type BarStyle = "continuous" | "blocks"; /** Render a continuous progress bar with sub-character precision. */ export function renderContinuousBar(pct: number, width: number, theme: Theme, color: string): string { const clamped = Math.max(0, Math.min(100, pct)); const filledFloat = (clamped / 100) * width; const filledFull = Math.floor(filledFloat); const remainder = filledFloat - filledFull; const levels = ["▏", "▎", "▍", "▌", "▋", "▊", "▉"]; const filledStr = theme.fg(color as ThemeColor, "█".repeat(filledFull)); let partial = ""; let emptyCount = width - filledFull; if (remainder >= 0.0625 && filledFull < width) { const levelIndex = Math.max(0, Math.min(levels.length - 1, Math.round(remainder * 8) - 1)); partial = theme.fg(color as ThemeColor, levels[levelIndex]!); emptyCount = Math.max(0, emptyCount - 1); } return filledStr + partial + " ".repeat(emptyCount); } /** Render discrete block characters with dim background track. */ export function renderBlocksBar(pct: number, segments: number, theme: Theme, color: string): string { const glyphs = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]; const dimBg = fgToBgAnsi(theme.getFgAnsi("dim")); const fgColor = theme.getFgAnsi(color as ThemeColor); const reset = "\x1b[39m\x1b[49m"; const clamped = Math.max(0, Math.min(100, pct)); const filledFloat = (clamped / 100) * segments; const result: string[] = []; for (let i = 0; i < segments; i++) { const blockFill = Math.max(0, Math.min(1, filledFloat - i)); const level = Math.round(blockFill * 8); const glyph = glyphs[level]; result.push(level > 0 ? `${dimBg}${fgColor}${glyph}${reset}` : `${dimBg}${glyph}${reset}`); } return result.join(""); } function fgToBgAnsi(fgAnsi: string): string { return fgAnsi.replace("\x1b[38;", "\x1b[48;"); }