ansi

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
var SGR = regexp.MustCompile("\x1b\\[[0-9;]*m")

SGR matches a Select Graphic Rendition escape sequence.

Functions

func Bold

func Bold(s string) string

func CellSlice

func CellSlice(s string, start, width int) string

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

func CellSliceToEnd(s string, start int) string

CellSliceToEnd returns everything from start to the end of the string — the Ruby call with the width argument omitted.

func Close

func Close(s string) string

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 ClusterWidth(gc string) int

func Clusters

func Clusters(s string) []string

func Color

func Color(str string, codes ...string) string

Color wraps str in an SGR sequence built from codes, closing with a reset.

func Composite

func Composite(sgr, str string) string

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 Cyan

func Cyan(s string) string

func Dim

func Dim(s string) string

func Invert

func Invert(s string) string

func Normalize

func Normalize(s string) string

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 Red

func Red(s string) string

func RuneWidth

func RuneWidth(r rune) int

RuneWidth and ClusterWidth are delegators onto the shared kernel.

func Strip

func Strip(s string) string

Strip removes every SGR sequence, leaving the visible text.

func VPad

func VPad(s string, w int) string

VPad pads s to visible width w (a no-op if it is already wider).

func VTrunc

func VTrunc(s string, w int) string

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

func VisLen(s string) int

VisLen is the visible display width in terminal cells: escape codes are ignored and wide/emoji graphemes count as two cells.

func Wrap

func Wrap(text string, w int) []string

Wrap word-wraps text to a terminal-cell width, preserving ANSI styling and grapheme clusters.

func Yellow

func Yellow(s string) string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL