Files
locode/src/ui/ink/StatusBar.tsx
T
kimandClaude Sonnet 5 e6795effe1 fix: use width-stable icons in the status bar
⏱ (U+23F1) and ⏵ (U+23F5) are text-default emoji: string-width counts
them as 1 column but Windows Terminal, iTerm and others render them as
2. That makes a status-bar line drift wider than Ink's layout math
expects, so the live region below it under-erases on re-render and
leaves stranded copies of the bar in scrollback. Swap in ⏳ (a full RGI
emoji, unambiguously 2) and ▸ (a plain geometric shape, unambiguously 1).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-10 16:01:20 +09:00

144 lines
5.2 KiB
TypeScript

import { Box, Text } from "ink";
import { memo, useEffect, useState } from "react";
import { ACCENT_HEX } from "../theme.js";
import type { PermissionMode } from "../../permissions/types.js";
import type { GitInfo } from "../../utils/gitInfo.js";
const MODE_LABELS: Record<PermissionMode, string> = {
default: "default",
"auto-edit": "auto-edit",
"auto-accept": "auto-accept",
plan: "plan",
};
// Distinct per mode so cycling modes (Shift+Tab / /perm) is visibly reflected here — default and
// auto-edit previously shared "yellow", making it look like the mode hadn't changed at all.
const MODE_COLORS: Record<PermissionMode, string> = {
default: "gray",
"auto-edit": "green",
"auto-accept": "yellowBright",
plan: "cyan",
};
interface Props {
model: string;
mode: string;
permMode: PermissionMode;
cwd: string;
contextTokens: number;
contextWindow: number;
contextIsEstimate: boolean;
sessionId: string;
createdAt: string;
inputTokens: number;
outputTokens: number;
modelTimeMs: number;
gitInfo: GitInfo | null;
}
// Always a legible, visible color — never dimmed, since this is one of the most-referenced fields.
function contextColor(percent: number): string {
if (percent >= 85) return "red";
if (percent >= 60) return "yellow";
return "green";
}
function formatTokenCount(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
if (n >= 1_000) return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}K`;
return String(n);
}
function renderBar(percent: number, width = 10): string {
const filled = Math.max(0, Math.min(width, Math.round((percent / 100) * width)));
return "█".repeat(filled) + "░".repeat(width - filled);
}
// Compact — "57m", "1h23m" — distinct from HistoryItemView's formatDuration (which keeps seconds
// for the /dashboard detail view); the status bar only has room for the coarse shape.
function formatElapsed(ms: number): string {
const totalMinutes = Math.floor(ms / 60_000);
if (totalMinutes < 1) return `${Math.floor(ms / 1000)}s`;
const h = Math.floor(totalMinutes / 60);
const m = totalMinutes % 60;
return h > 0 ? `${h}h${m}m` : `${m}m`;
}
export const StatusBar = memo(function StatusBar({
model,
mode,
permMode,
cwd,
contextTokens,
contextWindow,
contextIsEstimate,
sessionId,
createdAt,
inputTokens,
outputTokens,
modelTimeMs,
gitInfo,
}: Props) {
// Ticks every 30s purely to keep "elapsed"/burn-rate live while otherwise idle — no other state
// here changes on its own, so without this the clock would freeze between messages.
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 30_000);
return () => clearInterval(id);
}, []);
const shortCwd = cwd.split(/[/\\]/).pop() ?? cwd;
const percent = Math.round((contextTokens / contextWindow) * 100);
const approx = contextIsEstimate ? "~" : "";
const elapsedMs = Math.max(0, now - new Date(createdAt).getTime());
const elapsedMinutes = elapsedMs / 60_000;
// Use model time (wall-clock time spent waiting on backend responses) rather than session
// elapsed time. A session open for 30 minutes with only 2 minutes of actual model interaction
// should show the real throughput, not a diluted rate that makes the model look slow.
const modelMinutes = modelTimeMs / 60_000;
const burnRate = modelMinutes >= 0.1 ? outputTokens / modelMinutes : null;
// Icons here are deliberately either full RGI emoji (📁 🔑 ⏳ 🔥 — string-width and terminals
// agree they're 2 wide) or plain geometric shapes (◆ ▸ — agree they're 1 wide). Avoid
// text-default emoji like ⏱ U+23F1 / ⏵ U+23F5: string-width calls them 1 but many terminals
// (Windows Terminal, iTerm) render them 2, so a line drifts wider than Ink thinks and the live
// region below leaves stranded copies of itself in scrollback on every re-render.
return (
<Box flexDirection="column" width="100%" paddingX={1}>
<Box gap={1}>
<Text color={ACCENT_HEX}>
◆ {model} ({mode})
</Text>
<Text dimColor>│</Text>
<Text color={contextColor(percent)}>{renderBar(percent)}</Text>
<Text dimColor>│</Text>
<Text color={contextColor(percent)}>{percent}%</Text>
<Text dimColor>│</Text>
<Text dimColor>
{approx}
{formatTokenCount(contextTokens)}/{formatTokenCount(contextWindow)}
</Text>
</Box>
<Box gap={1}>
<Text dimColor>
📁 {shortCwd}
{gitInfo ? ` (${gitInfo.branch}${gitInfo.dirty ? "*" : ""})` : ""}
</Text>
<Text dimColor>│</Text>
<Text dimColor>🔑 {sessionId.slice(0, 8)}</Text>
<Text dimColor>│</Text>
<Text dimColor>⏳ {formatElapsed(elapsedMs)}</Text>
<Text dimColor>│</Text>
<Text dimColor>🔥 {burnRate === null ? "—" : `${formatTokenCount(burnRate)}/min`}</Text>
</Box>
<Box gap={1} justifyContent="space-between">
<Text color={MODE_COLORS[permMode]}>
▸▸ {MODE_LABELS[permMode]} (shift+tab to cycle)
</Text>
<Text color="yellow">/export · /compact · /help · /exit</Text>
</Box>
</Box>
);
});