console.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package console
  14. import (
  15. "errors"
  16. "io"
  17. "os"
  18. )
  19. var ErrNotAConsole = errors.New("provided file is not a console")
  20. type Console interface {
  21. io.Reader
  22. io.Writer
  23. io.Closer
  24. // Resize resizes the console to the provided window size
  25. Resize(WinSize) error
  26. // ResizeFrom resizes the calling console to the size of the
  27. // provided console
  28. ResizeFrom(Console) error
  29. // SetRaw sets the console in raw mode
  30. SetRaw() error
  31. // DisableEcho disables echo on the console
  32. DisableEcho() error
  33. // Reset restores the console to its orignal state
  34. Reset() error
  35. // Size returns the window size of the console
  36. Size() (WinSize, error)
  37. // Fd returns the console's file descriptor
  38. Fd() uintptr
  39. // Name returns the console's file name
  40. Name() string
  41. }
  42. // WinSize specifies the window size of the console
  43. type WinSize struct {
  44. // Height of the console
  45. Height uint16
  46. // Width of the console
  47. Width uint16
  48. x uint16
  49. y uint16
  50. }
  51. // Current returns the current processes console
  52. func Current() Console {
  53. c, err := ConsoleFromFile(os.Stdin)
  54. if err != nil {
  55. // stdin should always be a console for the design
  56. // of this function
  57. panic(err)
  58. }
  59. return c
  60. }
  61. // ConsoleFromFile returns a console using the provided file
  62. func ConsoleFromFile(f *os.File) (Console, error) {
  63. if err := checkConsole(f); err != nil {
  64. return nil, err
  65. }
  66. return newMaster(f)
  67. }