Files
oh-my-pi/packages/coding-agent/src/tui/utils.ts
T
can1357 cc90ed37f8 refactor: extracted padding logic into reusable utility function with optimized buffer allocation
- Extracted manual space padding logic into a reusable `padding()` utility function across all UI components and utilities.
- Optimized padding operations by introducing a pre-allocated 512-space buffer in the `padding()` function to reduce repeated string allocations.
- Updated all imports across 30 files to use the new `padding()` function from pi-tui instead of inline `' '.repeat()` calls.
- Renamed local variables from `padding` to `pad`, `padSize`, `indent`, or `linePad` to avoid naming conflicts with the imported `padding()` function.
- Exported the `padding()` utility function from the tui package index for public use.
2026-01-31 13:36:54 +01:00

49 lines
2.0 KiB
TypeScript

/**
* Shared helpers for tool-rendered UI components.
*/
import { padding, truncateToWidth as truncateToWidthBase, visibleWidth } from "@oh-my-pi/pi-tui";
import type { Theme, ThemeBg } from "../modes/theme/theme";
import type { IconType, State } from "./types";
export function buildTreePrefix(ancestors: boolean[], theme: Theme): string {
return ancestors.map(hasNext => (hasNext ? `${theme.tree.vertical} ` : " ")).join("");
}
export function getTreeBranch(isLast: boolean, theme: Theme): string {
return isLast ? theme.tree.last : theme.tree.branch;
}
export function getTreeContinuePrefix(isLast: boolean, theme: Theme): string {
return isLast ? " " : `${theme.tree.vertical} `;
}
export function truncateToWidth(text: string, width: number, ellipsis: string): string {
return truncateToWidthBase(text, width, ellipsis);
}
export function padToWidth(text: string, width: number, bgFn?: (s: string) => string): string {
if (width <= 0) return bgFn ? bgFn(text) : text;
const paddingNeeded = Math.max(0, width - visibleWidth(text));
const padded = paddingNeeded > 0 ? text + padding(paddingNeeded) : text;
return bgFn ? bgFn(padded) : padded;
}
export function getStateBgColor(state: State): ThemeBg {
if (state === "success") return "toolSuccessBg";
if (state === "error") return "toolErrorBg";
return "toolPendingBg";
}
export function getStateIcon(icon: IconType, theme: Theme, spinnerFrame?: number): string {
if (icon === "success") return theme.styledSymbol("status.success", "success");
if (icon === "error") return theme.styledSymbol("status.error", "error");
if (icon === "warning") return theme.styledSymbol("status.warning", "warning");
if (icon === "info") return theme.styledSymbol("status.info", "accent");
if (icon === "pending") return theme.styledSymbol("status.pending", "accent");
if (spinnerFrame !== undefined) {
const frames = theme.spinnerFrames;
return frames[spinnerFrame % frames.length];
}
return theme.styledSymbol("status.running", "accent");
}