Documentation
¶
Overview ¶
Package textutil holds small string helpers shared by tools and the TUI.
Package textutil holds small string helpers shared by tools and the TUI.
Index ¶
- func CollapseNewlines(s string, max int) string
- func Ellipsize(s string, n int) string
- func EstimateTokens(text string) int
- func ExpandBraces(glob string) []string
- func GlobToRegex(glob string) string
- func HTMLToText(src string) string
- func HeadTail(s string, head, tail int) (h, t string)
- func SanitizeTerminal(s string) string
- func StripANSI(s string) string
- func Tail(s string, n int) string
- func TrimPartialRune(s string) string
- func TruncateUTF8(s string, n int) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollapseNewlines ¶ added in v1.3.7
CollapseNewlines squeezes runs of more than max consecutive newlines in s down to max. Lines themselves are left as they are.
func EstimateTokens ¶ added in v1.3.7
EstimateTokens is about how many tokens text is (four characters each), the estimate used wherever no tokenizer is at hand.
func ExpandBraces ¶ added in v0.9.1
ExpandBraces makes one glob of each {a,b} alternative ("**/*.{pem,key}": "**/*.pem" and "**/*.key"), nested and repeated braces too. A brace without a comma, or unclosed, stays as written; more than 64 globs is none expanded, the glob as written.
func GlobToRegex ¶
GlobToRegex converts a slash-separated glob to an unanchored regex body using only syntax understood by both Go and the macOS sandbox profile language: *, ** and ? as wildcards, and a class, [abc], [a-z] or [!abc] (never a slash). Braces aren't alternatives here: ExpandBraces makes one glob of each first. Anything else is quoted, so the result always compiles.
func HTMLToText ¶ added in v0.7.0
HTMLToText extracts readable text from HTML: scripts and styles are dropped, block elements become line breaks, headings and list items keep light Markdown markers, and links keep their absolute targets.
func HeadTail ¶ added in v1.3.7
HeadTail splits a long s into its first head and last tail bytes, each cut on a rune boundary. When s fits in head+tail it is returned whole as head with an empty tail. len(s)-len(h)-len(t) is how much was left out.
func SanitizeTerminal ¶
SanitizeTerminal strips control characters that a terminal would interpret (ESC sequences, BEL, backspace, carriage return, C1 controls) so untrusted text from models, files, or tool output cannot rewrite the screen, change the window title, or write to the clipboard via OSC 52. Newlines and tabs are kept. Invalid UTF-8 is replaced with U+FFFD.
func StripANSI ¶ added in v1.3.7
StripANSI removes terminal escape sequences from s, keeping the text they decorate. Unlike SanitizeTerminal it drops the whole sequence, not just the ESC byte, so no "[31m" is left behind.
func Tail ¶ added in v0.8.3
Tail is the end of s, at most n bytes on a rune boundary, after "…" when cut: where a command's output says what went wrong.
func TrimPartialRune ¶
TrimPartialRune drops an incomplete multi-byte rune from the end of s, as left behind when a byte stream is cut at an arbitrary offset.
func TruncateUTF8 ¶
TruncateUTF8 returns s cut to at most n bytes without splitting a multi-byte rune.
Types ¶
This section is empty.