sequence.go 4.2 KB

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