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 ¶
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
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 ¶
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.
Types ¶
type AltScreenTracker ¶ added in v1.54.0
type AltScreenTracker struct {
// contains filtered or unexported fields
}
AltScreenTracker is a small stateful scanner that tracks whether a PTY output stream currently has its consumer in the alternate screen buffer. It mirrors this package's csi.go: a byte-string scanner, no regexp needed since the two markers are fixed literals rather than a variable-parameter CSI class.
Not safe for concurrent use — callers must serialize Observe calls (one PTY output stream has one logical writer).
func (*AltScreenTracker) Observe ¶ added in v1.54.0
func (t *AltScreenTracker) Observe(data string) (active, changed bool)
Observe scans data for alt-screen enter/exit markers and updates the tracker's state accordingly. If data contains multiple markers, the last one observed wins (matching real terminal semantics: state after processing the whole chunk). Returns the resulting active state and whether this call changed it from what it was before.
func (*AltScreenTracker) SetActive ¶ added in v1.54.0
func (t *AltScreenTracker) SetActive(active bool)
SetActive directly sets the tracker's state, bypassing byte-scanning -- for bootstrapping from a source that already knows the current state authoritatively (e.g. a live tmux query), so a subsequent Observe call computes `changed` relative to the correct baseline instead of the tracker's zero-value default. Also clears carry, so a partial marker buffered before this authoritative resync can't combine with the next chunk into a stale false match.