ansi

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const ESC = 0x1b

Variables

This section is empty.

Functions

This section is empty.

Types

type ANSITransformer

type ANSITransformer struct {
	// contains filtered or unexported fields
}

`ANSITransformer` wraps an inner Transformer, preserving ANSI SGR escape sequences (`ESC [ ... m`). What does this mean in plain English? If you have a string containing SGR colour codes (for the terminal), like `\x1b[31mhello world\x1b[0m` and you want to title case it, you could use `cases.Title`, except that this doesn't know anything about ANSI sequences, so it's likely to mess the string up, or at least not give the expected result. `cases.Titles` is a [`Transformer`]. This is an interface that expresses the idea of processing byte arrays, and applying transformations to them. `ANSITransformer` is a `Transformer` which wraps another `Transformer`. It removes any SGR codes, passing the plain text to the wrapped `Transformer`, and reassembles the result. So the underlying text is transformed, but any colours are preserved.

func NewANSITransformer

func NewANSITransformer(inner transform.Transformer) *ANSITransformer

NewANSITransformer returns a Transformer that applies inner to text segments but leaves ANSI bracket sequences intact.

func (*ANSITransformer) Reset

func (w *ANSITransformer) Reset()

Reset clears the transformer's state.

func (*ANSITransformer) Span

func (w *ANSITransformer) Span(src []byte, atEOF bool) (n int, err error)

Span implements the `transform.SpanningTransformer` interface. It returns the length of the longest prefix of src that consists of either ANSI sequences (which don't need transformation) or spans that the inner transformer (if it's a `SpanningTransformer`) reports don't need transformation.

func (*ANSITransformer) Transform

func (w *ANSITransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Transform processes `src` into `dst`, preserving complete ANSI sequences and applying `inner` to the text content.

type Segment

type Segment struct {
	// What kind of segment this is
	Type SegmentType
	// Start position in the original input
	Start int
	// End position in the original input (exclusive)
	End int
}

Segment represents a ANSI-sequence-separated segment of the input. The elements `[Start, End)` are the longest contiguous slice of elements which have type `Type`.

type SegmentType

type SegmentType int

SegmentType represents the type of segment found in the input

const (
	// PlainText is a regular text segment that should be transformed
	PlainText SegmentType = iota
	// AnsiSequence is a complete ANSI sequence that should be preserved as-is
	AnsiSequence
	// PartialAnsiSequence is an incomplete ANSI sequence that needs more input
	PartialAnsiSequence
)

Jump to

Keyboard shortcuts

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