Browse code

Lint pkg/term/windows package

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/09/04 05:01:56
Showing 4 changed files
... ...
@@ -88,6 +88,7 @@ packages=(
88 88
 	pkg/tailfile
89 89
 	pkg/tarsum
90 90
 	pkg/term
91
+	pkg/term/windows
91 92
 	pkg/timeoutconn
92 93
 	pkg/timeutils
93 94
 	pkg/tlsconfig
... ...
@@ -10,8 +10,8 @@ import (
10 10
 	"strings"
11 11
 	"unsafe"
12 12
 
13
-	. "github.com/Azure/go-ansiterm"
14
-	. "github.com/Azure/go-ansiterm/winterm"
13
+	ansiterm "github.com/Azure/go-ansiterm"
14
+	"github.com/Azure/go-ansiterm/winterm"
15 15
 )
16 16
 
17 17
 // ansiReader wraps a standard input file (e.g., os.Stdin) providing ANSI sequence translation.
... ...
@@ -26,12 +26,12 @@ type ansiReader struct {
26 26
 }
27 27
 
28 28
 func newAnsiReader(nFile int) *ansiReader {
29
-	file, fd := GetStdFile(nFile)
29
+	file, fd := winterm.GetStdFile(nFile)
30 30
 	return &ansiReader{
31 31
 		file:           file,
32 32
 		fd:             fd,
33
-		command:        make([]byte, 0, ANSI_MAX_CMD_LENGTH),
34
-		escapeSequence: []byte(KEY_ESC_CSI),
33
+		command:        make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
34
+		escapeSequence: []byte(ansiterm.KEY_ESC_CSI),
35 35
 		buffer:         make([]byte, 0),
36 36
 	}
37 37
 }
... ...
@@ -101,28 +101,28 @@ func (ar *ansiReader) Read(p []byte) (int, error) {
101 101
 }
102 102
 
103 103
 // readInputEvents polls until at least one event is available.
104
-func readInputEvents(fd uintptr, maxBytes int) ([]INPUT_RECORD, error) {
104
+func readInputEvents(fd uintptr, maxBytes int) ([]winterm.INPUT_RECORD, error) {
105 105
 	// Determine the maximum number of records to retrieve
106 106
 	// -- Cast around the type system to obtain the size of a single INPUT_RECORD.
107 107
 	//    unsafe.Sizeof requires an expression vs. a type-reference; the casting
108 108
 	//    tricks the type system into believing it has such an expression.
109
-	recordSize := int(unsafe.Sizeof(*((*INPUT_RECORD)(unsafe.Pointer(&maxBytes)))))
109
+	recordSize := int(unsafe.Sizeof(*((*winterm.INPUT_RECORD)(unsafe.Pointer(&maxBytes)))))
110 110
 	countRecords := maxBytes / recordSize
111
-	if countRecords > MAX_INPUT_EVENTS {
112
-		countRecords = MAX_INPUT_EVENTS
111
+	if countRecords > ansiterm.MAX_INPUT_EVENTS {
112
+		countRecords = ansiterm.MAX_INPUT_EVENTS
113 113
 	}
114 114
 	logger.Debugf("[windows] readInputEvents: Reading %v records (buffer size %v, record size %v)", countRecords, maxBytes, recordSize)
115 115
 
116 116
 	// Wait for and read input events
117
-	events := make([]INPUT_RECORD, countRecords)
117
+	events := make([]winterm.INPUT_RECORD, countRecords)
118 118
 	nEvents := uint32(0)
119
-	eventsExist, err := WaitForSingleObject(fd, WAIT_INFINITE)
119
+	eventsExist, err := winterm.WaitForSingleObject(fd, winterm.WAIT_INFINITE)
120 120
 	if err != nil {
121 121
 		return nil, err
122 122
 	}
123 123
 
124 124
 	if eventsExist {
125
-		err = ReadConsoleInput(fd, events, &nEvents)
125
+		err = winterm.ReadConsoleInput(fd, events, &nEvents)
126 126
 		if err != nil {
127 127
 			return nil, err
128 128
 		}
... ...
@@ -135,43 +135,43 @@ func readInputEvents(fd uintptr, maxBytes int) ([]INPUT_RECORD, error) {
135 135
 
136 136
 // KeyEvent Translation Helpers
137 137
 
138
-var arrowKeyMapPrefix = map[WORD]string{
139
-	VK_UP:    "%s%sA",
140
-	VK_DOWN:  "%s%sB",
141
-	VK_RIGHT: "%s%sC",
142
-	VK_LEFT:  "%s%sD",
138
+var arrowKeyMapPrefix = map[winterm.WORD]string{
139
+	winterm.VK_UP:    "%s%sA",
140
+	winterm.VK_DOWN:  "%s%sB",
141
+	winterm.VK_RIGHT: "%s%sC",
142
+	winterm.VK_LEFT:  "%s%sD",
143 143
 }
144 144
 
145
-var keyMapPrefix = map[WORD]string{
146
-	VK_UP:     "\x1B[%sA",
147
-	VK_DOWN:   "\x1B[%sB",
148
-	VK_RIGHT:  "\x1B[%sC",
149
-	VK_LEFT:   "\x1B[%sD",
150
-	VK_HOME:   "\x1B[1%s~", // showkey shows ^[[1
151
-	VK_END:    "\x1B[4%s~", // showkey shows ^[[4
152
-	VK_INSERT: "\x1B[2%s~",
153
-	VK_DELETE: "\x1B[3%s~",
154
-	VK_PRIOR:  "\x1B[5%s~",
155
-	VK_NEXT:   "\x1B[6%s~",
156
-	VK_F1:     "",
157
-	VK_F2:     "",
158
-	VK_F3:     "\x1B[13%s~",
159
-	VK_F4:     "\x1B[14%s~",
160
-	VK_F5:     "\x1B[15%s~",
161
-	VK_F6:     "\x1B[17%s~",
162
-	VK_F7:     "\x1B[18%s~",
163
-	VK_F8:     "\x1B[19%s~",
164
-	VK_F9:     "\x1B[20%s~",
165
-	VK_F10:    "\x1B[21%s~",
166
-	VK_F11:    "\x1B[23%s~",
167
-	VK_F12:    "\x1B[24%s~",
145
+var keyMapPrefix = map[winterm.WORD]string{
146
+	winterm.VK_UP:     "\x1B[%sA",
147
+	winterm.VK_DOWN:   "\x1B[%sB",
148
+	winterm.VK_RIGHT:  "\x1B[%sC",
149
+	winterm.VK_LEFT:   "\x1B[%sD",
150
+	winterm.VK_HOME:   "\x1B[1%s~", // showkey shows ^[[1
151
+	winterm.VK_END:    "\x1B[4%s~", // showkey shows ^[[4
152
+	winterm.VK_INSERT: "\x1B[2%s~",
153
+	winterm.VK_DELETE: "\x1B[3%s~",
154
+	winterm.VK_PRIOR:  "\x1B[5%s~",
155
+	winterm.VK_NEXT:   "\x1B[6%s~",
156
+	winterm.VK_F1:     "",
157
+	winterm.VK_F2:     "",
158
+	winterm.VK_F3:     "\x1B[13%s~",
159
+	winterm.VK_F4:     "\x1B[14%s~",
160
+	winterm.VK_F5:     "\x1B[15%s~",
161
+	winterm.VK_F6:     "\x1B[17%s~",
162
+	winterm.VK_F7:     "\x1B[18%s~",
163
+	winterm.VK_F8:     "\x1B[19%s~",
164
+	winterm.VK_F9:     "\x1B[20%s~",
165
+	winterm.VK_F10:    "\x1B[21%s~",
166
+	winterm.VK_F11:    "\x1B[23%s~",
167
+	winterm.VK_F12:    "\x1B[24%s~",
168 168
 }
169 169
 
170 170
 // translateKeyEvents converts the input events into the appropriate ANSI string.
171
-func translateKeyEvents(events []INPUT_RECORD, escapeSequence []byte) []byte {
171
+func translateKeyEvents(events []winterm.INPUT_RECORD, escapeSequence []byte) []byte {
172 172
 	var buffer bytes.Buffer
173 173
 	for _, event := range events {
174
-		if event.EventType == KEY_EVENT && event.KeyEvent.KeyDown != 0 {
174
+		if event.EventType == winterm.KEY_EVENT && event.KeyEvent.KeyDown != 0 {
175 175
 			buffer.WriteString(keyToString(&event.KeyEvent, escapeSequence))
176 176
 		}
177 177
 	}
... ...
@@ -180,7 +180,7 @@ func translateKeyEvents(events []INPUT_RECORD, escapeSequence []byte) []byte {
180 180
 }
181 181
 
182 182
 // keyToString maps the given input event record to the corresponding string.
183
-func keyToString(keyEvent *KEY_EVENT_RECORD, escapeSequence []byte) string {
183
+func keyToString(keyEvent *winterm.KEY_EVENT_RECORD, escapeSequence []byte) string {
184 184
 	if keyEvent.UnicodeChar == 0 {
185 185
 		return formatVirtualKey(keyEvent.VirtualKeyCode, keyEvent.ControlKeyState, escapeSequence)
186 186
 	}
... ...
@@ -199,14 +199,14 @@ func keyToString(keyEvent *KEY_EVENT_RECORD, escapeSequence []byte) string {
199 199
 
200 200
 	// <Alt>+Key generates ESC N Key
201 201
 	if !control && alt {
202
-		return KEY_ESC_N + strings.ToLower(string(keyEvent.UnicodeChar))
202
+		return ansiterm.KEY_ESC_N + strings.ToLower(string(keyEvent.UnicodeChar))
203 203
 	}
204 204
 
205 205
 	return string(keyEvent.UnicodeChar)
206 206
 }
207 207
 
208 208
 // formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string.
209
-func formatVirtualKey(key WORD, controlState DWORD, escapeSequence []byte) string {
209
+func formatVirtualKey(key winterm.WORD, controlState winterm.DWORD, escapeSequence []byte) string {
210 210
 	shift, alt, control := getControlKeys(controlState)
211 211
 	modifier := getControlKeysModifier(shift, alt, control, false)
212 212
 
... ...
@@ -222,35 +222,35 @@ func formatVirtualKey(key WORD, controlState DWORD, escapeSequence []byte) strin
222 222
 }
223 223
 
224 224
 // getControlKeys extracts the shift, alt, and ctrl key states.
225
-func getControlKeys(controlState DWORD) (shift, alt, control bool) {
226
-	shift = 0 != (controlState & SHIFT_PRESSED)
227
-	alt = 0 != (controlState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
228
-	control = 0 != (controlState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
225
+func getControlKeys(controlState winterm.DWORD) (shift, alt, control bool) {
226
+	shift = 0 != (controlState & winterm.SHIFT_PRESSED)
227
+	alt = 0 != (controlState & (winterm.LEFT_ALT_PRESSED | winterm.RIGHT_ALT_PRESSED))
228
+	control = 0 != (controlState & (winterm.LEFT_CTRL_PRESSED | winterm.RIGHT_CTRL_PRESSED))
229 229
 	return shift, alt, control
230 230
 }
231 231
 
232 232
 // getControlKeysModifier returns the ANSI modifier for the given combination of control keys.
233 233
 func getControlKeysModifier(shift, alt, control, meta bool) string {
234 234
 	if shift && alt && control {
235
-		return KEY_CONTROL_PARAM_8
235
+		return ansiterm.KEY_CONTROL_PARAM_8
236 236
 	}
237 237
 	if alt && control {
238
-		return KEY_CONTROL_PARAM_7
238
+		return ansiterm.KEY_CONTROL_PARAM_7
239 239
 	}
240 240
 	if shift && control {
241
-		return KEY_CONTROL_PARAM_6
241
+		return ansiterm.KEY_CONTROL_PARAM_6
242 242
 	}
243 243
 	if control {
244
-		return KEY_CONTROL_PARAM_5
244
+		return ansiterm.KEY_CONTROL_PARAM_5
245 245
 	}
246 246
 	if shift && alt {
247
-		return KEY_CONTROL_PARAM_4
247
+		return ansiterm.KEY_CONTROL_PARAM_4
248 248
 	}
249 249
 	if alt {
250
-		return KEY_CONTROL_PARAM_3
250
+		return ansiterm.KEY_CONTROL_PARAM_3
251 251
 	}
252 252
 	if shift {
253
-		return KEY_CONTROL_PARAM_2
253
+		return ansiterm.KEY_CONTROL_PARAM_2
254 254
 	}
255 255
 	return ""
256 256
 }
... ...
@@ -6,8 +6,8 @@ import (
6 6
 	"io/ioutil"
7 7
 	"os"
8 8
 
9
-	. "github.com/Azure/go-ansiterm"
10
-	. "github.com/Azure/go-ansiterm/winterm"
9
+	ansiterm "github.com/Azure/go-ansiterm"
10
+	"github.com/Azure/go-ansiterm/winterm"
11 11
 	"github.com/Sirupsen/logrus"
12 12
 )
13 13
 
... ...
@@ -17,17 +17,17 @@ var logger *logrus.Logger
17 17
 type ansiWriter struct {
18 18
 	file           *os.File
19 19
 	fd             uintptr
20
-	infoReset      *CONSOLE_SCREEN_BUFFER_INFO
20
+	infoReset      *winterm.CONSOLE_SCREEN_BUFFER_INFO
21 21
 	command        []byte
22 22
 	escapeSequence []byte
23 23
 	inAnsiSequence bool
24
-	parser         *AnsiParser
24
+	parser         *ansiterm.AnsiParser
25 25
 }
26 26
 
27 27
 func newAnsiWriter(nFile int) *ansiWriter {
28 28
 	logFile := ioutil.Discard
29 29
 
30
-	if isDebugEnv := os.Getenv(LogEnv); isDebugEnv == "1" {
30
+	if isDebugEnv := os.Getenv(ansiterm.LogEnv); isDebugEnv == "1" {
31 31
 		logFile, _ = os.Create("ansiReaderWriter.log")
32 32
 	}
33 33
 
... ...
@@ -37,21 +37,21 @@ func newAnsiWriter(nFile int) *ansiWriter {
37 37
 		Level:     logrus.DebugLevel,
38 38
 	}
39 39
 
40
-	file, fd := GetStdFile(nFile)
41
-	info, err := GetConsoleScreenBufferInfo(fd)
40
+	file, fd := winterm.GetStdFile(nFile)
41
+	info, err := winterm.GetConsoleScreenBufferInfo(fd)
42 42
 	if err != nil {
43 43
 		return nil
44 44
 	}
45 45
 
46
-	parser := CreateParser("Ground", CreateWinEventHandler(fd, file))
46
+	parser := ansiterm.CreateParser("Ground", winterm.CreateWinEventHandler(fd, file))
47 47
 	logger.Infof("newAnsiWriter: parser %p", parser)
48 48
 
49 49
 	aw := &ansiWriter{
50 50
 		file:           file,
51 51
 		fd:             fd,
52 52
 		infoReset:      info,
53
-		command:        make([]byte, 0, ANSI_MAX_CMD_LENGTH),
54
-		escapeSequence: []byte(KEY_ESC_CSI),
53
+		command:        make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
54
+		escapeSequence: []byte(ansiterm.KEY_ESC_CSI),
55 55
 		parser:         parser,
56 56
 	}
57 57
 
... ...
@@ -7,10 +7,10 @@ import (
7 7
 	"os"
8 8
 	"syscall"
9 9
 
10
-	. "github.com/Azure/go-ansiterm/winterm"
10
+	"github.com/Azure/go-ansiterm/winterm"
11 11
 )
12 12
 
13
-// ConsoleStreams, for each standard stream referencing a console, returns a wrapped version
13
+// ConsoleStreams returns a wrapped version for each standard stream referencing a console,
14 14
 // that handles ANSI character sequences.
15 15
 func ConsoleStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
16 16
 	if IsConsole(os.Stdin.Fd()) {
... ...
@@ -56,6 +56,6 @@ func GetHandleInfo(in interface{}) (uintptr, bool) {
56 56
 // IsConsole returns true if the given file descriptor is a Windows Console.
57 57
 // The code assumes that GetConsoleMode will return an error for file descriptors that are not a console.
58 58
 func IsConsole(fd uintptr) bool {
59
-	_, e := GetConsoleMode(fd)
59
+	_, e := winterm.GetConsoleMode(fd)
60 60
 	return e == nil
61 61
 }