sequence.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package ansi
  2. import (
  3. "bytes"
  4. "github.com/charmbracelet/x/ansi/parser"
  5. )
  6. // Sequence represents an ANSI sequence. This can be a control sequence, escape
  7. // sequence, a printable character, etc.
  8. // A Sequence can be one of the following types:
  9. // - [Rune]
  10. // - [ControlCode]
  11. // - [Grapheme]
  12. // - [EscSequence]
  13. // - [CsiSequence]
  14. // - [OscSequence]
  15. // - [DcsSequence]
  16. // - [SosSequence]
  17. // - [PmSequence]
  18. // - [ApcSequence]
  19. type Sequence interface {
  20. // Clone returns a deep copy of the sequence.
  21. Clone() Sequence
  22. }
  23. // Rune represents a printable character.
  24. type Rune rune
  25. var _ Sequence = Rune(0)
  26. // Clone returns a deep copy of the rune.
  27. func (r Rune) Clone() Sequence {
  28. return r
  29. }
  30. // Grapheme represents a grapheme cluster.
  31. type Grapheme struct {
  32. Cluster string
  33. Width int
  34. }
  35. var _ Sequence = Grapheme{}
  36. // Clone returns a deep copy of the grapheme.
  37. func (g Grapheme) Clone() Sequence {
  38. return g
  39. }
  40. // ControlCode represents a control code character. This is a character that
  41. // is not printable and is used to control the terminal. This would be a
  42. // character in the C0 or C1 set in the range of 0x00-0x1F and 0x80-0x9F.
  43. type ControlCode byte
  44. var _ Sequence = ControlCode(0)
  45. // Bytes implements Sequence.
  46. func (c ControlCode) Bytes() []byte {
  47. return []byte{byte(c)}
  48. }
  49. // String implements Sequence.
  50. func (c ControlCode) String() string {
  51. return string(c)
  52. }
  53. // Clone returns a deep copy of the control code.
  54. func (c ControlCode) Clone() Sequence {
  55. return c
  56. }
  57. // EscSequence represents an escape sequence.
  58. type EscSequence Command
  59. var _ Sequence = EscSequence(0)
  60. // buffer returns the buffer of the escape sequence.
  61. func (e EscSequence) buffer() *bytes.Buffer {
  62. var b bytes.Buffer
  63. b.WriteByte('\x1b')
  64. if i := parser.Intermediate(int(e)); i != 0 {
  65. b.WriteByte(byte(i))
  66. }
  67. if cmd := e.Command(); cmd != 0 {
  68. b.WriteByte(byte(cmd))
  69. }
  70. return &b
  71. }
  72. // Bytes implements Sequence.
  73. func (e EscSequence) Bytes() []byte {
  74. return e.buffer().Bytes()
  75. }
  76. // String implements Sequence.
  77. func (e EscSequence) String() string {
  78. return e.buffer().String()
  79. }
  80. // Clone returns a deep copy of the escape sequence.
  81. func (e EscSequence) Clone() Sequence {
  82. return e
  83. }
  84. // Command returns the command byte of the escape sequence.
  85. func (e EscSequence) Command() int {
  86. return Command(e).Command()
  87. }
  88. // Intermediate returns the intermediate byte of the escape sequence.
  89. func (e EscSequence) Intermediate() int {
  90. return Command(e).Intermediate()
  91. }
  92. // SosSequence represents a SOS sequence.
  93. type SosSequence struct {
  94. // Data contains the raw data of the sequence.
  95. Data []byte
  96. }
  97. var _ Sequence = SosSequence{}
  98. // Bytes implements Sequence.
  99. func (s SosSequence) Bytes() []byte {
  100. return s.buffer().Bytes()
  101. }
  102. // String implements Sequence.
  103. func (s SosSequence) String() string {
  104. return s.buffer().String()
  105. }
  106. func (s SosSequence) buffer() *bytes.Buffer {
  107. var b bytes.Buffer
  108. b.WriteByte('\x1b')
  109. b.WriteByte('X')
  110. b.Write(s.Data)
  111. b.WriteString("\x1b\\")
  112. return &b
  113. }
  114. // Clone returns a deep copy of the SOS sequence.
  115. func (s SosSequence) Clone() Sequence {
  116. return SosSequence{
  117. Data: append([]byte(nil), s.Data...),
  118. }
  119. }
  120. // PmSequence represents a PM sequence.
  121. type PmSequence struct {
  122. // Data contains the raw data of the sequence.
  123. Data []byte
  124. }
  125. var _ Sequence = PmSequence{}
  126. // Bytes implements Sequence.
  127. func (s PmSequence) Bytes() []byte {
  128. return s.buffer().Bytes()
  129. }
  130. // String implements Sequence.
  131. func (s PmSequence) String() string {
  132. return s.buffer().String()
  133. }
  134. // buffer returns the buffer of the PM sequence.
  135. func (s PmSequence) buffer() *bytes.Buffer {
  136. var b bytes.Buffer
  137. b.WriteByte('\x1b')
  138. b.WriteByte('^')
  139. b.Write(s.Data)
  140. b.WriteString("\x1b\\")
  141. return &b
  142. }
  143. // Clone returns a deep copy of the PM sequence.
  144. func (p PmSequence) Clone() Sequence {
  145. return PmSequence{
  146. Data: append([]byte(nil), p.Data...),
  147. }
  148. }
  149. // ApcSequence represents an APC sequence.
  150. type ApcSequence struct {
  151. // Data contains the raw data of the sequence.
  152. Data []byte
  153. }
  154. var _ Sequence = ApcSequence{}
  155. // Clone returns a deep copy of the APC sequence.
  156. func (a ApcSequence) Clone() Sequence {
  157. return ApcSequence{
  158. Data: append([]byte(nil), a.Data...),
  159. }
  160. }
  161. // Bytes implements Sequence.
  162. func (s ApcSequence) Bytes() []byte {
  163. return s.buffer().Bytes()
  164. }
  165. // String implements Sequence.
  166. func (s ApcSequence) String() string {
  167. return s.buffer().String()
  168. }
  169. // buffer returns the buffer of the APC sequence.
  170. func (s ApcSequence) buffer() *bytes.Buffer {
  171. var b bytes.Buffer
  172. b.WriteByte('\x1b')
  173. b.WriteByte('_')
  174. b.Write(s.Data)
  175. b.WriteString("\x1b\\")
  176. return &b
  177. }