Documentation
¶
Overview ¶
Package tuiutil hosts width-aware text helpers for fixed-width TUI dashboards: ANSI/control-char stripping, display-width-based truncation and padding, and a compact duration formatter.
Index ¶
- func FormatDuration(d time.Duration) string
- func FprintfTerminalText(w io.Writer, format string, args ...any)
- func PadDisplayWidth(s string, width int) string
- func PadDisplayWidthWith(s string, width int, pad string) string
- func SanitizeDisplayLine(s string) string
- func SanitizeDisplayText(s string) string
- func SanitizeTerminalLabel(s string) string
- func SanitizeTerminalText(s string) string
- func StripANSI(s string) string
- func TruncateDisplayWidth(s string, width int) string
- func WrapDisplayWidth(s string, width int) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatDuration ¶
FormatDuration renders a time.Duration in compact form for TUI dashboards: "523ms" / "8.4s" / "1m42s".
func FprintfTerminalText ¶ added in v0.13.5
FprintfTerminalText formats like fmt.Fprintf and writes the result through SanitizeTerminalText. It is for messages a command prints itself in place of an error it would otherwise have returned to main.go, which sanitizes those: a self-printed message naming a stored session ID, a path or server text needs the same filter. Line breaks and tabs survive; escape sequences, other control runes and bidi controls do not. Styled output must not go through it, since the styling escapes would be stripped too.
func PadDisplayWidth ¶
PadDisplayWidth truncates or right-pads s with spaces so its display width is exactly width cells (ANSI-aware).
func PadDisplayWidthWith ¶
PadDisplayWidthWith truncates or right-pads s with the pad string so its display width is exactly width cells. Pad strings whose display width is not 1 fall back to space padding to keep alignment predictable.
func SanitizeDisplayLine ¶ added in v0.13.0
SanitizeDisplayLine is SanitizeDisplayText for server text or stored metadata shown inline in a one-line message: it also drops bidi controls (U+202A–U+202E, U+2066–U+2069, and the other Bidi_Control runes), which are format characters rather than controls and could visually reorder the line. It keeps emoji sequences intact, so it suits prompts as well as labels such as session IDs, agents, models and branch names. Use it at the text rendering boundary only: JSON output escapes control characters itself and must keep the stored value.
func SanitizeDisplayText ¶
SanitizeDisplayText strips ANSI escapes and control characters so the result is safe to render in a single-line table cell. Newlines and tabs collapse to a single space; carriage returns and other control runes are dropped entirely.
func SanitizeTerminalLabel ¶ added in v0.11.0
SanitizeTerminalLabel is SanitizeTerminalText for width-aligned list labels: it additionally drops emoji skin-tone modifiers, zero-width joiners, and variation selectors, multi-codepoint sequences that confuse terminal width calculations. That normalization visibly mangles emoji in prose, so it is kept out of the general filter and applied only where a fixed-width line is actually being filled.
func SanitizeTerminalText ¶ added in v0.11.0
SanitizeTerminalText strips ANSI escape sequences and non-printable runes from multi-line terminal output while preserving tabs and newlines. Unlike SanitizeDisplayText, which flattens text into a single table cell, this keeps line structure. It is the one filter for free-form session content (prompts, transcript text, summary bodies) printed to a terminal, so escape sequences carried in agent-influenced content cannot drive the reader's terminal.
Emoji sequences are deliberately left intact: this filter serves prose, where nothing is being width-aligned, and stripping joiners turns a family emoji into three separate people. SanitizeTerminalLabel is the variant for width-aligned labels.
func TruncateDisplayWidth ¶
TruncateDisplayWidth shortens s so its display width is at most width cells, appending "…" as an ellipsis when truncation happens. Width 0 or less returns the empty string; width 1 truncates without an ellipsis since the ellipsis would consume the whole budget.
func WrapDisplayWidth ¶
WrapDisplayWidth wraps s to lines no wider than width display cells. Embedded '\n' characters are honored as paragraph boundaries: each paragraph is sanitized (ANSI/control stripped) and wrapped independently. A paragraph that wraps to nothing still contributes an empty line, preserving blank-line structure between paragraphs.
Trailing newlines are stripped before splitting so "text\n" yields a single line, not a phantom blank tail.
Returns nil for width <= 0 or input that is empty (or only newlines).
Types ¶
This section is empty.