key_bind.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package prompt
  2. // KeyBindFunc receives buffer and processed it.
  3. type KeyBindFunc func(*Buffer)
  4. // KeyBind represents which key should do what operation.
  5. type KeyBind struct {
  6. Key Key
  7. Fn KeyBindFunc
  8. }
  9. // ASCIICodeBind represents which []byte should do what operation
  10. type ASCIICodeBind struct {
  11. ASCIICode []byte
  12. Fn KeyBindFunc
  13. }
  14. // KeyBindMode to switch a key binding flexibly.
  15. type KeyBindMode string
  16. const (
  17. // CommonKeyBind is a mode without any keyboard shortcut
  18. CommonKeyBind KeyBindMode = "common"
  19. // EmacsKeyBind is a mode to use emacs-like keyboard shortcut
  20. EmacsKeyBind KeyBindMode = "emacs"
  21. )
  22. var commonKeyBindings = []KeyBind{
  23. // Go to the End of the line
  24. {
  25. Key: End,
  26. Fn: GoLineEnd,
  27. },
  28. // Go to the beginning of the line
  29. {
  30. Key: Home,
  31. Fn: GoLineBeginning,
  32. },
  33. // Delete character under the cursor
  34. {
  35. Key: Delete,
  36. Fn: DeleteChar,
  37. },
  38. // Backspace
  39. {
  40. Key: Backspace,
  41. Fn: DeleteBeforeChar,
  42. },
  43. // Right allow: Forward one character
  44. {
  45. Key: Right,
  46. Fn: GoRightChar,
  47. },
  48. // Left allow: Backward one character
  49. {
  50. Key: Left,
  51. Fn: GoLeftChar,
  52. },
  53. }