console.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package console
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. )
  7. var ErrNotAConsole = errors.New("provided file is not a console")
  8. type Console interface {
  9. io.Reader
  10. io.Writer
  11. io.Closer
  12. // Resize resizes the console to the provided window size
  13. Resize(WinSize) error
  14. // ResizeFrom resizes the calling console to the size of the
  15. // provided console
  16. ResizeFrom(Console) error
  17. // SetRaw sets the console in raw mode
  18. SetRaw() error
  19. // DisableEcho disables echo on the console
  20. DisableEcho() error
  21. // Reset restores the console to its orignal state
  22. Reset() error
  23. // Size returns the window size of the console
  24. Size() (WinSize, error)
  25. // Fd returns the console's file descriptor
  26. Fd() uintptr
  27. // Name returns the console's file name
  28. Name() string
  29. }
  30. // WinSize specifies the window size of the console
  31. type WinSize struct {
  32. // Height of the console
  33. Height uint16
  34. // Width of the console
  35. Width uint16
  36. x uint16
  37. y uint16
  38. }
  39. // Current returns the current processes console
  40. func Current() Console {
  41. c, err := ConsoleFromFile(os.Stdin)
  42. if err != nil {
  43. // stdin should always be a console for the design
  44. // of this function
  45. panic(err)
  46. }
  47. return c
  48. }
  49. // ConsoleFromFile returns a console using the provided file
  50. func ConsoleFromFile(f *os.File) (Console, error) {
  51. if err := checkConsole(f); err != nil {
  52. return nil, err
  53. }
  54. return newMaster(f)
  55. }