@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
111 lines • 4.68 kB
JavaScript
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
function segmentsWidth(segments, separator) {
if (segments.length === 0)
return 0;
let total = visibleWidth(separator) * (segments.length - 1);
for (const segment of segments)
total += visibleWidth(segment.plain);
return total;
}
function fits(left, right, input) {
return segmentsWidth(left, input.separator) + input.minPadding + visibleWidth(right.plain) <= input.width;
}
function pwdBudget(rest, right, input) {
return (input.width -
input.minPadding -
visibleWidth(right.plain) -
visibleWidth(input.separator) -
segmentsWidth(rest, input.separator));
}
/**
* Elide the head of a string so it fits maxWidth, keeping the tail (the most
* identifying part of a path) and marking the cut with a single "…".
*/
export function elideHead(text, maxWidth) {
if (maxWidth <= 0)
return "";
if (visibleWidth(text) <= maxWidth)
return text;
if (maxWidth === 1)
return "…";
const budget = maxWidth - visibleWidth("…");
const chars = [...text];
const kept = [];
let used = 0;
for (let i = chars.length - 1; i >= 0; i--) {
const char = chars[i];
if (char === undefined)
break;
const charWidth = visibleWidth(char);
if (used + charWidth > budget)
break;
kept.unshift(char);
used += charWidth;
}
return `…${kept.join("")}`;
}
/**
* Pick the richest layout that fits the width. The model label is pinned to
* the right edge and the anchor/tail segments stay visible. The pwd shrinks
* from its head before middle stats yield from the right; the right label
* steps down its own ladder (`right.forms`) one form at a time before that.
*/
export function planFooterLayout(input) {
const { anchor, middle, tail, right } = input;
const pwd = anchor[input.pwdIndex];
if (pwd === undefined)
throw new RangeError(`pwdIndex ${input.pwdIndex} is outside the footer anchor`);
const anchorRest = anchor.filter((_, index) => index !== input.pwdIndex);
const preferPwdElision = visibleWidth(pwd.plain) > Math.floor(input.width / 3);
const floorForm = right.forms.length - 1;
const planForRight = (rightSegment, rightForm) => {
const fit = rightSegment.fitsWith ?? "pwd-elision";
const candidates = [
{ keptMiddleCount: middle.length, showMarker: false },
...Array.from({ length: middle.length }, (_, index) => ({
keptMiddleCount: middle.length - index - 1,
showMarker: true,
})),
{ keptMiddleCount: 0, showMarker: false },
];
for (const candidate of candidates) {
const retainedMiddle = middle.slice(0, candidate.keptMiddleCount);
const marker = candidate.showMarker ? [input.ellipsisMarker] : [];
const left = [...anchor, ...retainedMiddle, ...marker, tail];
if (fits(left, rightSegment, input)) {
if (candidate.keptMiddleCount === middle.length) {
return { kind: "full", rightForm };
}
return { kind: "middle-elided", ...candidate, rightForm };
}
if (fit === "everything")
return undefined;
const emptyMiddle = candidate.keptMiddleCount === 0 && !candidate.showMarker;
if (preferPwdElision || (fit === "pwd-elision" && emptyMiddle)) {
const budget = pwdBudget([...anchorRest, ...retainedMiddle, ...marker, tail], rightSegment, input);
if (budget >= 2) {
return {
kind: "pwd-elided",
pwdPlain: elideHead(pwd.plain, budget),
...candidate,
rightForm,
};
}
}
}
return undefined;
};
for (const [rightForm, segment] of right.forms.entries()) {
const plan = planForRight(segment, rightForm);
if (plan !== undefined)
return plan;
}
const floor = right.forms[floorForm] ?? right.forms[0];
const leftBudget = input.width - input.minPadding - visibleWidth(floor.plain);
if (leftBudget >= 1) {
const allLeftPlain = [...anchor, tail].map((segment) => segment.plain).join(input.separator);
return { kind: "left-elided", leftPlain: elideHead(allLeftPlain, leftBudget) };
}
return { kind: "right-truncated", rightPlain: truncateToWidth(floor.plain, input.width, "") };
}
//# sourceMappingURL=footer-layout.js.map