vendor/github.com/charmbracelet/x/ansi/csi.go

141 lines · 4137 bytes

  1package ansi
  2
  3import (
  4	"bytes"
  5	"strconv"
  6
  7	"github.com/charmbracelet/x/ansi/parser"
  8)
  9
 10// CsiSequence represents a control sequence introducer (CSI) sequence.
 11//
 12// The sequence starts with a CSI sequence, CSI (0x9B) in a 8-bit environment
 13// or ESC [ (0x1B 0x5B) in a 7-bit environment, followed by any number of
 14// parameters in the range of 0x30-0x3F, then by any number of intermediate
 15// byte in the range of 0x20-0x2F, then finally with a single final byte in the
 16// range of 0x20-0x7E.
 17//
 18//	CSI P..P I..I F
 19//
 20// See ECMA-48 § 5.4.
 21type CsiSequence struct {
 22	// Params contains the raw parameters of the sequence.
 23	// This is a slice of integers, where each integer is a 32-bit integer
 24	// containing the parameter value in the lower 31 bits and a flag in the
 25	// most significant bit indicating whether there are more sub-parameters.
 26	Params []int
 27
 28	// Cmd contains the raw command of the sequence.
 29	// The command is a 32-bit integer containing the CSI command byte in the
 30	// lower 8 bits, the private marker in the next 8 bits, and the intermediate
 31	// byte in the next 8 bits.
 32	//
 33	//  CSI ? u
 34	//
 35	// Is represented as:
 36	//
 37	//  'u' | '?' << 8
 38	Cmd int
 39}
 40
 41var _ Sequence = CsiSequence{}
 42
 43// Marker returns the marker byte of the CSI sequence.
 44// This is always gonna be one of the following '<' '=' '>' '?' and in the
 45// range of 0x3C-0x3F.
 46// Zero is returned if the sequence does not have a marker.
 47func (s CsiSequence) Marker() int {
 48	return parser.Marker(s.Cmd)
 49}
 50
 51// Intermediate returns the intermediate byte of the CSI sequence.
 52// An intermediate byte is in the range of 0x20-0x2F. This includes these
 53// characters from ' ', '!', '"', '#', '$', '%', '&', ”', '(', ')', '*', '+',
 54// ',', '-', '.', '/'.
 55// Zero is returned if the sequence does not have an intermediate byte.
 56func (s CsiSequence) Intermediate() int {
 57	return parser.Intermediate(s.Cmd)
 58}
 59
 60// Command returns the command byte of the CSI sequence.
 61func (s CsiSequence) Command() int {
 62	return parser.Command(s.Cmd)
 63}
 64
 65// Param returns the parameter at the given index.
 66// It returns -1 if the parameter does not exist.
 67func (s CsiSequence) Param(i int) int {
 68	return parser.Param(s.Params, i)
 69}
 70
 71// HasMore returns true if the parameter has more sub-parameters.
 72func (s CsiSequence) HasMore(i int) bool {
 73	return parser.HasMore(s.Params, i)
 74}
 75
 76// Subparams returns the sub-parameters of the given parameter.
 77// It returns nil if the parameter does not exist.
 78func (s CsiSequence) Subparams(i int) []int {
 79	return parser.Subparams(s.Params, i)
 80}
 81
 82// Len returns the number of parameters in the sequence.
 83// This will return the number of parameters in the sequence, excluding any
 84// sub-parameters.
 85func (s CsiSequence) Len() int {
 86	return parser.Len(s.Params)
 87}
 88
 89// Range iterates over the parameters of the sequence and calls the given
 90// function for each parameter.
 91// The function should return false to stop the iteration.
 92func (s CsiSequence) Range(fn func(i int, param int, hasMore bool) bool) {
 93	parser.Range(s.Params, fn)
 94}
 95
 96// Clone returns a copy of the CSI sequence.
 97func (s CsiSequence) Clone() Sequence {
 98	return CsiSequence{
 99		Params: append([]int(nil), s.Params...),
100		Cmd:    s.Cmd,
101	}
102}
103
104// String returns a string representation of the sequence.
105// The string will always be in the 7-bit format i.e (ESC [ P..P I..I F).
106func (s CsiSequence) String() string {
107	return s.buffer().String()
108}
109
110// buffer returns a buffer containing the sequence.
111func (s CsiSequence) buffer() *bytes.Buffer {
112	var b bytes.Buffer
113	b.WriteString("\x1b[")
114	if m := s.Marker(); m != 0 {
115		b.WriteByte(byte(m))
116	}
117	s.Range(func(i, param int, hasMore bool) bool {
118		if param >= 0 {
119			b.WriteString(strconv.Itoa(param))
120		}
121		if i < len(s.Params)-1 {
122			if hasMore {
123				b.WriteByte(':')
124			} else {
125				b.WriteByte(';')
126			}
127		}
128		return true
129	})
130	if i := s.Intermediate(); i != 0 {
131		b.WriteByte(byte(i))
132	}
133	b.WriteByte(byte(s.Command()))
134	return &b
135}
136
137// Bytes returns the byte representation of the sequence.
138// The bytes will always be in the 7-bit format i.e (ESC [ P..P I..I F).
139func (s CsiSequence) Bytes() []byte {
140	return s.buffer().Bytes()
141}