Documentation
¶
Overview ¶
Package ansi holds the SGR-aware, width-aware string helpers. Everything that knows about escape codes lives here so the rest of the TUI can treat styled strings as opaque. Bare codepoint/grapheme widths come from the shared charwidth kernel; this package adds the SGR-aware layer on top.
Go port of Ruby's lib/tui/ansi.rb.
Index ¶
- Variables
- func Bold(s string) string
- func CellSlice(s string, start, width int) string
- func CellSliceToEnd(s string, start int) string
- func Close(s string) string
- func ClusterWidth(gc string) int
- func Clusters(s string) []string
- func Color(str string, codes ...string) string
- func Composite(sgr, str string) string
- func Cyan(s string) string
- func Dim(s string) string
- func Invert(s string) string
- func Normalize(s string) string
- func Red(s string) string
- func RuneWidth(r rune) int
- func Strip(s string) string
- func VPad(s string, w int) string
- func VTrunc(s string, w int) string
- func VisLen(s string) int
- func Wrap(text string, w int) []string
- func Yellow(s string) string
Constants ¶
This section is empty.
Variables ¶
var SGR = regexp.MustCompile("\x1b\\[[0-9;]*m")
SGR matches a Select Graphic Rendition escape sequence.
Functions ¶
func CellSlice ¶
CellSlice returns the visible cell window [start, start+width) without splitting grapheme clusters. SGR styling is retained and closed at the slice boundary. If a boundary crosses a wide cluster, spaces occupy the partial cells so content after that cluster stays at its original terminal column.
func CellSliceToEnd ¶
CellSliceToEnd returns everything from start to the end of the string — the Ruby call with the width argument omitted.
func Close ¶
Close closes any styling a composed line still has open, so a row can never leak color into the one painted below it. A line with no SGR at all, or one that already ends in a reset, is returned unchanged.
func ClusterWidth ¶
func Composite ¶
Composite lays sgr (an opening SGR sequence, e.g. "\e[1m") over str, whose embedded field styling already closes with a reset. It re-injects sgr immediately after every true reset so the overlay survives each field boundary instead of being cleared by it. It does NOT append a trailing reset — the caller closes once, after any padding. An empty sgr returns str unchanged.
func Normalize ¶
Normalize returns valid UTF-8, substituting U+FFFD for invalid bytes. Subprocess output arrives as raw bytes and can end on an incomplete sequence, so every entry point normalizes defensively.
func VTrunc ¶
VTrunc truncates s to visible width w, appending a dim ellipsis. Escape codes are preserved and a reset is appended so styles cannot leak.
func VisLen ¶
VisLen is the visible display width in terminal cells: escape codes are ignored and wide/emoji graphemes count as two cells.
Types ¶
This section is empty.