input.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package prompt
  2. import "bytes"
  3. // WinSize represents the width and height of terminal.
  4. type WinSize struct {
  5. Row uint16
  6. Col uint16
  7. }
  8. // ConsoleParser is an interface to abstract input layer.
  9. type ConsoleParser interface {
  10. // Setup should be called before starting input
  11. Setup() error
  12. // TearDown should be called after stopping input
  13. TearDown() error
  14. // GetWinSize returns WinSize object to represent width and height of terminal.
  15. GetWinSize() *WinSize
  16. // Read returns byte array.
  17. Read() ([]byte, error)
  18. }
  19. // GetKey returns Key correspond to input byte codes.
  20. func GetKey(b []byte) Key {
  21. for _, k := range ASCIISequences {
  22. if bytes.Equal(k.ASCIICode, b) {
  23. return k.Key
  24. }
  25. }
  26. return NotDefined
  27. }
  28. // ASCIISequences holds mappings of the key and byte array.
  29. var ASCIISequences = []*ASCIICode{
  30. {Key: Escape, ASCIICode: []byte{0x1b}},
  31. {Key: ControlSpace, ASCIICode: []byte{0x00}},
  32. {Key: ControlA, ASCIICode: []byte{0x1}},
  33. {Key: ControlB, ASCIICode: []byte{0x2}},
  34. {Key: ControlC, ASCIICode: []byte{0x3}},
  35. {Key: ControlD, ASCIICode: []byte{0x4}},
  36. {Key: ControlE, ASCIICode: []byte{0x5}},
  37. {Key: ControlF, ASCIICode: []byte{0x6}},
  38. {Key: ControlG, ASCIICode: []byte{0x7}},
  39. {Key: ControlH, ASCIICode: []byte{0x8}},
  40. //{Key: ControlI, ASCIICode: []byte{0x9}},
  41. //{Key: ControlJ, ASCIICode: []byte{0xa}},
  42. {Key: ControlK, ASCIICode: []byte{0xb}},
  43. {Key: ControlL, ASCIICode: []byte{0xc}},
  44. {Key: ControlM, ASCIICode: []byte{0xd}},
  45. {Key: ControlN, ASCIICode: []byte{0xe}},
  46. {Key: ControlO, ASCIICode: []byte{0xf}},
  47. {Key: ControlP, ASCIICode: []byte{0x10}},
  48. {Key: ControlQ, ASCIICode: []byte{0x11}},
  49. {Key: ControlR, ASCIICode: []byte{0x12}},
  50. {Key: ControlS, ASCIICode: []byte{0x13}},
  51. {Key: ControlT, ASCIICode: []byte{0x14}},
  52. {Key: ControlU, ASCIICode: []byte{0x15}},
  53. {Key: ControlV, ASCIICode: []byte{0x16}},
  54. {Key: ControlW, ASCIICode: []byte{0x17}},
  55. {Key: ControlX, ASCIICode: []byte{0x18}},
  56. {Key: ControlY, ASCIICode: []byte{0x19}},
  57. {Key: ControlZ, ASCIICode: []byte{0x1a}},
  58. {Key: ControlBackslash, ASCIICode: []byte{0x1c}},
  59. {Key: ControlSquareClose, ASCIICode: []byte{0x1d}},
  60. {Key: ControlCircumflex, ASCIICode: []byte{0x1e}},
  61. {Key: ControlUnderscore, ASCIICode: []byte{0x1f}},
  62. {Key: Backspace, ASCIICode: []byte{0x7f}},
  63. {Key: Up, ASCIICode: []byte{0x1b, 0x5b, 0x41}},
  64. {Key: Down, ASCIICode: []byte{0x1b, 0x5b, 0x42}},
  65. {Key: Right, ASCIICode: []byte{0x1b, 0x5b, 0x43}},
  66. {Key: Left, ASCIICode: []byte{0x1b, 0x5b, 0x44}},
  67. {Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x48}},
  68. {Key: Home, ASCIICode: []byte{0x1b, 0x30, 0x48}},
  69. {Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x46}},
  70. {Key: End, ASCIICode: []byte{0x1b, 0x30, 0x46}},
  71. {Key: Enter, ASCIICode: []byte{0xa}},
  72. {Key: Delete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x7e}},
  73. {Key: ShiftDelete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x3b, 0x32, 0x7e}},
  74. {Key: ControlDelete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x3b, 0x35, 0x7e}},
  75. {Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x7e}},
  76. {Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x34, 0x7e}},
  77. {Key: PageUp, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x7e}},
  78. {Key: PageDown, ASCIICode: []byte{0x1b, 0x5b, 0x36, 0x7e}},
  79. {Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x37, 0x7e}},
  80. {Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x38, 0x7e}},
  81. {Key: Tab, ASCIICode: []byte{0x9}},
  82. {Key: BackTab, ASCIICode: []byte{0x1b, 0x5b, 0x5a}},
  83. {Key: Insert, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x7e}},
  84. {Key: F1, ASCIICode: []byte{0x1b, 0x4f, 0x50}},
  85. {Key: F2, ASCIICode: []byte{0x1b, 0x4f, 0x51}},
  86. {Key: F3, ASCIICode: []byte{0x1b, 0x4f, 0x52}},
  87. {Key: F4, ASCIICode: []byte{0x1b, 0x4f, 0x53}},
  88. {Key: F1, ASCIICode: []byte{0x1b, 0x4f, 0x50, 0x41}}, // Linux console
  89. {Key: F2, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x42}}, // Linux console
  90. {Key: F3, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x43}}, // Linux console
  91. {Key: F4, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x44}}, // Linux console
  92. {Key: F5, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x45}}, // Linux console
  93. {Key: F1, ASCIICode: []byte{0x1b, 0x5b, 0x11, 0x7e}}, // rxvt-unicode
  94. {Key: F2, ASCIICode: []byte{0x1b, 0x5b, 0x12, 0x7e}}, // rxvt-unicode
  95. {Key: F3, ASCIICode: []byte{0x1b, 0x5b, 0x13, 0x7e}}, // rxvt-unicode
  96. {Key: F4, ASCIICode: []byte{0x1b, 0x5b, 0x14, 0x7e}}, // rxvt-unicode
  97. {Key: F5, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x35, 0x7e}},
  98. {Key: F6, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x37, 0x7e}},
  99. {Key: F7, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x38, 0x7e}},
  100. {Key: F8, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x39, 0x7e}},
  101. {Key: F9, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x30, 0x7e}},
  102. {Key: F10, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x31, 0x7e}},
  103. {Key: F11, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x32, 0x7e}},
  104. {Key: F12, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x34, 0x7e, 0x8}},
  105. {Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x25, 0x7e}},
  106. {Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x26, 0x7e}},
  107. {Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x28, 0x7e}},
  108. {Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x29, 0x7e}},
  109. {Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x7e}},
  110. {Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x7e}},
  111. {Key: F19, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x7e}},
  112. {Key: F20, ASCIICode: []byte{0x1b, 0x5b, 0x34, 0x7e}},
  113. // Xterm
  114. {Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x50}},
  115. {Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x51}},
  116. // &ASCIICode{Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}}, // Conflicts with CPR response
  117. {Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}},
  118. {Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x15, 0x3b, 0x32, 0x7e}},
  119. {Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x17, 0x3b, 0x32, 0x7e}},
  120. {Key: F19, ASCIICode: []byte{0x1b, 0x5b, 0x18, 0x3b, 0x32, 0x7e}},
  121. {Key: F20, ASCIICode: []byte{0x1b, 0x5b, 0x19, 0x3b, 0x32, 0x7e}},
  122. {Key: F21, ASCIICode: []byte{0x1b, 0x5b, 0x20, 0x3b, 0x32, 0x7e}},
  123. {Key: F22, ASCIICode: []byte{0x1b, 0x5b, 0x21, 0x3b, 0x32, 0x7e}},
  124. {Key: F23, ASCIICode: []byte{0x1b, 0x5b, 0x23, 0x3b, 0x32, 0x7e}},
  125. {Key: F24, ASCIICode: []byte{0x1b, 0x5b, 0x24, 0x3b, 0x32, 0x7e}},
  126. {Key: ControlUp, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x41}},
  127. {Key: ControlDown, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x42}},
  128. {Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x43}},
  129. {Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x44}},
  130. {Key: ShiftUp, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x41}},
  131. {Key: ShiftDown, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x42}},
  132. {Key: ShiftRight, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x43}},
  133. {Key: ShiftLeft, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x44}},
  134. // Tmux sends following keystrokes when control+arrow is pressed, but for
  135. // Emacs ansi-term sends the same sequences for normal arrow keys. Consider
  136. // it a normal arrow press, because that's more important.
  137. {Key: Up, ASCIICode: []byte{0x1b, 0x4f, 0x41}},
  138. {Key: Down, ASCIICode: []byte{0x1b, 0x4f, 0x42}},
  139. {Key: Right, ASCIICode: []byte{0x1b, 0x4f, 0x43}},
  140. {Key: Left, ASCIICode: []byte{0x1b, 0x4f, 0x44}},
  141. {Key: ControlUp, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x41}},
  142. {Key: ControlDown, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x42}},
  143. {Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x43}},
  144. {Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x44}},
  145. {Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x4f, 0x63}}, // rxvt
  146. {Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x4f, 0x64}}, // rxvt
  147. {Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x45}}, // Xterm
  148. {Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x46}}, // Linux console
  149. }