github.com/pmorton/docker@v1.5.0/pkg/term/term_windows.go (about)

     1  // +build windows
     2  
     3  package term
     4  
     5  type State struct {
     6  	mode uint32
     7  }
     8  
     9  type Winsize struct {
    10  	Height uint16
    11  	Width  uint16
    12  	x      uint16
    13  	y      uint16
    14  }
    15  
    16  func GetWinsize(fd uintptr) (*Winsize, error) {
    17  	ws := &Winsize{}
    18  	var info *CONSOLE_SCREEN_BUFFER_INFO
    19  	info, err := GetConsoleScreenBufferInfo(fd)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	ws.Height = uint16(info.srWindow.Right - info.srWindow.Left + 1)
    24  	ws.Width = uint16(info.srWindow.Bottom - info.srWindow.Top + 1)
    25  
    26  	ws.x = 0 // todo azlinux -- this is the pixel size of the Window, and not currently used by any caller
    27  	ws.y = 0
    28  
    29  	return ws, nil
    30  }
    31  
    32  func SetWinsize(fd uintptr, ws *Winsize) error {
    33  	return nil
    34  }
    35  
    36  // IsTerminal returns true if the given file descriptor is a terminal.
    37  func IsTerminal(fd uintptr) bool {
    38  	_, e := GetConsoleMode(fd)
    39  	return e == nil
    40  }
    41  
    42  // Restore restores the terminal connected to the given file descriptor to a
    43  // previous state.
    44  func RestoreTerminal(fd uintptr, state *State) error {
    45  	return SetConsoleMode(fd, state.mode)
    46  }
    47  
    48  func SaveState(fd uintptr) (*State, error) {
    49  	mode, e := GetConsoleMode(fd)
    50  	if e != nil {
    51  		return nil, e
    52  	}
    53  	return &State{mode}, nil
    54  }
    55  
    56  // see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx for these flag settings
    57  func DisableEcho(fd uintptr, state *State) error {
    58  	state.mode &^= (ENABLE_ECHO_INPUT)
    59  	state.mode |= (ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT)
    60  	return SetConsoleMode(fd, state.mode)
    61  }
    62  
    63  func SetRawTerminal(fd uintptr) (*State, error) {
    64  	oldState, err := MakeRaw(fd)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	// TODO (azlinux): implement handling interrupt and restore state of terminal
    69  	return oldState, err
    70  }
    71  
    72  // MakeRaw puts the terminal connected to the given file descriptor into raw
    73  // mode and returns the previous state of the terminal so that it can be
    74  // restored.
    75  func MakeRaw(fd uintptr) (*State, error) {
    76  	var state *State
    77  	state, err := SaveState(fd)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx for these flag settings
    83  	state.mode &^= (ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT)
    84  	err = SetConsoleMode(fd, state.mode)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	return state, nil
    89  }