console.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 File interface {
  21. io.ReadWriteCloser
  22. // Fd returns its file descriptor
  23. Fd() uintptr
  24. // Name returns its file name
  25. Name() string
  26. }
  27. type Console interface {
  28. File
  29. // Resize resizes the console to the provided window size
  30. Resize(WinSize) error
  31. // ResizeFrom resizes the calling console to the size of the
  32. // provided console
  33. ResizeFrom(Console) error
  34. // SetRaw sets the console in raw mode
  35. SetRaw() error
  36. // DisableEcho disables echo on the console
  37. DisableEcho() error
  38. // Reset restores the console to its orignal state
  39. Reset() error
  40. // Size returns the window size of the console
  41. Size() (WinSize, error)
  42. }
  43. // WinSize specifies the window size of the console
  44. type WinSize struct {
  45. // Height of the console
  46. Height uint16
  47. // Width of the console
  48. Width uint16
  49. x uint16
  50. y uint16
  51. }
  52. // Current returns the current processes console
  53. func Current() Console {
  54. c, err := ConsoleFromFile(os.Stdin)
  55. if err != nil {
  56. // stdin should always be a console for the design
  57. // of this function
  58. panic(err)
  59. }
  60. return c
  61. }
  62. // ConsoleFromFile returns a console using the provided file
  63. func ConsoleFromFile(f File) (Console, error) {
  64. if err := checkConsole(f); err != nil {
  65. return nil, err
  66. }
  67. return newMaster(f)
  68. }