term.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 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. // Package term provides support functions for dealing with terminals, as
  5. // commonly found on UNIX systems.
  6. //
  7. // Putting a terminal into raw mode is the most common requirement:
  8. //
  9. // oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
  10. // if err != nil {
  11. // panic(err)
  12. // }
  13. // defer term.Restore(int(os.Stdin.Fd()), oldState)
  14. //
  15. // Note that on non-Unix systems os.Stdin.Fd() may not be 0.
  16. package term
  17. // State contains the state of a terminal.
  18. type State struct {
  19. state
  20. }
  21. // IsTerminal returns whether the given file descriptor is a terminal.
  22. func IsTerminal(fd int) bool {
  23. return isTerminal(fd)
  24. }
  25. // MakeRaw puts the terminal connected to the given file descriptor into raw
  26. // mode and returns the previous state of the terminal so that it can be
  27. // restored.
  28. func MakeRaw(fd int) (*State, error) {
  29. return makeRaw(fd)
  30. }
  31. // GetState returns the current state of a terminal which may be useful to
  32. // restore the terminal after a signal.
  33. func GetState(fd int) (*State, error) {
  34. return getState(fd)
  35. }
  36. // Restore restores the terminal connected to the given file descriptor to a
  37. // previous state.
  38. func Restore(fd int, oldState *State) error {
  39. return restore(fd, oldState)
  40. }
  41. // GetSize returns the visible dimensions of the given terminal.
  42. //
  43. // These dimensions don't include any scrollback buffer height.
  44. func GetSize(fd int) (width, height int, err error) {
  45. return getSize(fd)
  46. }
  47. // ReadPassword reads a line of input from a terminal without local echo. This
  48. // is commonly used for inputting passwords and other sensitive data. The slice
  49. // returned does not include the \n.
  50. func ReadPassword(fd int) ([]byte, error) {
  51. return readPassword(fd)
  52. }