dcs.go 3.9 KB

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