term.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package term
  2. import "io"
  3. // State holds the platform-specific state / console mode for the terminal.
  4. type State terminalState
  5. // Winsize represents the size of the terminal window.
  6. type Winsize struct {
  7. Height uint16
  8. Width uint16
  9. // Only used on Unix
  10. x uint16
  11. y uint16
  12. }
  13. // StdStreams returns the standard streams (stdin, stdout, stderr).
  14. //
  15. // On Windows, it attempts to turn on VT handling on all std handles if
  16. // supported, or falls back to terminal emulation. On Unix, this returns
  17. // the standard [os.Stdin], [os.Stdout] and [os.Stderr].
  18. func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
  19. return stdStreams()
  20. }
  21. // GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
  22. func GetFdInfo(in interface{}) (fd uintptr, isTerminal bool) {
  23. return getFdInfo(in)
  24. }
  25. // GetWinsize returns the window size based on the specified file descriptor.
  26. func GetWinsize(fd uintptr) (*Winsize, error) {
  27. return getWinsize(fd)
  28. }
  29. // SetWinsize tries to set the specified window size for the specified file
  30. // descriptor. It is only implemented on Unix, and returns an error on Windows.
  31. func SetWinsize(fd uintptr, ws *Winsize) error {
  32. return setWinsize(fd, ws)
  33. }
  34. // IsTerminal returns true if the given file descriptor is a terminal.
  35. func IsTerminal(fd uintptr) bool {
  36. return isTerminal(fd)
  37. }
  38. // RestoreTerminal restores the terminal connected to the given file descriptor
  39. // to a previous state.
  40. func RestoreTerminal(fd uintptr, state *State) error {
  41. return restoreTerminal(fd, state)
  42. }
  43. // SaveState saves the state of the terminal connected to the given file descriptor.
  44. func SaveState(fd uintptr) (*State, error) {
  45. return saveState(fd)
  46. }
  47. // DisableEcho applies the specified state to the terminal connected to the file
  48. // descriptor, with echo disabled.
  49. func DisableEcho(fd uintptr, state *State) error {
  50. return disableEcho(fd, state)
  51. }
  52. // SetRawTerminal puts the terminal connected to the given file descriptor into
  53. // raw mode and returns the previous state. On UNIX, this is the equivalent of
  54. // [MakeRaw], and puts both the input and output into raw mode. On Windows, it
  55. // only puts the input into raw mode.
  56. func SetRawTerminal(fd uintptr) (previousState *State, err error) {
  57. return setRawTerminal(fd)
  58. }
  59. // SetRawTerminalOutput puts the output of terminal connected to the given file
  60. // descriptor into raw mode. On UNIX, this does nothing and returns nil for the
  61. // state. On Windows, it disables LF -> CRLF translation.
  62. func SetRawTerminalOutput(fd uintptr) (previousState *State, err error) {
  63. return setRawTerminalOutput(fd)
  64. }
  65. // MakeRaw puts the terminal (Windows Console) connected to the
  66. // given file descriptor into raw mode and returns the previous state of
  67. // the terminal so that it can be restored.
  68. func MakeRaw(fd uintptr) (previousState *State, err error) {
  69. return makeRaw(fd)
  70. }