From c23a02e41afd0fd218a0b0c8161866d7314fb0f1 Mon Sep 17 00:00:00 2001 From: Sachin Joshi Date: Thu, 5 Mar 2015 16:41:48 -0800 Subject: [PATCH] Move windows console specific implementation in sub package Signed-off-by: Sachin Joshi --- term/term_windows.go | 33 +++++++++++++------ term/{ => winconsole}/console_windows.go | 15 ++++++--- term/{ => winconsole}/console_windows_test.go | 2 +- term/{ => winconsole}/term_emulator.go | 2 +- term/{ => winconsole}/term_emulator_test.go | 2 +- 5 files changed, 36 insertions(+), 18 deletions(-) rename term/{ => winconsole}/console_windows.go (99%) rename term/{ => winconsole}/console_windows_test.go (99%) rename term/{ => winconsole}/term_emulator.go (99%) rename term/{ => winconsole}/term_emulator_test.go (99%) diff --git a/term/term_windows.go b/term/term_windows.go index ea4ba53..711f744 100644 --- a/term/term_windows.go +++ b/term/term_windows.go @@ -1,7 +1,11 @@ // +build windows - package term +import ( + "github.com/docker/docker/pkg/term/winconsole" + "io" +) + // State holds the console mode for the terminal. type State struct { mode uint32 @@ -18,8 +22,8 @@ type Winsize struct { // GetWinsize gets the window size of the given terminal func GetWinsize(fd uintptr) (*Winsize, error) { ws := &Winsize{} - var info *CONSOLE_SCREEN_BUFFER_INFO - info, err := GetConsoleScreenBufferInfo(fd) + var info *winconsole.CONSOLE_SCREEN_BUFFER_INFO + info, err := winconsole.GetConsoleScreenBufferInfo(fd) if err != nil { return nil, err } @@ -41,19 +45,19 @@ func SetWinsize(fd uintptr, ws *Winsize) error { // IsTerminal returns true if the given file descriptor is a terminal. func IsTerminal(fd uintptr) bool { - _, e := GetConsoleMode(fd) + _, e := winconsole.GetConsoleMode(fd) return e == nil } // RestoreTerminal restores the terminal connected to the given file descriptor to a // previous state. func RestoreTerminal(fd uintptr, state *State) error { - return SetConsoleMode(fd, state.mode) + return winconsole.SetConsoleMode(fd, state.mode) } // SaveState saves the state of the given console func SaveState(fd uintptr) (*State, error) { - mode, e := GetConsoleMode(fd) + mode, e := winconsole.GetConsoleMode(fd) if e != nil { return nil, e } @@ -63,9 +67,9 @@ func SaveState(fd uintptr) (*State, error) { // DisableEcho disbales the echo for given file descriptor and returns previous state // see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx for these flag settings func DisableEcho(fd uintptr, state *State) error { - state.mode &^= (ENABLE_ECHO_INPUT) - state.mode |= (ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT) - return SetConsoleMode(fd, state.mode) + state.mode &^= (winconsole.ENABLE_ECHO_INPUT) + state.mode |= (winconsole.ENABLE_PROCESSED_INPUT | winconsole.ENABLE_LINE_INPUT) + return winconsole.SetConsoleMode(fd, state.mode) } // SetRawTerminal puts the terminal connected to the given file descriptor into raw @@ -96,9 +100,18 @@ func MakeRaw(fd uintptr) (*State, error) { // When all are enabled, the application is said to be in "cooked" mode, which means that most of the processing is handled for the application. // When all are disabled, the application is in "raw" mode, which means that input is unfiltered and any processing is left to the application. state.mode = 0 - err = SetConsoleMode(fd, state.mode) + err = winconsole.SetConsoleMode(fd, state.mode) if err != nil { return nil, err } return state, nil } + +// GetHandleInfo returns file descriptor and bool indicating whether the file is a terminal +func GetHandleInfo(in interface{}) (uintptr, bool) { + return winconsole.GetHandleInfo(in) +} + +func StdStreams() (stdOut io.Writer, stdErr io.Writer, stdIn io.ReadCloser) { + return winconsole.StdStreams() +} diff --git a/term/console_windows.go b/term/winconsole/console_windows.go similarity index 99% rename from term/console_windows.go rename to term/winconsole/console_windows.go index 5306920..8121aee 100644 --- a/term/console_windows.go +++ b/term/winconsole/console_windows.go @@ -1,6 +1,6 @@ // +build windows -package term +package winconsole import ( "bytes" @@ -415,7 +415,7 @@ func getNumberOfChars(fromCoord COORD, toCoord COORD, screenSize COORD) uint32 { return 0 } -func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { +func clearDisplayRect(fileDesc uintptr, fillChar rune, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { var writeRegion SMALL_RECT writeRegion.Top = fromCoord.Y writeRegion.Left = fromCoord.X @@ -429,7 +429,7 @@ func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoor if size > 0 { buffer := make([]CHAR_INFO, size) for i := 0; i < len(buffer); i++ { - buffer[i].UnicodeChar = WCHAR(string(fillChar)[0]) + buffer[i].UnicodeChar = WCHAR(fillChar) buffer[i].Attributes = attributes } @@ -445,7 +445,7 @@ func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoor return true, uint32(size), nil } -func clearDisplayRange(fileDesc uintptr, fillChar byte, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { +func clearDisplayRange(fileDesc uintptr, fillChar rune, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { nw := uint32(0) // start and end on same line if fromCoord.Y == toCoord.Y { @@ -531,7 +531,6 @@ func getNumberOfConsoleInputEvents(fileDesc uintptr) (uint16, error) { r, _, err := getNumberOfConsoleInputEventsProc.Call(uintptr(fileDesc), uintptr(unsafe.Pointer(&n))) //If the function succeeds, the return value is nonzero if r != 0 { - //fmt.Printf("################%d #################\n", n) return uint16(n), nil } return 0, err @@ -1103,3 +1102,9 @@ func marshal(c COORD) uint32 { // works only on intel-endian machines return uint32(uint32(uint16(c.Y))<<16 | uint32(uint16(c.X))) } + +// IsTerminal returns true if the given file descriptor is a terminal. +func IsTerminal(fd uintptr) bool { + _, e := GetConsoleMode(fd) + return e == nil +} diff --git a/term/console_windows_test.go b/term/winconsole/console_windows_test.go similarity index 99% rename from term/console_windows_test.go rename to term/winconsole/console_windows_test.go index 01c2572..ee9d968 100644 --- a/term/console_windows_test.go +++ b/term/winconsole/console_windows_test.go @@ -1,6 +1,6 @@ // +build windows -package term +package winconsole import ( "fmt" diff --git a/term/term_emulator.go b/term/winconsole/term_emulator.go similarity index 99% rename from term/term_emulator.go rename to term/winconsole/term_emulator.go index 1713f42..13bcde8 100644 --- a/term/term_emulator.go +++ b/term/winconsole/term_emulator.go @@ -1,4 +1,4 @@ -package term +package winconsole import ( "io" diff --git a/term/term_emulator_test.go b/term/winconsole/term_emulator_test.go similarity index 99% rename from term/term_emulator_test.go rename to term/winconsole/term_emulator_test.go index 7a9e1ab..7017d42 100644 --- a/term/term_emulator_test.go +++ b/term/winconsole/term_emulator_test.go @@ -1,4 +1,4 @@ -package term +package winconsole import ( "bytes"