kitty.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package ansi
  2. import "strconv"
  3. // Kitty keyboard protocol progressive enhancement flags.
  4. // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
  5. const (
  6. KittyDisambiguateEscapeCodes = 1 << iota
  7. KittyReportEventTypes
  8. KittyReportAlternateKeys
  9. KittyReportAllKeysAsEscapeCodes
  10. KittyReportAssociatedKeys
  11. KittyAllFlags = KittyDisambiguateEscapeCodes | KittyReportEventTypes |
  12. KittyReportAlternateKeys | KittyReportAllKeysAsEscapeCodes | KittyReportAssociatedKeys
  13. )
  14. // RequestKittyKeyboard is a sequence to request the terminal Kitty keyboard
  15. // protocol enabled flags.
  16. //
  17. // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
  18. const RequestKittyKeyboard = "\x1b[?u"
  19. // KittyKeyboard returns a sequence to request keyboard enhancements from the terminal.
  20. // The flags argument is a bitmask of the Kitty keyboard protocol flags. While
  21. // mode specifies how the flags should be interpreted.
  22. //
  23. // Possible values for flags mask:
  24. //
  25. // 1: Disambiguate escape codes
  26. // 2: Report event types
  27. // 4: Report alternate keys
  28. // 8: Report all keys as escape codes
  29. // 16: Report associated text
  30. //
  31. // Possible values for mode:
  32. //
  33. // 1: Set given flags and unset all others
  34. // 2: Set given flags and keep existing flags unchanged
  35. // 3: Unset given flags and keep existing flags unchanged
  36. //
  37. // See https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
  38. func KittyKeyboard(flags, mode int) string {
  39. return "\x1b[=" + strconv.Itoa(flags) + ";" + strconv.Itoa(mode) + "u"
  40. }
  41. // PushKittyKeyboard returns a sequence to push the given flags to the terminal
  42. // Kitty Keyboard stack.
  43. //
  44. // Possible values for flags mask:
  45. //
  46. // 0: Disable all features
  47. // 1: Disambiguate escape codes
  48. // 2: Report event types
  49. // 4: Report alternate keys
  50. // 8: Report all keys as escape codes
  51. // 16: Report associated text
  52. //
  53. // CSI > flags u
  54. //
  55. // See https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
  56. func PushKittyKeyboard(flags int) string {
  57. var f string
  58. if flags > 0 {
  59. f = strconv.Itoa(flags)
  60. }
  61. return "\x1b[>" + f + "u"
  62. }
  63. // DisableKittyKeyboard is a sequence to push zero into the terminal Kitty
  64. // Keyboard stack to disable the protocol.
  65. //
  66. // This is equivalent to PushKittyKeyboard(0).
  67. const DisableKittyKeyboard = "\x1b[>u"
  68. // PopKittyKeyboard returns a sequence to pop n number of flags from the
  69. // terminal Kitty Keyboard stack.
  70. //
  71. // CSI < flags u
  72. //
  73. // See https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
  74. func PopKittyKeyboard(n int) string {
  75. var num string
  76. if n > 0 {
  77. num = strconv.Itoa(n)
  78. }
  79. return "\x1b[<" + num + "u"
  80. }