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