constants.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package ansiterm
  2. const LogEnv = "DEBUG_TERMINAL"
  3. // ANSI constants
  4. // References:
  5. // -- http://www.ecma-international.org/publications/standards/Ecma-048.htm
  6. // -- http://man7.org/linux/man-pages/man4/console_codes.4.html
  7. // -- http://manpages.ubuntu.com/manpages/intrepid/man4/console_codes.4.html
  8. // -- http://en.wikipedia.org/wiki/ANSI_escape_code
  9. // -- http://vt100.net/emu/dec_ansi_parser
  10. // -- http://vt100.net/emu/vt500_parser.svg
  11. // -- http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
  12. // -- http://www.inwap.com/pdp10/ansicode.txt
  13. const (
  14. // ECMA-48 Set Graphics Rendition
  15. // Note:
  16. // -- Constants leading with an underscore (e.g., _ANSI_xxx) are unsupported or reserved
  17. // -- Fonts could possibly be supported via SetCurrentConsoleFontEx
  18. // -- Windows does not expose the per-window cursor (i.e., caret) blink times
  19. ANSI_SGR_RESET = 0
  20. ANSI_SGR_BOLD = 1
  21. ANSI_SGR_DIM = 2
  22. _ANSI_SGR_ITALIC = 3
  23. ANSI_SGR_UNDERLINE = 4
  24. _ANSI_SGR_BLINKSLOW = 5
  25. _ANSI_SGR_BLINKFAST = 6
  26. ANSI_SGR_REVERSE = 7
  27. _ANSI_SGR_INVISIBLE = 8
  28. _ANSI_SGR_LINETHROUGH = 9
  29. _ANSI_SGR_FONT_00 = 10
  30. _ANSI_SGR_FONT_01 = 11
  31. _ANSI_SGR_FONT_02 = 12
  32. _ANSI_SGR_FONT_03 = 13
  33. _ANSI_SGR_FONT_04 = 14
  34. _ANSI_SGR_FONT_05 = 15
  35. _ANSI_SGR_FONT_06 = 16
  36. _ANSI_SGR_FONT_07 = 17
  37. _ANSI_SGR_FONT_08 = 18
  38. _ANSI_SGR_FONT_09 = 19
  39. _ANSI_SGR_FONT_10 = 20
  40. _ANSI_SGR_DOUBLEUNDERLINE = 21
  41. ANSI_SGR_BOLD_DIM_OFF = 22
  42. _ANSI_SGR_ITALIC_OFF = 23
  43. ANSI_SGR_UNDERLINE_OFF = 24
  44. _ANSI_SGR_BLINK_OFF = 25
  45. _ANSI_SGR_RESERVED_00 = 26
  46. ANSI_SGR_REVERSE_OFF = 27
  47. _ANSI_SGR_INVISIBLE_OFF = 28
  48. _ANSI_SGR_LINETHROUGH_OFF = 29
  49. ANSI_SGR_FOREGROUND_BLACK = 30
  50. ANSI_SGR_FOREGROUND_RED = 31
  51. ANSI_SGR_FOREGROUND_GREEN = 32
  52. ANSI_SGR_FOREGROUND_YELLOW = 33
  53. ANSI_SGR_FOREGROUND_BLUE = 34
  54. ANSI_SGR_FOREGROUND_MAGENTA = 35
  55. ANSI_SGR_FOREGROUND_CYAN = 36
  56. ANSI_SGR_FOREGROUND_WHITE = 37
  57. _ANSI_SGR_RESERVED_01 = 38
  58. ANSI_SGR_FOREGROUND_DEFAULT = 39
  59. ANSI_SGR_BACKGROUND_BLACK = 40
  60. ANSI_SGR_BACKGROUND_RED = 41
  61. ANSI_SGR_BACKGROUND_GREEN = 42
  62. ANSI_SGR_BACKGROUND_YELLOW = 43
  63. ANSI_SGR_BACKGROUND_BLUE = 44
  64. ANSI_SGR_BACKGROUND_MAGENTA = 45
  65. ANSI_SGR_BACKGROUND_CYAN = 46
  66. ANSI_SGR_BACKGROUND_WHITE = 47
  67. _ANSI_SGR_RESERVED_02 = 48
  68. ANSI_SGR_BACKGROUND_DEFAULT = 49
  69. // 50 - 65: Unsupported
  70. ANSI_MAX_CMD_LENGTH = 4096
  71. MAX_INPUT_EVENTS = 128
  72. DEFAULT_WIDTH = 80
  73. DEFAULT_HEIGHT = 24
  74. ANSI_BEL = 0x07
  75. ANSI_BACKSPACE = 0x08
  76. ANSI_TAB = 0x09
  77. ANSI_LINE_FEED = 0x0A
  78. ANSI_VERTICAL_TAB = 0x0B
  79. ANSI_FORM_FEED = 0x0C
  80. ANSI_CARRIAGE_RETURN = 0x0D
  81. ANSI_ESCAPE_PRIMARY = 0x1B
  82. ANSI_ESCAPE_SECONDARY = 0x5B
  83. ANSI_OSC_STRING_ENTRY = 0x5D
  84. ANSI_COMMAND_FIRST = 0x40
  85. ANSI_COMMAND_LAST = 0x7E
  86. DCS_ENTRY = 0x90
  87. CSI_ENTRY = 0x9B
  88. OSC_STRING = 0x9D
  89. ANSI_PARAMETER_SEP = ";"
  90. ANSI_CMD_G0 = '('
  91. ANSI_CMD_G1 = ')'
  92. ANSI_CMD_G2 = '*'
  93. ANSI_CMD_G3 = '+'
  94. ANSI_CMD_DECPNM = '>'
  95. ANSI_CMD_DECPAM = '='
  96. ANSI_CMD_OSC = ']'
  97. ANSI_CMD_STR_TERM = '\\'
  98. KEY_CONTROL_PARAM_2 = ";2"
  99. KEY_CONTROL_PARAM_3 = ";3"
  100. KEY_CONTROL_PARAM_4 = ";4"
  101. KEY_CONTROL_PARAM_5 = ";5"
  102. KEY_CONTROL_PARAM_6 = ";6"
  103. KEY_CONTROL_PARAM_7 = ";7"
  104. KEY_CONTROL_PARAM_8 = ";8"
  105. KEY_ESC_CSI = "\x1B["
  106. KEY_ESC_N = "\x1BN"
  107. KEY_ESC_O = "\x1BO"
  108. FILL_CHARACTER = ' '
  109. )
  110. func getByteRange(start byte, end byte) []byte {
  111. bytes := make([]byte, 0, 32)
  112. for i := start; i <= end; i++ {
  113. bytes = append(bytes, byte(i))
  114. }
  115. return bytes
  116. }
  117. var toGroundBytes = getToGroundBytes()
  118. var executors = getExecuteBytes()
  119. // SPACE 20+A0 hex Always and everywhere a blank space
  120. // Intermediate 20-2F hex !"#$%&'()*+,-./
  121. var intermeds = getByteRange(0x20, 0x2F)
  122. // Parameters 30-3F hex 0123456789:;<=>?
  123. // CSI Parameters 30-39, 3B hex 0123456789;
  124. var csiParams = getByteRange(0x30, 0x3F)
  125. var csiCollectables = append(getByteRange(0x30, 0x39), getByteRange(0x3B, 0x3F)...)
  126. // Uppercase 40-5F hex @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
  127. var upperCase = getByteRange(0x40, 0x5F)
  128. // Lowercase 60-7E hex `abcdefghijlkmnopqrstuvwxyz{|}~
  129. var lowerCase = getByteRange(0x60, 0x7E)
  130. // Alphabetics 40-7E hex (all of upper and lower case)
  131. var alphabetics = append(upperCase, lowerCase...)
  132. var printables = getByteRange(0x20, 0x7F)
  133. var escapeIntermediateToGroundBytes = getByteRange(0x30, 0x7E)
  134. var escapeToGroundBytes = getEscapeToGroundBytes()
  135. // See http://www.vt100.net/emu/vt500_parser.png for description of the complex
  136. // byte ranges below
  137. func getEscapeToGroundBytes() []byte {
  138. escapeToGroundBytes := getByteRange(0x30, 0x4F)
  139. escapeToGroundBytes = append(escapeToGroundBytes, getByteRange(0x51, 0x57)...)
  140. escapeToGroundBytes = append(escapeToGroundBytes, 0x59)
  141. escapeToGroundBytes = append(escapeToGroundBytes, 0x5A)
  142. escapeToGroundBytes = append(escapeToGroundBytes, 0x5C)
  143. escapeToGroundBytes = append(escapeToGroundBytes, getByteRange(0x60, 0x7E)...)
  144. return escapeToGroundBytes
  145. }
  146. func getExecuteBytes() []byte {
  147. executeBytes := getByteRange(0x00, 0x17)
  148. executeBytes = append(executeBytes, 0x19)
  149. executeBytes = append(executeBytes, getByteRange(0x1C, 0x1F)...)
  150. return executeBytes
  151. }
  152. func getToGroundBytes() []byte {
  153. groundBytes := []byte{0x18}
  154. groundBytes = append(groundBytes, 0x1A)
  155. groundBytes = append(groundBytes, getByteRange(0x80, 0x8F)...)
  156. groundBytes = append(groundBytes, getByteRange(0x91, 0x97)...)
  157. groundBytes = append(groundBytes, 0x99)
  158. groundBytes = append(groundBytes, 0x9A)
  159. groundBytes = append(groundBytes, 0x9C)
  160. return groundBytes
  161. }
  162. // Delete 7F hex Always and everywhere ignored
  163. // C1 Control 80-9F hex 32 additional control characters
  164. // G1 Displayable A1-FE hex 94 additional displayable characters
  165. // Special A0+FF hex Same as SPACE and DELETE