@@ -2,6 +2,7 @@ import * as c from "yoctocolors";
22 import { terminal } from "./terminal-io.ts";
33
44 const ANSI_ESCAPE = "\u001b";
5+const STATUS_SEP = c.dim(" · ");
56
67 function stripAnsi(s: string): string {
78 if (!s.includes(ANSI_ESCAPE)) return s;
@@ -11,20 +12,10 @@ function stripAnsi(s: string): string {
1112 .join("");
1213 }
1314
14-function truncateAnsi(s: string, maxLen: number): string {
15- const plain = stripAnsi(s);
16- if (plain.length <= maxLen) return s;
17- let visible = 0;
18- let i = 0;
19- while (i < s.length && visible < maxLen - 1) {
20- if (s[i] === "\x1B") {
21- while (i < s.length && s[i] !== "m") i++;
22- } else {
23- visible++;
24- }
25- i++;
26- }
27- return s.slice(0, i) + c.dim("…");
15+function truncatePlainText(value: string, maxLen: number): string {
16+ if (value.length <= maxLen) return value;
17+ if (maxLen <= 1) return "…";
18+ return `${value.slice(0, maxLen - 1)}…`;
2819 }
2920
3021 function fmtTokens(n: number): string {
@@ -32,6 +23,55 @@ function fmtTokens(n: number): string {
3223 return String(n);
3324 }
3425
26+function buildContextSegment(opts: {
27+ contextTokens: number;
28+ contextWindow: number | null;
29+}): string | null {
30+ if (opts.contextTokens <= 0) return null;
31+ if (opts.contextWindow === null) {
32+ return c.dim(`ctx ${fmtTokens(opts.contextTokens)}`);
33+ }
34+
35+ const pct = Math.round((opts.contextTokens / opts.contextWindow) * 100);
36+ const pctStr = `${pct}%`;
37+ const pctColored =
38+ pct >= 90 ? c.red(pctStr) : pct >= 75 ? c.yellow(pctStr) : c.dim(pctStr);
39+ return (
40+ c.dim(
41+ `ctx ${fmtTokens(opts.contextTokens)}/${fmtTokens(opts.contextWindow)} `,
42+ ) + pctColored
43+ );
44+}
45+
46+function renderStatusLine(segments: string[]): string {
47+ return segments.join(STATUS_SEP);
48+}
49+
50+function fitStatusSegments(
51+ required: string[],
52+ optional: string[],
53+ cols: number,
54+): string {
55+ const fittedOptional = [...optional];
56+ let line = renderStatusLine([...required, ...fittedOptional]);
57+
58+ while (stripAnsi(line).length > cols && fittedOptional.length > 0) {
59+ fittedOptional.pop();
60+ line = renderStatusLine([...required, ...fittedOptional]);
61+ }
62+
63+ if (stripAnsi(line).length <= cols) return line;
64+
65+ const plainRequired = required.map((segment) => stripAnsi(segment));
66+ const sepLen = stripAnsi(STATUS_SEP).length;
67+ const fixedPrefix = plainRequired[0] ?? "";
68+ if (plainRequired.length <= 1) return truncatePlainText(fixedPrefix, cols);
69+
70+ const maxTailLen = Math.max(8, cols - fixedPrefix.length - sepLen);
71+ const truncatedTail = truncatePlainText(plainRequired[1] ?? "", maxTailLen);
72+ return `${required[0]}${STATUS_SEP}${c.dim(truncatedTail)}`;
73+}
74+
3575 export function renderStatusBar(opts: {
3676 model: string;
3777 provider: string;
@@ -46,53 +86,34 @@ export function renderStatusBar(opts: {
4686 activeAgent?: string | null;
4787 showReasoning?: boolean;
4888 }): void {
49- const cols = terminal.stdoutColumns || 80;
89+ const cols = Math.max(20, terminal.stdoutColumns || 80);
90+ const required = [c.cyan(opts.model), c.dim(opts.cwd)];
91+ const optional: string[] = [];
5092
51- // Build segments from right priority (rightmost items drop first)
52- const left: string[] = [c.cyan(opts.model)];
53- if (opts.thinkingEffort) left.push(c.dim(`✦ ${opts.thinkingEffort}`));
54- if (opts.showReasoning) left.push(c.dim("🤔"));
55- if (opts.provider && opts.provider !== "zen") left.push(c.dim(opts.provider));
56- left.push(c.dim(opts.sessionId.slice(0, 8)));
57- if (opts.activeAgent) left.push(c.green(`@${opts.activeAgent}`));
93+ if (opts.activeAgent) optional.push(c.green(`@${opts.activeAgent}`));
94+ if (opts.thinkingEffort) optional.push(c.dim(`✦ ${opts.thinkingEffort}`));
95+ if (opts.showReasoning) optional.push(c.dim("reasoning"));
96+ if (opts.provider && opts.provider !== "zen") {
97+ optional.push(c.dim(opts.provider));
98+ }
99+ if (opts.gitBranch) optional.push(c.dim(`⎇ ${opts.gitBranch}`));
58100
59- const right: string[] = [];
60101 if (opts.inputTokens > 0 || opts.outputTokens > 0) {
61- right.push(
102+ optional.push(
62103 c.dim(
63- `↑ ${fmtTokens(opts.inputTokens)} ↓ ${fmtTokens(opts.outputTokens)}`,
104+ `tok ${fmtTokens(opts.inputTokens)}/${fmtTokens(opts.outputTokens)}`,
64105 ),
65106 );
66107 }
67- if (opts.contextTokens > 0) {
68- const ctxRaw = fmtTokens(opts.contextTokens);
69- if (opts.contextWindow !== null) {
70- const pct = Math.round((opts.contextTokens / opts.contextWindow) * 100);
71- const ctxMax = fmtTokens(opts.contextWindow);
72- const pctStr = `${pct}%`;
73- const colored =
74- pct >= 90
75- ? c.red(pctStr)
76- : pct >= 75
77- ? c.yellow(pctStr)
78- : c.dim(pctStr);
79- right.push(c.dim(`ctx ${ctxRaw}/${ctxMax} `) + colored);
80- } else {
81- right.push(c.dim(`ctx ${ctxRaw}`));
82- }
83- }
84- if (opts.gitBranch) right.push(c.dim(`⎇ ${opts.gitBranch}`));
85-
86- const cwdDisplay = opts.cwd;
87-
88- // Assemble: left · cwd · right (respects terminal width)
89- const middle = c.dim(cwdDisplay);
90- const sep = c.dim(" ");
91- const full = [...left, middle, ...right.reverse()].join(sep);
92108
93- const visible = stripAnsi(full);
109+ const contextSegment = buildContextSegment({
110+ contextTokens: opts.contextTokens,
111+ contextWindow: opts.contextWindow,
112+ });
113+ if (contextSegment) optional.push(contextSegment);
94114
95- const out = visible.length > cols ? truncateAnsi(full, cols - 1) : full;
115+ optional.push(c.dim(`#${opts.sessionId.slice(0, 8)}`));
96116
117+ const out = fitStatusSegments(required, optional, cols);
97118 terminal.stdoutWrite(`${out}\n`);
98119 }