UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

151 lines • 5.16 kB
import { cutPlain, formatJobWidgetLine, jobWidgetLineStyle } from "./tui-line.js"; export const JOB_WIDGET_TUI_KEY = "job-widgets"; const MAX_JOB_WIDGET_LINES = 3; export const DONE_LINGER_MS = 10_000; const PAD = " "; class JobWidgetAreaComponent { #style; #rows; #cache; constructor(style, rows) { this.#style = style; this.#rows = rows; } setRows(rows) { this.#rows = rows; this.#cache = undefined; } render(width) { if (this.#cache?.width === width) return this.#cache.lines; const inner = Math.max(0, width - PAD.length * 2); const lines = []; for (const entry of this.#rows.entries) { const line = formatJobWidgetLine(entry, inner, this.#style); if (line !== undefined) lines.push(`${PAD}${line}`); } const more = this.#rows.more > 0 ? cutPlain(`+${this.#rows.more} more`, inner) : ""; if (more.length > 0) lines.push(`${PAD}${this.#style.fg("muted", more)}`); this.#cache = { width, lines }; return lines; } invalidate() { this.#cache = undefined; } } export class JobWidgetArea { #setWidget; #env; #now; #doneSince = new Map(); #jobs = []; #shownKey; #component; #tui; #lingerTimer; #seenState = false; #disposed = false; constructor(options) { this.#setWidget = options.setWidget; this.#env = options.env ?? process.env; this.#now = options.now ?? Date.now; } update(state) { if (this.#disposed) return; this.#jobs = state.jobs; const now = this.#now(); // A job already done in the first state finished before this area existed: its linger is over. const doneAt = this.#seenState ? now : Number.NEGATIVE_INFINITY; this.#seenState = true; const present = new Set(); for (const job of state.jobs) { present.add(job.id); if (job.state !== "done") this.#doneSince.delete(job.id); else if (!this.#doneSince.has(job.id)) this.#doneSince.set(job.id, doneAt); } for (const id of this.#doneSince.keys()) if (!present.has(id)) this.#doneSince.delete(id); this.#redraw(); } dispose() { if (this.#disposed) return; this.#disposed = true; clearTimeout(this.#lingerTimer); this.#lingerTimer = undefined; if (this.#shownKey !== undefined) this.#setWidget(JOB_WIDGET_TUI_KEY, undefined); this.#shownKey = undefined; this.#component = undefined; this.#tui = undefined; } #lingering(entry, now) { const since = this.#doneSince.get(entry.id); return since !== undefined && now - since < DONE_LINGER_MS; } #rows(now) { const shown = [...this.#jobs] .reverse() .filter((entry) => entry.state !== "skipped" && entry.view !== undefined) .filter((entry) => entry.state !== "done" || this.#lingering(entry, now)); const active = shown.some((entry) => entry.state === "live" || entry.state === "pending" || entry.state === "done"); if (!active) return undefined; return { entries: shown.slice(0, MAX_JOB_WIDGET_LINES), more: Math.max(0, shown.length - MAX_JOB_WIDGET_LINES), }; } #redraw() { const now = this.#now(); const rows = this.#rows(now); this.#armLinger(now); if (rows === undefined) { if (this.#shownKey !== undefined) this.#setWidget(JOB_WIDGET_TUI_KEY, undefined); this.#shownKey = undefined; this.#component = undefined; this.#tui = undefined; return; } const key = JSON.stringify({ more: rows.more, rows: rows.entries.map(({ updatedAtMs: _at, ...rest }) => rest) }); if (key === this.#shownKey) return; this.#shownKey = key; if (this.#component) { this.#component.setRows(rows); this.#tui?.requestRender(); return; } this.#setWidget(JOB_WIDGET_TUI_KEY, (tui, theme) => { const component = new JobWidgetAreaComponent(jobWidgetLineStyle(theme, this.#env), rows); this.#component = component; this.#tui = tui; return component; }); } #armLinger(now) { clearTimeout(this.#lingerTimer); this.#lingerTimer = undefined; let next; for (const since of this.#doneSince.values()) { const expiry = since + DONE_LINGER_MS; if (expiry > now && (next === undefined || expiry < next)) next = expiry; } if (next === undefined) return; this.#lingerTimer = setTimeout(() => { this.#lingerTimer = undefined; this.#redraw(); }, next - now); this.#lingerTimer.unref?.(); } } //# sourceMappingURL=tui-widget.js.map