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

148 lines · 4494 bytes

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