proxy.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package term
  2. import (
  3. "io"
  4. )
  5. // EscapeError is special error which returned by a TTY proxy reader's Read()
  6. // method in case its detach escape sequence is read.
  7. type EscapeError struct{}
  8. func (EscapeError) Error() string {
  9. return "read escape sequence"
  10. }
  11. // escapeProxy is used only for attaches with a TTY. It is used to proxy
  12. // stdin keypresses from the underlying reader and look for the passed in
  13. // escape key sequence to signal a detach.
  14. type escapeProxy struct {
  15. escapeKeys []byte
  16. escapeKeyPos int
  17. r io.Reader
  18. buf []byte
  19. }
  20. // NewEscapeProxy returns a new TTY proxy reader which wraps the given reader
  21. // and detects when the specified escape keys are read, in which case the Read
  22. // method will return an error of type EscapeError.
  23. func NewEscapeProxy(r io.Reader, escapeKeys []byte) io.Reader {
  24. return &escapeProxy{
  25. escapeKeys: escapeKeys,
  26. r: r,
  27. }
  28. }
  29. func (r *escapeProxy) Read(buf []byte) (n int, err error) {
  30. if len(r.escapeKeys) > 0 && r.escapeKeyPos == len(r.escapeKeys) {
  31. return 0, EscapeError{}
  32. }
  33. if len(r.buf) > 0 {
  34. n = copy(buf, r.buf)
  35. r.buf = r.buf[n:]
  36. }
  37. nr, err := r.r.Read(buf[n:])
  38. n += nr
  39. if len(r.escapeKeys) == 0 {
  40. return n, err
  41. }
  42. for i := 0; i < n; i++ {
  43. if buf[i] == r.escapeKeys[r.escapeKeyPos] {
  44. r.escapeKeyPos++
  45. // Check if the full escape sequence is matched.
  46. if r.escapeKeyPos == len(r.escapeKeys) {
  47. n = i + 1 - r.escapeKeyPos
  48. if n < 0 {
  49. n = 0
  50. }
  51. return n, EscapeError{}
  52. }
  53. continue
  54. }
  55. // If we need to prepend a partial escape sequence from the previous
  56. // read, make sure the new buffer size doesn't exceed len(buf).
  57. // Otherwise, preserve any extra data in a buffer for the next read.
  58. if i < r.escapeKeyPos {
  59. preserve := make([]byte, 0, r.escapeKeyPos+n)
  60. preserve = append(preserve, r.escapeKeys[:r.escapeKeyPos]...)
  61. preserve = append(preserve, buf[:n]...)
  62. n = copy(buf, preserve)
  63. i += r.escapeKeyPos
  64. r.buf = append(r.buf, preserve[n:]...)
  65. }
  66. r.escapeKeyPos = 0
  67. }
  68. // If we're in the middle of reading an escape sequence, make sure we don't
  69. // let the caller read it. If later on we find that this is not the escape
  70. // sequence, we'll prepend it back to buf.
  71. n -= r.escapeKeyPos
  72. if n < 0 {
  73. n = 0
  74. }
  75. return n, err
  76. }