util_windows.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "os"
  18. "golang.org/x/sys/windows"
  19. )
  20. type State struct {
  21. mode uint32
  22. }
  23. // IsTerminal returns whether the given file descriptor is a terminal.
  24. func IsTerminal(fd int) bool {
  25. var st uint32
  26. err := windows.GetConsoleMode(windows.Handle(fd), &st)
  27. return err == nil
  28. }
  29. // MakeRaw put the terminal connected to the given file descriptor into raw
  30. // mode and returns the previous state of the terminal so that it can be
  31. // restored.
  32. func MakeRaw(fd int) (*State, error) {
  33. var st uint32
  34. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  35. return nil, err
  36. }
  37. raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
  38. if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
  39. return nil, err
  40. }
  41. return &State{st}, nil
  42. }
  43. // GetState returns the current state of a terminal which may be useful to
  44. // restore the terminal after a signal.
  45. func GetState(fd int) (*State, error) {
  46. var st uint32
  47. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  48. return nil, err
  49. }
  50. return &State{st}, nil
  51. }
  52. // Restore restores the terminal connected to the given file descriptor to a
  53. // previous state.
  54. func Restore(fd int, state *State) error {
  55. return windows.SetConsoleMode(windows.Handle(fd), state.mode)
  56. }
  57. // GetSize returns the visible dimensions of the given terminal.
  58. //
  59. // These dimensions don't include any scrollback buffer height.
  60. func GetSize(fd int) (width, height int, err error) {
  61. var info windows.ConsoleScreenBufferInfo
  62. if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
  63. return 0, 0, err
  64. }
  65. return int(info.Window.Right - info.Window.Left + 1), int(info.Window.Bottom - info.Window.Top + 1), nil
  66. }
  67. // ReadPassword reads a line of input from a terminal without local echo. This
  68. // is commonly used for inputting passwords and other sensitive data. The slice
  69. // returned does not include the \n.
  70. func ReadPassword(fd int) ([]byte, error) {
  71. var st uint32
  72. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  73. return nil, err
  74. }
  75. old := st
  76. st &^= (windows.ENABLE_ECHO_INPUT)
  77. st |= (windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
  78. if err := windows.SetConsoleMode(windows.Handle(fd), st); err != nil {
  79. return nil, err
  80. }
  81. defer windows.SetConsoleMode(windows.Handle(fd), old)
  82. var h windows.Handle
  83. p, _ := windows.GetCurrentProcess()
  84. if err := windows.DuplicateHandle(p, windows.Handle(fd), p, &h, 0, false, windows.DUPLICATE_SAME_ACCESS); err != nil {
  85. return nil, err
  86. }
  87. f := os.NewFile(uintptr(h), "stdin")
  88. defer f.Close()
  89. return readPasswordLine(f)
  90. }