1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Lint pkg/term/windows package

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2015-09-03 22:01:56 +02:00
parent d9065fcdb5
commit 3409de971c
4 changed files with 70 additions and 69 deletions

View file

@ -88,6 +88,7 @@ packages=(
pkg/tailfile pkg/tailfile
pkg/tarsum pkg/tarsum
pkg/term pkg/term
pkg/term/windows
pkg/timeoutconn pkg/timeoutconn
pkg/timeutils pkg/timeutils
pkg/tlsconfig pkg/tlsconfig

View file

@ -10,8 +10,8 @@ import (
"strings" "strings"
"unsafe" "unsafe"
. "github.com/Azure/go-ansiterm" ansiterm "github.com/Azure/go-ansiterm"
. "github.com/Azure/go-ansiterm/winterm" "github.com/Azure/go-ansiterm/winterm"
) )
// ansiReader wraps a standard input file (e.g., os.Stdin) providing ANSI sequence translation. // ansiReader wraps a standard input file (e.g., os.Stdin) providing ANSI sequence translation.
@ -26,12 +26,12 @@ type ansiReader struct {
} }
func newAnsiReader(nFile int) *ansiReader { func newAnsiReader(nFile int) *ansiReader {
file, fd := GetStdFile(nFile) file, fd := winterm.GetStdFile(nFile)
return &ansiReader{ return &ansiReader{
file: file, file: file,
fd: fd, fd: fd,
command: make([]byte, 0, ANSI_MAX_CMD_LENGTH), command: make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
escapeSequence: []byte(KEY_ESC_CSI), escapeSequence: []byte(ansiterm.KEY_ESC_CSI),
buffer: make([]byte, 0), buffer: make([]byte, 0),
} }
} }
@ -101,28 +101,28 @@ func (ar *ansiReader) Read(p []byte) (int, error) {
} }
// readInputEvents polls until at least one event is available. // readInputEvents polls until at least one event is available.
func readInputEvents(fd uintptr, maxBytes int) ([]INPUT_RECORD, error) { func readInputEvents(fd uintptr, maxBytes int) ([]winterm.INPUT_RECORD, error) {
// Determine the maximum number of records to retrieve // Determine the maximum number of records to retrieve
// -- Cast around the type system to obtain the size of a single INPUT_RECORD. // -- Cast around the type system to obtain the size of a single INPUT_RECORD.
// unsafe.Sizeof requires an expression vs. a type-reference; the casting // unsafe.Sizeof requires an expression vs. a type-reference; the casting
// tricks the type system into believing it has such an expression. // tricks the type system into believing it has such an expression.
recordSize := int(unsafe.Sizeof(*((*INPUT_RECORD)(unsafe.Pointer(&maxBytes))))) recordSize := int(unsafe.Sizeof(*((*winterm.INPUT_RECORD)(unsafe.Pointer(&maxBytes)))))
countRecords := maxBytes / recordSize countRecords := maxBytes / recordSize
if countRecords > MAX_INPUT_EVENTS { if countRecords > ansiterm.MAX_INPUT_EVENTS {
countRecords = MAX_INPUT_EVENTS countRecords = ansiterm.MAX_INPUT_EVENTS
} }
logger.Debugf("[windows] readInputEvents: Reading %v records (buffer size %v, record size %v)", countRecords, maxBytes, recordSize) logger.Debugf("[windows] readInputEvents: Reading %v records (buffer size %v, record size %v)", countRecords, maxBytes, recordSize)
// Wait for and read input events // Wait for and read input events
events := make([]INPUT_RECORD, countRecords) events := make([]winterm.INPUT_RECORD, countRecords)
nEvents := uint32(0) nEvents := uint32(0)
eventsExist, err := WaitForSingleObject(fd, WAIT_INFINITE) eventsExist, err := winterm.WaitForSingleObject(fd, winterm.WAIT_INFINITE)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if eventsExist { if eventsExist {
err = ReadConsoleInput(fd, events, &nEvents) err = winterm.ReadConsoleInput(fd, events, &nEvents)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -135,43 +135,43 @@ func readInputEvents(fd uintptr, maxBytes int) ([]INPUT_RECORD, error) {
// KeyEvent Translation Helpers // KeyEvent Translation Helpers
var arrowKeyMapPrefix = map[WORD]string{ var arrowKeyMapPrefix = map[winterm.WORD]string{
VK_UP: "%s%sA", winterm.VK_UP: "%s%sA",
VK_DOWN: "%s%sB", winterm.VK_DOWN: "%s%sB",
VK_RIGHT: "%s%sC", winterm.VK_RIGHT: "%s%sC",
VK_LEFT: "%s%sD", winterm.VK_LEFT: "%s%sD",
} }
var keyMapPrefix = map[WORD]string{ var keyMapPrefix = map[winterm.WORD]string{
VK_UP: "\x1B[%sA", winterm.VK_UP: "\x1B[%sA",
VK_DOWN: "\x1B[%sB", winterm.VK_DOWN: "\x1B[%sB",
VK_RIGHT: "\x1B[%sC", winterm.VK_RIGHT: "\x1B[%sC",
VK_LEFT: "\x1B[%sD", winterm.VK_LEFT: "\x1B[%sD",
VK_HOME: "\x1B[1%s~", // showkey shows ^[[1 winterm.VK_HOME: "\x1B[1%s~", // showkey shows ^[[1
VK_END: "\x1B[4%s~", // showkey shows ^[[4 winterm.VK_END: "\x1B[4%s~", // showkey shows ^[[4
VK_INSERT: "\x1B[2%s~", winterm.VK_INSERT: "\x1B[2%s~",
VK_DELETE: "\x1B[3%s~", winterm.VK_DELETE: "\x1B[3%s~",
VK_PRIOR: "\x1B[5%s~", winterm.VK_PRIOR: "\x1B[5%s~",
VK_NEXT: "\x1B[6%s~", winterm.VK_NEXT: "\x1B[6%s~",
VK_F1: "", winterm.VK_F1: "",
VK_F2: "", winterm.VK_F2: "",
VK_F3: "\x1B[13%s~", winterm.VK_F3: "\x1B[13%s~",
VK_F4: "\x1B[14%s~", winterm.VK_F4: "\x1B[14%s~",
VK_F5: "\x1B[15%s~", winterm.VK_F5: "\x1B[15%s~",
VK_F6: "\x1B[17%s~", winterm.VK_F6: "\x1B[17%s~",
VK_F7: "\x1B[18%s~", winterm.VK_F7: "\x1B[18%s~",
VK_F8: "\x1B[19%s~", winterm.VK_F8: "\x1B[19%s~",
VK_F9: "\x1B[20%s~", winterm.VK_F9: "\x1B[20%s~",
VK_F10: "\x1B[21%s~", winterm.VK_F10: "\x1B[21%s~",
VK_F11: "\x1B[23%s~", winterm.VK_F11: "\x1B[23%s~",
VK_F12: "\x1B[24%s~", winterm.VK_F12: "\x1B[24%s~",
} }
// translateKeyEvents converts the input events into the appropriate ANSI string. // translateKeyEvents converts the input events into the appropriate ANSI string.
func translateKeyEvents(events []INPUT_RECORD, escapeSequence []byte) []byte { func translateKeyEvents(events []winterm.INPUT_RECORD, escapeSequence []byte) []byte {
var buffer bytes.Buffer var buffer bytes.Buffer
for _, event := range events { for _, event := range events {
if event.EventType == KEY_EVENT && event.KeyEvent.KeyDown != 0 { if event.EventType == winterm.KEY_EVENT && event.KeyEvent.KeyDown != 0 {
buffer.WriteString(keyToString(&event.KeyEvent, escapeSequence)) buffer.WriteString(keyToString(&event.KeyEvent, escapeSequence))
} }
} }
@ -180,7 +180,7 @@ func translateKeyEvents(events []INPUT_RECORD, escapeSequence []byte) []byte {
} }
// keyToString maps the given input event record to the corresponding string. // keyToString maps the given input event record to the corresponding string.
func keyToString(keyEvent *KEY_EVENT_RECORD, escapeSequence []byte) string { func keyToString(keyEvent *winterm.KEY_EVENT_RECORD, escapeSequence []byte) string {
if keyEvent.UnicodeChar == 0 { if keyEvent.UnicodeChar == 0 {
return formatVirtualKey(keyEvent.VirtualKeyCode, keyEvent.ControlKeyState, escapeSequence) return formatVirtualKey(keyEvent.VirtualKeyCode, keyEvent.ControlKeyState, escapeSequence)
} }
@ -199,14 +199,14 @@ func keyToString(keyEvent *KEY_EVENT_RECORD, escapeSequence []byte) string {
// <Alt>+Key generates ESC N Key // <Alt>+Key generates ESC N Key
if !control && alt { if !control && alt {
return KEY_ESC_N + strings.ToLower(string(keyEvent.UnicodeChar)) return ansiterm.KEY_ESC_N + strings.ToLower(string(keyEvent.UnicodeChar))
} }
return string(keyEvent.UnicodeChar) return string(keyEvent.UnicodeChar)
} }
// formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string. // formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string.
func formatVirtualKey(key WORD, controlState DWORD, escapeSequence []byte) string { func formatVirtualKey(key winterm.WORD, controlState winterm.DWORD, escapeSequence []byte) string {
shift, alt, control := getControlKeys(controlState) shift, alt, control := getControlKeys(controlState)
modifier := getControlKeysModifier(shift, alt, control, false) modifier := getControlKeysModifier(shift, alt, control, false)
@ -222,35 +222,35 @@ func formatVirtualKey(key WORD, controlState DWORD, escapeSequence []byte) strin
} }
// getControlKeys extracts the shift, alt, and ctrl key states. // getControlKeys extracts the shift, alt, and ctrl key states.
func getControlKeys(controlState DWORD) (shift, alt, control bool) { func getControlKeys(controlState winterm.DWORD) (shift, alt, control bool) {
shift = 0 != (controlState & SHIFT_PRESSED) shift = 0 != (controlState & winterm.SHIFT_PRESSED)
alt = 0 != (controlState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) alt = 0 != (controlState & (winterm.LEFT_ALT_PRESSED | winterm.RIGHT_ALT_PRESSED))
control = 0 != (controlState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) control = 0 != (controlState & (winterm.LEFT_CTRL_PRESSED | winterm.RIGHT_CTRL_PRESSED))
return shift, alt, control return shift, alt, control
} }
// getControlKeysModifier returns the ANSI modifier for the given combination of control keys. // getControlKeysModifier returns the ANSI modifier for the given combination of control keys.
func getControlKeysModifier(shift, alt, control, meta bool) string { func getControlKeysModifier(shift, alt, control, meta bool) string {
if shift && alt && control { if shift && alt && control {
return KEY_CONTROL_PARAM_8 return ansiterm.KEY_CONTROL_PARAM_8
} }
if alt && control { if alt && control {
return KEY_CONTROL_PARAM_7 return ansiterm.KEY_CONTROL_PARAM_7
} }
if shift && control { if shift && control {
return KEY_CONTROL_PARAM_6 return ansiterm.KEY_CONTROL_PARAM_6
} }
if control { if control {
return KEY_CONTROL_PARAM_5 return ansiterm.KEY_CONTROL_PARAM_5
} }
if shift && alt { if shift && alt {
return KEY_CONTROL_PARAM_4 return ansiterm.KEY_CONTROL_PARAM_4
} }
if alt { if alt {
return KEY_CONTROL_PARAM_3 return ansiterm.KEY_CONTROL_PARAM_3
} }
if shift { if shift {
return KEY_CONTROL_PARAM_2 return ansiterm.KEY_CONTROL_PARAM_2
} }
return "" return ""
} }

View file

@ -6,8 +6,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
. "github.com/Azure/go-ansiterm" ansiterm "github.com/Azure/go-ansiterm"
. "github.com/Azure/go-ansiterm/winterm" "github.com/Azure/go-ansiterm/winterm"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
) )
@ -17,17 +17,17 @@ var logger *logrus.Logger
type ansiWriter struct { type ansiWriter struct {
file *os.File file *os.File
fd uintptr fd uintptr
infoReset *CONSOLE_SCREEN_BUFFER_INFO infoReset *winterm.CONSOLE_SCREEN_BUFFER_INFO
command []byte command []byte
escapeSequence []byte escapeSequence []byte
inAnsiSequence bool inAnsiSequence bool
parser *AnsiParser parser *ansiterm.AnsiParser
} }
func newAnsiWriter(nFile int) *ansiWriter { func newAnsiWriter(nFile int) *ansiWriter {
logFile := ioutil.Discard logFile := ioutil.Discard
if isDebugEnv := os.Getenv(LogEnv); isDebugEnv == "1" { if isDebugEnv := os.Getenv(ansiterm.LogEnv); isDebugEnv == "1" {
logFile, _ = os.Create("ansiReaderWriter.log") logFile, _ = os.Create("ansiReaderWriter.log")
} }
@ -37,21 +37,21 @@ func newAnsiWriter(nFile int) *ansiWriter {
Level: logrus.DebugLevel, Level: logrus.DebugLevel,
} }
file, fd := GetStdFile(nFile) file, fd := winterm.GetStdFile(nFile)
info, err := GetConsoleScreenBufferInfo(fd) info, err := winterm.GetConsoleScreenBufferInfo(fd)
if err != nil { if err != nil {
return nil return nil
} }
parser := CreateParser("Ground", CreateWinEventHandler(fd, file)) parser := ansiterm.CreateParser("Ground", winterm.CreateWinEventHandler(fd, file))
logger.Infof("newAnsiWriter: parser %p", parser) logger.Infof("newAnsiWriter: parser %p", parser)
aw := &ansiWriter{ aw := &ansiWriter{
file: file, file: file,
fd: fd, fd: fd,
infoReset: info, infoReset: info,
command: make([]byte, 0, ANSI_MAX_CMD_LENGTH), command: make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
escapeSequence: []byte(KEY_ESC_CSI), escapeSequence: []byte(ansiterm.KEY_ESC_CSI),
parser: parser, parser: parser,
} }

View file

@ -7,10 +7,10 @@ import (
"os" "os"
"syscall" "syscall"
. "github.com/Azure/go-ansiterm/winterm" "github.com/Azure/go-ansiterm/winterm"
) )
// ConsoleStreams, for each standard stream referencing a console, returns a wrapped version // ConsoleStreams returns a wrapped version for each standard stream referencing a console,
// that handles ANSI character sequences. // that handles ANSI character sequences.
func ConsoleStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { func ConsoleStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
if IsConsole(os.Stdin.Fd()) { if IsConsole(os.Stdin.Fd()) {
@ -56,6 +56,6 @@ func GetHandleInfo(in interface{}) (uintptr, bool) {
// IsConsole returns true if the given file descriptor is a Windows Console. // IsConsole returns true if the given file descriptor is a Windows Console.
// The code assumes that GetConsoleMode will return an error for file descriptors that are not a console. // The code assumes that GetConsoleMode will return an error for file descriptors that are not a console.
func IsConsole(fd uintptr) bool { func IsConsole(fd uintptr) bool {
_, e := GetConsoleMode(fd) _, e := winterm.GetConsoleMode(fd)
return e == nil return e == nil
} }