/** @jsxRuntime automatic */ /** @jsxImportSource @alchemy.run/sigil */ import { isValidElement, useSyncExternalStore } from "@alchemy.run/sigil/react"; import type { ReactNode } from "react"; import { theme } from "../../../Util/Theme.ts"; import { useCliEnvironment, useGlyphs } from "./Environment.tsx"; import { SpinnerGlyph } from "./Feedback.tsx"; import { Box, Row, Stack } from "./Layout.tsx"; import { ProgressBar } from "./ProgressBar.tsx"; import { Text } from "./Typography.tsx"; export interface TaskRowProps { /** Status glyph. Ignored while `spinning`; defaults to the bullet glyph. */ readonly icon?: string; /** Color for the glyph (and the spinner while `spinning`). */ readonly iconColor?: string; /** Render an animated spinner frame in the glyph slot. */ readonly spinning?: boolean; readonly label: ReactNode; /** Muted annotation directly after the label. */ readonly detail?: ReactNode; /** * How a detail longer than the row is fitted: cut to one line (progress * messages), or wrapped within its own column (errors worth reading whole). * Either way the glyph, label and trailing cells keep their full width. * @default "truncate" */ readonly detailOverflow?: "truncate" | "wrap"; /** Indentation in 2-space units. */ readonly depth?: number; /** Extra trailing cells (status labels, chips). */ readonly children?: ReactNode; } /** * The one status row shape shared by TaskTree and the plan/apply views: * glyph-or-spinner, bold label, muted detail, then any trailing cells. */ export function TaskRow({ icon, iconColor, spinning = false, label, detail, detailOverflow = "truncate", depth = 0, children, }: TaskRowProps) { const glyphs = useGlyphs(); const { columns } = useCliEnvironment(); // A failure message keeps a column of its own beside a label that has to // truncate: as wide as it needs, but at most a share of the row, and never // more than the glyph, trailing cells and a sliver of label leave. With no // room for that column, it moves to its own line under the row. const row = columns - depth * theme.space.indent; const trailing = cellsWidth(children); const free = row - 2 - 1 - (trailing > 0 ? trailing + 1 : 0) - labelMinWidth; const wrap = detail !== undefined && detailOverflow === "wrap"; const below = wrap && free < detailMinColumn; const detailMinWidth = wrap ? Math.min(oneLine(textOf(detail)).length + 2, Math.floor(row * failureShare), free) : 0; const detailText = ( · {typeof detail === "string" && !wrap ? oneLine(detail) : detail} ); // Only the detail gives way when the row is too narrow, then the label; // letting every cell shrink wrapped the label and split the status word. const line = ( {spinning ? ( ) : ( {icon ?? glyphs.bullet} )} {/* The label gives way only once the detail is gone (the detail's huge shrink factor takes the overflow first), so a long name still leaves the trailing status on screen. */} {label} {detail === undefined || below ? null : ( {detailText} )} {children} ); return below ? ( {line} {detailText} ) : ( line ); } /** Progress messages can carry several lines of tool output; keep them on one. */ const oneLine = (text: string) => text.replaceAll(/\s*\r?\n\s*/g, " ").trim(); /** Share of the row a wrapped failure detail keeps when the label has to truncate. */ const failureShare = 0.4; /** Cells a truncated label keeps beside a failure (`W…`). */ const labelMinWidth = 2; /** Narrowest failure column worth keeping beside the row (`· fail…`). */ const detailMinColumn = 8; /** Width of the trailing cells, with the 1-cell gap between them. */ const cellsWidth = (cells: ReactNode): number => { const widths = (Array.isArray(cells) ? cells : [cells]) .filter((cell) => cell !== null && cell !== undefined && cell !== false) .map((cell) => textOf(cell).length); return widths.length === 0 ? 0 : widths.reduce((a, b) => a + b, 0) + widths.length - 1; }; /** The plain text of a detail, to size its column. */ const textOf = (node: ReactNode): string => { if (typeof node === "string" || typeof node === "number") return String(node); if (Array.isArray(node)) return node.map(textOf).join(""); if (isValidElement<{ readonly children?: ReactNode }>(node)) return textOf(node.props.children); return ""; }; export interface ProgressGroupRow { readonly id: string; readonly label: ReactNode; readonly completed: number; readonly total: number; readonly failed?: number; readonly detail?: ReactNode; } type ProgressGroupProps = { readonly rows: ReadonlyArray; readonly width?: number; /** Fixed label column (truncated) so the count cells align across rows. */ readonly labelWidth?: number; }; export function ProgressGroup({ rows, width = 20, labelWidth }: ProgressGroupProps) { return ( {rows.map((row) => { const complete = row.total > 0 && row.completed >= row.total; const variant = row.failed ? "error" : complete ? "success" : "info"; return ( {labelWidth === undefined ? ( {row.label} ) : ( {row.label} )} {row.completed}/{row.total} {row.failed ? ({row.failed} failed) : null} {row.detail === undefined ? null : {row.detail}} ); })} ); } export class LiveStore { private value: Value; private readonly listeners = new Set<() => void>(); constructor(initial: Value) { this.value = initial; } readonly subscribe = (listener: () => void) => { this.listeners.add(listener); return () => this.listeners.delete(listener); }; readonly snapshot = () => this.value; readonly set = (value: Value) => { this.value = value; for (const listener of this.listeners) listener(); }; readonly update = (f: (value: Value) => Value) => this.set(f(this.value)); } export const useLiveStore = (store: LiveStore): Value => useSyncExternalStore(store.subscribe, store.snapshot);