ansi

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 2 Imported by: 2 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 small, dependency-free ANSI helpers for the corners of the library where lipgloss's full \x1b[0m reset is the wrong tool — most notably cells inside a styled row (e.g. pkg/table's selected row) where an inner reset would clobber the row's background.

Fill (and RenderFill, its lipgloss-friendly form) is the general answer: it wraps content in a highlight and re-opens whatever the content's own escapes turn off, so a selected row's background spans the row. Every component that paints a selected row goes through it.

Components that own their own pane (pkg/list, pkg/tree, pkg/logview, …) render with their own per-row styling and you can keep using lipgloss inside them. Reach for this package only when downstream rendering will wrap your content in its own SGR with a background you want preserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CellColor

func CellColor(n int, text string) string

CellColor wraps text in a foreground SGR with a foreground-only reset (\x1b[39m). The 39 reset preserves any outer background, making it safe to nest inside a styled row (e.g. pkg/table's selected row) where the row's Background would otherwise be clobbered by lipgloss's full \x1b[0m reset.

n is a 256-color palette index (0–255):

0–7    standard 8-color palette       (emitted as \x1b[3Nm — 4 bytes open)
8–15   bright variants                (emitted as \x1b[9Nm — 4 bytes open)
16–255 256-color cube + grayscale     (emitted as \x1b[38;5;Nm — 8–10 bytes open)

CellColor picks the shortest valid form to keep escape overhead small.

func CellColorRGB added in v0.24.0

func CellColorRGB(r, g, b uint8, text string) string

CellColorRGB is CellColor for a 24-bit color, emitting the truecolor foreground form with the same foreground-only reset. Callers that hold a lipgloss color rather than a palette index resolve it to RGB and come here; keeping the escape construction in one place is why this lives beside CellColor instead of in the caller.

func ExtractHyperlink(s string) (url, text string, ok bool)

ExtractHyperlink pulls the url and visible text out of a string wrapped by Hyperlink. ok is false when s isn't an OSC 8 envelope, so callers can treat the cell as a bare string in that case. Useful in a screen's Update when "press o to open the focused row's URL" needs the original target — shift-click handles the common case at the terminal level, but programmatic open paths need the URL back.

func Fill added in v0.39.0

func Fill(s, open string) string

Fill wraps s in the SGR sequence open, and puts back whatever part of open s turns off along the way. A full reset (\x1b[0m) inside s re-opens all of it; a background reset (\x1b[49m) re-opens only the background; a bold reset (\x1b[22m) only the weight; and so on for each attribute open sets. The result closes with a full reset.

This is how a highlight survives the content inside it. A selected row painted with one lipgloss.Style.Render loses its background at the first cell that carries its own style, because lipgloss closes every style with \x1b[0m. Fill makes the highlight span the row whatever the row contains.

Sequences are parsed parameter by parameter, so the operands of extended colours are never mistaken for resets: the 0 in \x1b[38;2;255;0;0m is a colour component, and \x1b[0;31m turns the foreground back on after the reset rather than having Fill overwrite it. Escapes other than SGR, such as OSC 8 hyperlinks, pass through untouched.

An open with no SGR attributes returns s unchanged.

func Hyperlink(url, text string) string

Hyperlink wraps text in an OSC 8 hyperlink escape so terminals that support it (alacritty, tmux, kitty, wezterm, iTerm2) launch url on click. The wrapper is invisible to terminal width calculation and x/ansi.Cut preserves both the open and close sequences across truncation, so a long URL stays attached to its cell even when the visible label is cut to fit a narrow column.

Use this in pkg/table cells when you want shift-click / cmd-click to open the full URL regardless of column width. Bare URL text would be truncated mid-string and break the launched URL; the OSC 8 envelope decouples display text from the underlying link.

func RenderFill added in v0.39.0

func RenderFill(render func(...string) string, s string) string

RenderFill is Fill for a renderer such as lipgloss.Style.Render, without this package importing lipgloss: the renderer's opening sequence is found by rendering a probe. Text with no escapes in it renders exactly as render would, so plain rows are byte-identical to an ordinary Render.

Types

This section is empty.

Jump to

Keyboard shortcuts

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