package ansi

import "golazy.dev/lazytui/encoding/ansi"

Package ansi encodes and decodes ANSI and VT terminal byte streams.

The package is deliberately protocol-level. It turns byte streams into tokens and tokens or operations into byte streams, but it does not mutate a terminal screen. Terminal emulation belongs in higher-level lazytui packages.

Functions

func Append

Append appends op's ANSI byte representation to dst.

func Append(dst []byte, op Op) []byte

Types

type CSIToken

CSIToken contains a Control Sequence Introducer sequence.

type CSIToken struct {
	Prefix		string
	Params		[]Param
	Intermediates	string
	Final		byte
	Raw		[]byte
}

type Decoder

Decoder incrementally decodes ANSI and VT terminal streams.

type Decoder struct {
	// contains filtered or unexported fields
}
{
	var decoder Decoder
	tokens, _ := decoder.Decode([]byte("\x1b[2JHi"))

	for _, token := range tokens {
		fmt.Printf("%T\n", token)
	}

}
ansi.CSIToken
ansi.PrintToken
func (d *Decoder) Decode

Decode consumes data and returns any complete tokens found.

func (d *Decoder) Decode(data []byte) ([]Token, error)
func (d *Decoder) Reset

Reset clears the decoder state and drops buffered incomplete input.

func (d *Decoder) Reset()

type Encoder

Encoder writes terminal operations to an io.Writer.

type Encoder struct{}
{
	var buf bytes.Buffer
	encoder := &Encoder{}

	_ = encoder.Encode(&buf, SGR(codes.Bold, codes.FgRGB(1, 2, 3)))
	_ = encoder.Encode(&buf, Print("Lazy"))
	_ = encoder.Encode(&buf, SGR(codes.Reset))

	fmt.Printf("%q\n", buf.String())

}
"\x1b[1;38;2;1;2;3mLazy\x1b[0m"
func (e *Encoder) Encode

Encode writes op to w.

func (e *Encoder) Encode(w io.Writer, op Op) error

type HyperlinkToken

HyperlinkToken reports an OSC 8 hyperlink command. An empty URL ends the current hyperlink.

type HyperlinkToken struct {
	Params	string
	URL	string
	Raw	[]byte
}

type MouseToken

MouseToken reports an SGR mouse event.

type MouseToken struct {
	Button	int
	Row	int
	Col	int
	Release	bool
	Raw	[]byte
}

type Op

Op is an encodable terminal operation.

type Op interface {
	// contains filtered or unexported methods
}
func CSI

CSI returns an operation for a CSI sequence with numeric parameters.

func CSI(final byte, params ...int) Op
func CSIWithPrefix

CSIWithPrefix returns a CSI operation with a private or extension prefix.

func CSIWithPrefix(prefix string, final byte, params ...int) Op
func Control

Control returns an operation for a single control byte.

func Control(code codes.Control) Op
func CursorBack

CursorBack moves the cursor left n columns.

func CursorBack(n int) Op
func CursorDown

CursorDown moves the cursor down n rows.

func CursorDown(n int) Op
func CursorForward

CursorForward moves the cursor right n columns.

func CursorForward(n int) Op
func CursorPosition

CursorPosition moves the cursor to row and col, both 1-based.

func CursorPosition(row, col int) Op
func CursorUp

CursorUp moves the cursor up n rows.

func CursorUp(n int) Op
func DisableMouse

DisableMouse disables common xterm-compatible mouse tracking modes.

func DisableMouse() Op
func EnableMouse

EnableMouse enables button-event mouse tracking with SGR coordinates.

func EnableMouse() Op
func EraseDisplay

EraseDisplay erases part of the display according to mode.

func EraseDisplay(mode int) Op
func EraseLine

EraseLine erases part of the current line according to mode.

func EraseLine(mode int) Op
func Escape

Escape returns an operation for a simple ESC sequence.

func Escape(final byte) Op
func OSC

OSC returns an Operating System Command operation terminated with ST.

func OSC(command int, data string) Op
func Print

Print returns an operation that writes printable text.

func Print(text string) Op
func RequestWindowSize

RequestWindowSize asks an xterm-compatible terminal to report text-area size.

func RequestWindowSize() Op
func SGR

SGR returns a Select Graphic Rendition operation.

func SGR(parts ...codes.SGR) Op
func Sequence

Sequence returns an operation that writes each op in order.

func Sequence(ops ...Op) Op
func WindowSizeReport

WindowSizeReport encodes an xterm-compatible text-area size report.

func WindowSizeReport(rows, cols int) Op
func WindowTitle

WindowTitle sets the terminal window title.

func WindowTitle(title string) Op

type Param

Param is a CSI parameter. Empty parameters are preserved as nil or empty slices so callers can apply command-specific defaults.

type Param []int

type Token

Token is a decoded ANSI stream item.

type Token interface {
	// contains filtered or unexported methods
}

Directories

Path Synopsis
lazytui/encoding/ansi/codes Package codes defines ANSI, ECMA-48, and common terminal extension codes.