|
| 1 | +/** |
| 2 | + * ProgressBar component inlined from @four-bytes/opencode-plugin-lib/tui-components |
| 3 | + * (Apache-2.0) to avoid the broken raw-.jsx node_modules dependency. |
| 4 | + */ |
| 5 | +/** @jsxImportSource @opentui/solid */ |
| 6 | +import { createMemo } from "solid-js"; |
| 7 | + |
| 8 | +export interface ProgressBarProps { |
| 9 | + current: number; |
| 10 | + total: number; |
| 11 | + width?: number | "auto" | `${number}%`; |
| 12 | + barWidth?: number; |
| 13 | + showLabel?: boolean; |
| 14 | + rightAlign?: boolean; |
| 15 | + fillFg?: string; |
| 16 | + fillBg?: string; |
| 17 | + unfillFg?: string; |
| 18 | + unfillBg?: string; |
| 19 | +} |
| 20 | + |
| 21 | +function centerText(s: string, width: number): string { |
| 22 | + if (s.length >= width) return s.slice(0, width); |
| 23 | + const left = Math.floor((width - s.length) / 2); |
| 24 | + return " ".repeat(left) + s + " ".repeat(Math.max(0, width - s.length - left)); |
| 25 | +} |
| 26 | + |
| 27 | +function formatNum(n: number): string { |
| 28 | + return n.toLocaleString("en-US"); |
| 29 | +} |
| 30 | + |
| 31 | +export function ProgressBar(props: ProgressBarProps) { |
| 32 | + const pct = createMemo(() => (props.total > 0 ? (props.current / props.total) * 100 : 0)); |
| 33 | + const w = createMemo(() => props.barWidth ?? 10); |
| 34 | + const filledCount = createMemo(() => Math.round((props.current / props.total) * w())); |
| 35 | + const fillFg = createMemo(() => props.fillFg ?? "#fff"); |
| 36 | + const fillBg = createMemo(() => props.fillBg ?? "#4caf50"); |
| 37 | + const unfillFg = createMemo(() => props.unfillFg ?? "#888"); |
| 38 | + const unfillBg = createMemo(() => props.unfillBg ?? "#2a2a2a"); |
| 39 | + const text = createMemo(() => centerText(` ${pct().toFixed(1)}% `, w())); |
| 40 | + const showLabel = createMemo(() => props.showLabel !== false); |
| 41 | + const rightAlign = createMemo(() => props.rightAlign === true); |
| 42 | + const justify = createMemo(() => (rightAlign() ? "flex-end" : "flex-start")); |
| 43 | + |
| 44 | + return ( |
| 45 | + <box flexDirection="row" width={props.width ?? "100%"} justifyContent={justify()}> |
| 46 | + {showLabel() && <text>{`${formatNum(props.current)}/${formatNum(props.total)} `}</text>} |
| 47 | + <box flexDirection="row" justifyContent={justify()}> |
| 48 | + {Array.from(text()).map((char, i) => ( |
| 49 | + <box backgroundColor={i < filledCount() ? fillBg() : unfillBg()}> |
| 50 | + <text fg={i < filledCount() ? fillFg() : unfillFg()}>{char}</text> |
| 51 | + </box> |
| 52 | + ))} |
| 53 | + </box> |
| 54 | + </box> |
| 55 | + ); |
| 56 | +} |
0 commit comments