tty.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2021 The TCell Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use file except in compliance with the License.
  5. // You may obtain a copy of the license at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcell
  15. import "io"
  16. // Tty is an abstraction of a tty (traditionally "teletype"). This allows applications to
  17. // provide for alternate backends, as there are situations where the traditional /dev/tty
  18. // does not work, or where more flexible handling is required. This interface is for use
  19. // with the terminfo-style based API. It extends the io.ReadWriter API. It is reasonable
  20. // that the implementation might choose to use different underlying files for the Reader
  21. // and Writer sides of this API, as part of it's internal implementation.
  22. type Tty interface {
  23. // Start is used to activate the Tty for use. Upon return the terminal should be
  24. // in raw mode, non-blocking, etc. The implementation should take care of saving
  25. // any state that is required so that it may be restored when Stop is called.
  26. Start() error
  27. // Stop is used to stop using this Tty instance. This may be a suspend, so that other
  28. // terminal based applications can run in the foreground. Implementations should
  29. // restore any state collected at Start(), and return to ordinary blocking mode, etc.
  30. // Drain is called first to drain the input. Once this is called, no more Read
  31. // or Write calls will be made until Start is called again.
  32. Stop() error
  33. // Drain is called before Stop, and ensures that the reader will wake up appropriately
  34. // if it was blocked. This workaround is required for /dev/tty on certain UNIX systems
  35. // to ensure that Read() does not block forever. This typically arranges for the tty driver
  36. // to send data immediately (e.g. VMIN and VTIME both set zero) and sets a deadline on input.
  37. // Implementations may reasonably make this a no-op. There will still be control sequences
  38. // emitted between the time this is called, and when Stop is called.
  39. Drain() error
  40. // NotifyResize is used register a callback when the tty thinks the dimensions have
  41. // changed. The standard UNIX implementation links this to a handler for SIGWINCH.
  42. // If the supplied callback is nil, then any handler should be unregistered.
  43. NotifyResize(cb func())
  44. // WindowSize is called to determine the terminal dimensions. This might be determined
  45. // by an ioctl or other means.
  46. WindowSize() (width int, height int, err error)
  47. io.ReadWriteCloser
  48. }