ansi

package
v1.50.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package ansi provides shared helpers for scanning ECMA-48/ANSI escape sequences. It exists to give the CSI final-byte range a single source of truth: the same [a-zA-Z]-only bug (missing '@', '~', and other non-letter terminators per ECMA-48) was independently reimplemented across the codebase (session/detection, session/detection/ratelimit, session/tmux, server/services, pkg/analytics) before being fixed in BUG-025. Callers should use this package instead of hand-rolling the character class or byte range again — CSIFinalByteClass/StripCSI for regex-based scanners, IsCSIFinalByte for manual byte-level scanners (e.g. pkg/analytics/escape_code_parser.go).

Only the entry points callers actually need are exported; the compiled regex and the raw byte bounds are implementation details of StripCSI and IsCSIFinalByte respectively.

Index

Constants

View Source
const CSIFinalByteClass = `[@-~]`

CSIFinalByteClass is the regex character class for a CSI sequence's final byte. Per ECMA-48, CSI final bytes span 0x40-0x7E ('@' through '~'), not just ASCII letters — sequences like Insert Character (CSI Ps @) or tilde-terminated function-key sequences would otherwise be left unstripped, leaking raw escape bytes into text that downstream pattern matching operates on. Exported because callers combining the CSI branch with other alternatives (OSC, charset designation, etc.) need to build their own regexp rather than use the simple csiRegex below — see session/detection/detector.go's ansiStripRegex.

Variables

This section is empty.

Functions

func ExtractLastOSC added in v1.49.0

func ExtractLastOSC(data string, oscNums ...string) (payload string, ok bool)

ExtractLastOSC scans data for complete OSC sequences whose numeric command matches one of oscNums, terminated by BEL or ST, and returns the payload of the last (right-most) complete match, regardless of oscNums order. data should already be a bounded window (e.g. a fixed-size PTY tail) — this function does not itself cap scan length.

func IsCSIFinalByte

func IsCSIFinalByte(b byte) bool

IsCSIFinalByte reports whether b is a valid CSI sequence final byte per ECMA-48 (0x40-0x7E). Byte-level twin of CSIFinalByteClass for parsers that scan byte-by-byte rather than via regexp.

func StripCSI

func StripCSI(s string) string

StripCSI removes CSI escape sequences from s. Inputs without an ESC byte take a fast path that avoids the regexp entirely.

Types

This section is empty.

Jump to

Keyboard shortcuts

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