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