You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

104 lines
3.0 KiB

  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 true if 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 dimensions of the given terminal.
  58. func GetSize(fd int) (width, height int, err error) {
  59. var info windows.ConsoleScreenBufferInfo
  60. if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
  61. return 0, 0, err
  62. }
  63. return int(info.Size.X), int(info.Size.Y), nil
  64. }
  65. // ReadPassword reads a line of input from a terminal without local echo. This
  66. // is commonly used for inputting passwords and other sensitive data. The slice
  67. // returned does not include the \n.
  68. func ReadPassword(fd int) ([]byte, error) {
  69. var st uint32
  70. if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
  71. return nil, err
  72. }
  73. old := st
  74. st &^= (windows.ENABLE_ECHO_INPUT)
  75. st |= (windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
  76. if err := windows.SetConsoleMode(windows.Handle(fd), st); err != nil {
  77. return nil, err
  78. }
  79. defer windows.SetConsoleMode(windows.Handle(fd), old)
  80. var h windows.Handle
  81. p, _ := windows.GetCurrentProcess()
  82. if err := windows.DuplicateHandle(p, windows.Handle(fd), p, &h, 0, false, windows.DUPLICATE_SAME_ACCESS); err != nil {
  83. return nil, err
  84. }
  85. f := os.NewFile(uintptr(h), "stdin")
  86. defer f.Close()
  87. return readPasswordLine(f)
  88. }