Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 5x 5x 1x 3x 1x 5x 4x 4x 2x 2x 2x 2x 4x 4x 3x 3x 2x 1x 1x 1x 4x 4x 4x 4x 2x 2x 14x 14x | /**
* Shared transient status renderers for command-level CLI orchestration.
*
* Terminal output design guide: `specs/terminal-output.md`
*/
import React, { useEffect, useState } from 'react';
import { render, Box, Text } from 'ink';
import type { OutputMode } from './tty.js';
import { Verbosity } from './verbosity.js';
import { ICON_CHECK, ICON_ERROR, ICON_PENDING, SPINNER_FRAMES } from './icons.js';
import { formatDuration } from './formatters.js';
import { runPool } from '../../utils/index.js';
interface LiveStatusProps {
message: string;
detail?: string;
startedAt: number;
}
function Spinner(): React.ReactElement {
const [frame, setFrame] = useState(0);
useEffect(() => {
const timer = setInterval(() => {
setFrame((current) => (current + 1) % SPINNER_FRAMES.length);
}, 80);
return () => clearInterval(timer);
}, []);
return <Text color="yellow">{SPINNER_FRAMES[frame]}</Text>;
}
function LiveStatus({ message, detail, startedAt }: LiveStatusProps): React.ReactElement {
const [elapsed, setElapsed] = useState(0);
useEffect(() => {
const timer = setInterval(() => {
setElapsed(Date.now() - startedAt);
}, 250);
return () => clearInterval(timer);
}, [startedAt]);
return (
<Box flexDirection="column">
<Box>
<Spinner />
<Text> {message}</Text>
<Text dimColor> [{formatDuration(elapsed)}]</Text>
</Box>
{detail ? <Text dimColor>{detail}</Text> : null}
</Box>
);
}
interface LiveStatusListItemState {
label: string;
status: 'pending' | 'running' | 'done' | 'error';
detail?: string;
startedAt?: number;
durationMs?: number;
showRunningDuration?: boolean;
error?: string;
}
function LiveStatusListItem(args: {
item: LiveStatusListItemState;
now: number;
}): React.ReactElement {
const { item, now } = args;
if (item.status === 'done') {
return (
<Text>
<Text color="green">{ICON_CHECK}</Text> {item.label}
{item.detail ? <Text dimColor> {item.detail}</Text> : null}
{item.durationMs !== undefined ? <Text dimColor> [{formatDuration(item.durationMs)}]</Text> : null}
</Text>
);
}
Iif (item.status === 'error') {
return (
<Text>
<Text color="red">{ICON_ERROR}</Text> {item.label}
{item.durationMs !== undefined ? <Text dimColor> [{formatDuration(item.durationMs)}]</Text> : null}
{item.error ? <Text dimColor> {item.error}</Text> : null}
</Text>
);
}
Eif (item.status === 'running') {
return (
<Box>
<Spinner />
<Text> {item.label}</Text>
{item.showRunningDuration && item.startedAt !== undefined
? <Text dimColor> [{formatDuration(Math.max(0, now - item.startedAt))}]</Text>
: null}
</Box>
);
}
return (
<Text dimColor>
{ICON_PENDING} {item.label}
</Text>
);
}
/** Render the transient list UI used for concurrent build progress. */
export function LiveStatusList({ items }: { items: LiveStatusListItemState[] }): React.ReactElement {
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
const hasRunningItems = items.some((item) => item.status === 'running' && item.showRunningDuration);
if (!hasRunningItems) {
return undefined;
}
const timer = setInterval(() => {
setNow(Date.now());
}, 250);
return () => clearInterval(timer);
}, [items]);
const completed = items.filter((item) => item.status === 'done' || item.status === 'error');
const running = items.filter((item) => item.status === 'running');
const pending = items.filter((item) => item.status === 'pending');
return (
<Box flexDirection="column">
{completed.map((item, index) => (
<LiveStatusListItem key={`done:${index}:${item.label}`} item={item} now={now} />
))}
{running.map((item, index) => (
<LiveStatusListItem key={`running:${index}:${item.label}`} item={item} now={now} />
))}
{pending.map((item, index) => (
<LiveStatusListItem key={`pending:${index}:${item.label}`} item={item} now={now} />
))}
</Box>
);
}
export async function runWithLiveStatus<T>(args: {
mode: OutputMode;
verbosity: Verbosity;
message: string;
detail?: string;
task: (controls: { setDetail: (detail: string | undefined) => void }) => Promise<T>;
}): Promise<T> {
Eif (!args.mode.isTTY || args.verbosity === Verbosity.Quiet) {
return args.task({ setDetail: () => undefined });
}
const startedAt = Date.now();
let detail = args.detail;
const { rerender, clear, unmount } = render(
<LiveStatus message={args.message} detail={detail} startedAt={startedAt} />,
{ stdout: process.stderr },
);
let updatePending = false;
let unmounted = false;
const updateUI = () => {
if (updatePending || unmounted) return;
updatePending = true;
setImmediate(() => {
updatePending = false;
if (unmounted) return;
rerender(<LiveStatus message={args.message} detail={detail} startedAt={startedAt} />);
});
};
try {
return await args.task({
setDetail: (nextDetail) => {
if (detail === nextDetail) {
return;
}
detail = nextDetail;
updateUI();
},
});
} finally {
await new Promise((resolve) => setImmediate(resolve));
unmounted = true;
clear();
unmount();
}
}
/**
* Run concurrent work items with a live TTY list that mirrors Warden's normal
* multi-item progress rendering instead of collapsing active work into one line.
*/
export async function runWithLiveStatusList<TItem, TResult>(args: {
mode: OutputMode;
verbosity: Verbosity;
items: TItem[];
concurrency: number;
getLabel: (item: TItem, index: number) => string;
task: (item: TItem, index: number) => Promise<TResult>;
getDoneDetail?: (result: TResult, item: TItem, index: number) => string | undefined;
showRunningDuration?: (item: TItem, index: number) => boolean;
showDoneDuration?: (result: TResult, item: TItem, index: number) => boolean;
shouldAbort?: () => boolean;
}): Promise<TResult[]> {
const run = () => runPool(args.items, args.concurrency, args.task, {
shouldAbort: args.shouldAbort,
});
if (!args.mode.isTTY || args.verbosity === Verbosity.Quiet || args.items.length === 0) {
return run();
}
const itemStates: LiveStatusListItemState[] = args.items.map((item, index) => ({
label: args.getLabel(item, index),
status: 'pending',
}));
const { rerender, clear, unmount } = render(
<LiveStatusList items={itemStates} />,
{ stdout: process.stderr },
);
let updatePending = false;
let unmounted = false;
const updateUI = () => {
if (updatePending || unmounted) return;
updatePending = true;
setImmediate(() => {
updatePending = false;
if (unmounted) return;
rerender(<LiveStatusList items={[...itemStates]} />);
});
};
try {
return await runPool(args.items, args.concurrency, async (item, index) => {
const itemState = itemStates[index];
if (itemState) {
itemState.status = 'running';
itemState.startedAt = Date.now();
itemState.showRunningDuration = args.showRunningDuration?.(item, index) ?? false;
updateUI();
}
const startedAt = itemState?.startedAt ?? Date.now();
try {
const result = await args.task(item, index);
if (itemState) {
itemState.status = 'done';
itemState.detail = args.getDoneDetail?.(result, item, index);
itemState.durationMs = args.showDoneDuration?.(result, item, index) === false
? undefined
: Date.now() - startedAt;
updateUI();
}
return result;
} catch (error) {
if (itemState) {
itemState.status = 'error';
itemState.durationMs = Date.now() - startedAt;
itemState.error = error instanceof Error ? error.message : String(error);
updateUI();
}
throw error;
}
}, { shouldAbort: args.shouldAbort });
} finally {
await new Promise((resolve) => setImmediate(resolve));
unmounted = true;
clear();
unmount();
}
}
|