util_windows.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. // Package terminal provides support functions for dealing with terminals, as
  6. // commonly found on UNIX systems.
  7. //
  8. // Putting a terminal into raw mode is the most common requirement:
  9. //
  10. // oldState, err := terminal.MakeRaw(0)
  11. // if err != nil {
  12. // panic(err)
  13. // }
  14. // defer terminal.Restore(0, oldState)
  15. package terminal
  16. import (
  17. "golang.org/x/sys/windows"
  18. )
  19. type State struct {
  20. mode uint32
  21. }
  22. // IsTerminal returns true if the given file descriptor is a terminal.
  23. func IsTerminal(fd int) bool {
  24. var st uint32
  25. err := windows.GetConsoleMode(windows.Handle(fd), &st)
  26. return err == nil
  27. }
  28. // MakeRaw put the terminal connected to the given file descriptor into raw
  29. // mode and returns the previous state of the terminal so that it can be
  30. // restored.
  31. func MakeRaw(fd int) (*State, error) {
  32. var st uint32
  33. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  34. return nil, err
  35. }
  36. raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
  37. if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
  38. return nil, err
  39. }
  40. return &State{st}, nil
  41. }
  42. // GetState returns the current state of a terminal which may be useful to
  43. // restore the terminal after a signal.
  44. func GetState(fd int) (*State, error) {
  45. var st uint32
  46. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  47. return nil, err
  48. }
  49. return &State{st}, nil
  50. }
  51. // Restore restores the terminal connected to the given file descriptor to a
  52. // previous state.
  53. func Restore(fd int, state *State) error {
  54. return windows.SetConsoleMode(windows.Handle(fd), state.mode)
  55. }
  56. // GetSize returns the dimensions of the given terminal.
  57. func GetSize(fd int) (width, height int, err error) {
  58. var info windows.ConsoleScreenBufferInfo
  59. if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
  60. return 0, 0, err
  61. }
  62. return int(info.Size.X), int(info.Size.Y), nil
  63. }
  64. // passwordReader is an io.Reader that reads from a specific Windows HANDLE.
  65. type passwordReader int
  66. func (r passwordReader) Read(buf []byte) (int, error) {
  67. return windows.Read(windows.Handle(r), buf)
  68. }
  69. // ReadPassword reads a line of input from a terminal without local echo. This
  70. // is commonly used for inputting passwords and other sensitive data. The slice
  71. // returned does not include the \n.
  72. func ReadPassword(fd int) ([]byte, error) {
  73. var st uint32
  74. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  75. return nil, err
  76. }
  77. old := st
  78. st &^= (windows.ENABLE_ECHO_INPUT)
  79. st |= (windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
  80. if err := windows.SetConsoleMode(windows.Handle(fd), st); err != nil {
  81. return nil, err
  82. }
  83. defer func() {
  84. windows.SetConsoleMode(windows.Handle(fd), old)
  85. }()
  86. return readPasswordLine(passwordReader(fd))
  87. }