| 1 |
//go:build tcell || windows |
| 2 |
|
| 3 |
package tui |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/gdamore/tcell/v2" |
| 10 |
"github.com/junegunn/fzf/src/util" |
| 11 |
) |
| 12 |
|
| 13 |
func assert(t *testing.T, context string, got any, want any) bool { |
| 14 |
if got == want { |
| 15 |
return true |
| 16 |
} else { |
| 17 |
t.Errorf("%s = (%T)%v, want (%T)%v", context, got, got, want, want) |
| 18 |
return false |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// Test the handling of the tcell keyboard events. |
| 23 |
func TestGetCharEventKey(t *testing.T) { |
| 24 |
if util.IsTty(os.Stdout) { |
| 25 |
// This test is skipped when output goes to terminal, because it causes |
| 26 |
// some glitches: |
| 27 |
// - output lines may not start at the beginning of a row which makes |
| 28 |
// the output unreadable |
| 29 |
// - terminal may get cleared which prevents you from seeing results of |
| 30 |
// previous tests |
| 31 |
// Good ways to prevent the glitches are piping the output to a pager |
| 32 |
// or redirecting to a file. I've found `less +G` to be trouble-free. |
| 33 |
t.Skip("Skipped because this test misbehaves in terminal, pipe to a pager or redirect output to a file to run it safely.") |
| 34 |
} else if testing.Verbose() { |
| 35 |
// I have observed a behaviour when this test outputted more than 8192 |
| 36 |
// bytes (32*256) into the 'less' pager, both the go's test executable |
| 37 |
// and the pager hanged. The go's executable was blocking on printing. |
| 38 |
// I was able to create minimal working example of that behaviour, but |
| 39 |
// that example hanged after 12256 bytes (32*(256+127)). |
| 40 |
t.Log("If you are piping this test to a pager and it hangs, make the pager greedy for input, e.g. 'less +G'.") |
| 41 |
} |
| 42 |
|
| 43 |
if !HasFullscreenRenderer() { |
| 44 |
t.Skip("Can't test FullscreenRenderer.") |
| 45 |
} |
| 46 |
|
| 47 |
// construct test cases |
| 48 |
type giveKey struct { |
| 49 |
Type tcell.Key |
| 50 |
Char rune |
| 51 |
Mods tcell.ModMask |
| 52 |
} |
| 53 |
type wantKey = Event |
| 54 |
type testCase struct { |
| 55 |
giveKey |
| 56 |
wantKey |
| 57 |
} |
| 58 |
/* |
| 59 |
Some test cases are marked "fabricated". It means that giveKey value |
| 60 |
is valid, but it is not what you get when you press the keys. For |
| 61 |
example Ctrl+C will NOT give you tcell.KeyCtrlC, but tcell.KeyETX |
| 62 |
(End-Of-Text character, causing SIGINT). |
| 63 |
I was trying to accompany the fabricated test cases with real ones. |
| 64 |
|
| 65 |
Some test cases are marked "unhandled". It means that giveKey.Type |
| 66 |
is not present in tcell.go source code. It can still be handled via |
| 67 |
implicit or explicit alias. |
| 68 |
|
| 69 |
If not said otherwise, test cases are for US keyboard. |
| 70 |
|
| 71 |
(tabstop=44) |
| 72 |
*/ |
| 73 |
tests := []testCase{ |
| 74 |
|
| 75 |
// section 1: Ctrl+(Alt)+[a-z] |
| 76 |
{giveKey{tcell.KeyCtrlA, rune(tcell.KeyCtrlA), tcell.ModCtrl}, wantKey{CtrlA, 0, nil}}, |
| 77 |
{giveKey{tcell.KeyCtrlC, rune(tcell.KeyCtrlC), tcell.ModCtrl}, wantKey{CtrlC, 0, nil}}, // fabricated |
| 78 |
{giveKey{tcell.KeyETX, rune(tcell.KeyETX), tcell.ModCtrl}, wantKey{CtrlC, 0, nil}}, // this is SIGINT (Ctrl+C) |
| 79 |
{giveKey{tcell.KeyCtrlZ, rune(tcell.KeyCtrlZ), tcell.ModCtrl}, wantKey{CtrlZ, 0, nil}}, // fabricated |
| 80 |
// KeyTab is alias for KeyTAB |
| 81 |
{giveKey{tcell.KeyCtrlI, rune(tcell.KeyCtrlI), tcell.ModCtrl}, wantKey{Tab, 0, nil}}, // fabricated |
| 82 |
{giveKey{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone}, wantKey{Tab, 0, nil}}, // unhandled, actual "Tab" keystroke |
| 83 |
{giveKey{tcell.KeyTAB, rune(tcell.KeyTAB), tcell.ModNone}, wantKey{Tab, 0, nil}}, // fabricated, unhandled |
| 84 |
// KeyEnter is alias for KeyCR |
| 85 |
{giveKey{tcell.KeyCtrlM, rune(tcell.KeyCtrlM), tcell.ModNone}, wantKey{Enter, 0, nil}}, // actual "Enter" keystroke |
| 86 |
{giveKey{tcell.KeyCR, rune(tcell.KeyCR), tcell.ModNone}, wantKey{Enter, 0, nil}}, // fabricated, unhandled |
| 87 |
{giveKey{tcell.KeyEnter, rune(tcell.KeyEnter), tcell.ModNone}, wantKey{Enter, 0, nil}}, // fabricated, unhandled |
| 88 |
// Ctrl+Alt keys |
| 89 |
{giveKey{tcell.KeyCtrlA, rune(tcell.KeyCtrlA), tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAlt, 'a', nil}}, // fabricated |
| 90 |
{giveKey{tcell.KeyCtrlA, rune(tcell.KeyCtrlA), tcell.ModCtrl | tcell.ModAlt | tcell.ModShift}, wantKey{CtrlAlt, 'a', nil}}, // fabricated |
| 91 |
|
| 92 |
// section 2: Ctrl+[ \]_] |
| 93 |
{giveKey{tcell.KeyCtrlSpace, rune(tcell.KeyCtrlSpace), tcell.ModCtrl}, wantKey{CtrlSpace, 0, nil}}, // fabricated |
| 94 |
{giveKey{tcell.KeyNUL, rune(tcell.KeyNUL), tcell.ModNone}, wantKey{CtrlSpace, 0, nil}}, // fabricated, unhandled |
| 95 |
{giveKey{tcell.KeyRune, ' ', tcell.ModCtrl}, wantKey{CtrlSpace, 0, nil}}, // actual Ctrl+' ' |
| 96 |
{giveKey{tcell.KeyCtrlBackslash, rune(tcell.KeyCtrlBackslash), tcell.ModCtrl}, wantKey{CtrlBackSlash, 0, nil}}, |
| 97 |
{giveKey{tcell.KeyCtrlRightSq, rune(tcell.KeyCtrlRightSq), tcell.ModCtrl}, wantKey{CtrlRightBracket, 0, nil}}, |
| 98 |
{giveKey{tcell.KeyCtrlCarat, rune(tcell.KeyCtrlCarat), tcell.ModShift | tcell.ModCtrl}, wantKey{CtrlCaret, 0, nil}}, // fabricated |
| 99 |
{giveKey{tcell.KeyRS, rune(tcell.KeyRS), tcell.ModShift | tcell.ModCtrl}, wantKey{CtrlCaret, 0, nil}}, // actual Ctrl+Shift+6 (i.e. Ctrl+^) keystroke |
| 100 |
{giveKey{tcell.KeyCtrlUnderscore, rune(tcell.KeyCtrlUnderscore), tcell.ModShift | tcell.ModCtrl}, wantKey{CtrlSlash, 0, nil}}, |
| 101 |
|
| 102 |
// section 3: (Alt)+Backspace2 |
| 103 |
// KeyBackspace2 is alias for KeyDEL = 0x7F (ASCII) (allegedly unused by Windows) |
| 104 |
// KeyDelete = 0x2E (VK_DELETE constant in Windows) |
| 105 |
// KeyBackspace is alias for KeyBS = 0x08 (ASCII) (implicit alias with KeyCtrlH) |
| 106 |
{giveKey{tcell.KeyBackspace2, 0, tcell.ModNone}, wantKey{Backspace, 0, nil}}, // fabricated |
| 107 |
{giveKey{tcell.KeyBackspace2, 0, tcell.ModAlt}, wantKey{AltBackspace, 0, nil}}, // fabricated |
| 108 |
{giveKey{tcell.KeyDEL, 0, tcell.ModNone}, wantKey{Backspace, 0, nil}}, // fabricated, unhandled |
| 109 |
{giveKey{tcell.KeyDelete, 0, tcell.ModNone}, wantKey{Delete, 0, nil}}, |
| 110 |
{giveKey{tcell.KeyDelete, 0, tcell.ModAlt}, wantKey{AltDelete, 0, nil}}, |
| 111 |
{giveKey{tcell.KeyBackspace, 0, tcell.ModCtrl}, wantKey{CtrlBackspace, 0, nil}}, |
| 112 |
{giveKey{tcell.KeyBackspace, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltBackspace, 0, nil}}, |
| 113 |
{giveKey{tcell.KeyBackspace, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 114 |
{giveKey{tcell.KeyBS, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 115 |
{giveKey{tcell.KeyCtrlH, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 116 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModNone}, wantKey{Backspace, 0, nil}}, // actual "Backspace" keystroke |
| 117 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModAlt}, wantKey{AltBackspace, 0, nil}}, // actual "Alt+Backspace" keystroke |
| 118 |
{giveKey{tcell.KeyDEL, rune(tcell.KeyDEL), tcell.ModCtrl}, wantKey{CtrlBackspace, 0, nil}}, // actual "Ctrl+Backspace" keystroke |
| 119 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModShift}, wantKey{Backspace, 0, nil}}, // actual "Shift+Backspace" keystroke |
| 120 |
{giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltBackspace, 0, nil}}, // actual "Ctrl+Alt+Backspace" keystroke |
| 121 |
{giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlBackspace, 0, nil}}, // actual "Ctrl+Shift+Backspace" keystroke |
| 122 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModShift | tcell.ModAlt}, wantKey{AltBackspace, 0, nil}}, // actual "Shift+Alt+Backspace" keystroke |
| 123 |
{giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModAlt | tcell.ModShift}, wantKey{CtrlAltBackspace, 0, nil}}, // actual "Ctrl+Shift+Alt+Backspace" keystroke |
| 124 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl}, wantKey{CtrlH, 0, nil}}, // actual "Ctrl+H" keystroke |
| 125 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAlt, 'h', nil}}, // fabricated "Ctrl+Alt+H" keystroke |
| 126 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlH, 0, nil}}, // actual "Ctrl+Shift+H" keystroke |
| 127 |
{giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl | tcell.ModAlt | tcell.ModShift}, wantKey{CtrlAlt, 'h', nil}}, // fabricated "Ctrl+Shift+Alt+H" keystroke |
| 128 |
|
| 129 |
// section 4: (Alt+Shift)+Key(Up|Down|Left|Right) |
| 130 |
{giveKey{tcell.KeyUp, 0, tcell.ModNone}, wantKey{Up, 0, nil}}, |
| 131 |
{giveKey{tcell.KeyDown, 0, tcell.ModNone}, wantKey{Down, 0, nil}}, |
| 132 |
{giveKey{tcell.KeyLeft, 0, tcell.ModNone}, wantKey{Left, 0, nil}}, |
| 133 |
{giveKey{tcell.KeyRight, 0, tcell.ModNone}, wantKey{Right, 0, nil}}, |
| 134 |
{giveKey{tcell.KeyUp, 0, tcell.ModNone}, wantKey{Up, 0, nil}}, |
| 135 |
{giveKey{tcell.KeyDown, 0, tcell.ModNone}, wantKey{Down, 0, nil}}, |
| 136 |
{giveKey{tcell.KeyRight, 0, tcell.ModNone}, wantKey{Right, 0, nil}}, |
| 137 |
{giveKey{tcell.KeyLeft, 0, tcell.ModNone}, wantKey{Left, 0, nil}}, |
| 138 |
{giveKey{tcell.KeyUp, 0, tcell.ModCtrl}, wantKey{CtrlUp, 0, nil}}, |
| 139 |
{giveKey{tcell.KeyDown, 0, tcell.ModCtrl}, wantKey{CtrlDown, 0, nil}}, |
| 140 |
{giveKey{tcell.KeyRight, 0, tcell.ModCtrl}, wantKey{CtrlRight, 0, nil}}, |
| 141 |
{giveKey{tcell.KeyLeft, 0, tcell.ModCtrl}, wantKey{CtrlLeft, 0, nil}}, |
| 142 |
{giveKey{tcell.KeyUp, 0, tcell.ModShift}, wantKey{ShiftUp, 0, nil}}, |
| 143 |
{giveKey{tcell.KeyDown, 0, tcell.ModShift}, wantKey{ShiftDown, 0, nil}}, |
| 144 |
{giveKey{tcell.KeyRight, 0, tcell.ModShift}, wantKey{ShiftRight, 0, nil}}, |
| 145 |
{giveKey{tcell.KeyLeft, 0, tcell.ModShift}, wantKey{ShiftLeft, 0, nil}}, |
| 146 |
{giveKey{tcell.KeyUp, 0, tcell.ModAlt}, wantKey{AltUp, 0, nil}}, |
| 147 |
{giveKey{tcell.KeyDown, 0, tcell.ModAlt}, wantKey{AltDown, 0, nil}}, |
| 148 |
{giveKey{tcell.KeyRight, 0, tcell.ModAlt}, wantKey{AltRight, 0, nil}}, |
| 149 |
{giveKey{tcell.KeyLeft, 0, tcell.ModAlt}, wantKey{AltLeft, 0, nil}}, |
| 150 |
{giveKey{tcell.KeyUp, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftUp, 0, nil}}, |
| 151 |
{giveKey{tcell.KeyDown, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftDown, 0, nil}}, |
| 152 |
{giveKey{tcell.KeyRight, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftRight, 0, nil}}, |
| 153 |
{giveKey{tcell.KeyLeft, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftLeft, 0, nil}}, |
| 154 |
{giveKey{tcell.KeyUp, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltUp, 0, nil}}, |
| 155 |
{giveKey{tcell.KeyDown, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltDown, 0, nil}}, |
| 156 |
{giveKey{tcell.KeyRight, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltRight, 0, nil}}, |
| 157 |
{giveKey{tcell.KeyLeft, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltLeft, 0, nil}}, |
| 158 |
{giveKey{tcell.KeyUp, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftUp, 0, nil}}, |
| 159 |
{giveKey{tcell.KeyDown, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftDown, 0, nil}}, |
| 160 |
{giveKey{tcell.KeyRight, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftRight, 0, nil}}, |
| 161 |
{giveKey{tcell.KeyLeft, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftLeft, 0, nil}}, |
| 162 |
{giveKey{tcell.KeyUp, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftUp, 0, nil}}, |
| 163 |
{giveKey{tcell.KeyDown, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftDown, 0, nil}}, |
| 164 |
{giveKey{tcell.KeyRight, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftRight, 0, nil}}, |
| 165 |
{giveKey{tcell.KeyLeft, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftLeft, 0, nil}}, |
| 166 |
{giveKey{tcell.KeyUpLeft, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 167 |
{giveKey{tcell.KeyUpRight, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 168 |
{giveKey{tcell.KeyDownLeft, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 169 |
{giveKey{tcell.KeyDownRight, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 170 |
{giveKey{tcell.KeyCenter, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 171 |
// section 5: (Insert|Home|Delete|End|PgUp|PgDn|BackTab|F1-F12) |
| 172 |
{giveKey{tcell.KeyInsert, 0, tcell.ModNone}, wantKey{Insert, 0, nil}}, |
| 173 |
{giveKey{tcell.KeyF1, 0, tcell.ModNone}, wantKey{F1, 0, nil}}, |
| 174 |
{giveKey{tcell.KeyHome, 0, tcell.ModNone}, wantKey{Home, 0, nil}}, |
| 175 |
{giveKey{tcell.KeyEnd, 0, tcell.ModNone}, wantKey{End, 0, nil}}, |
| 176 |
{giveKey{tcell.KeyDelete, 0, tcell.ModNone}, wantKey{Delete, 0, nil}}, |
| 177 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModNone}, wantKey{PageUp, 0, nil}}, |
| 178 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModNone}, wantKey{PageDown, 0, nil}}, |
| 179 |
{giveKey{tcell.KeyHome, 0, tcell.ModCtrl}, wantKey{CtrlHome, 0, nil}}, |
| 180 |
{giveKey{tcell.KeyEnd, 0, tcell.ModCtrl}, wantKey{CtrlEnd, 0, nil}}, |
| 181 |
{giveKey{tcell.KeyDelete, 0, tcell.ModCtrl}, wantKey{CtrlDelete, 0, nil}}, |
| 182 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModCtrl}, wantKey{CtrlPageUp, 0, nil}}, |
| 183 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModCtrl}, wantKey{CtrlPageDown, 0, nil}}, |
| 184 |
{giveKey{tcell.KeyHome, 0, tcell.ModShift}, wantKey{ShiftHome, 0, nil}}, |
| 185 |
{giveKey{tcell.KeyEnd, 0, tcell.ModShift}, wantKey{ShiftEnd, 0, nil}}, |
| 186 |
{giveKey{tcell.KeyDelete, 0, tcell.ModShift}, wantKey{ShiftDelete, 0, nil}}, |
| 187 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModShift}, wantKey{ShiftPageUp, 0, nil}}, |
| 188 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModShift}, wantKey{ShiftPageDown, 0, nil}}, |
| 189 |
{giveKey{tcell.KeyHome, 0, tcell.ModAlt}, wantKey{AltHome, 0, nil}}, |
| 190 |
{giveKey{tcell.KeyEnd, 0, tcell.ModAlt}, wantKey{AltEnd, 0, nil}}, |
| 191 |
{giveKey{tcell.KeyDelete, 0, tcell.ModAlt}, wantKey{AltDelete, 0, nil}}, |
| 192 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModAlt}, wantKey{AltPageUp, 0, nil}}, |
| 193 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModAlt}, wantKey{AltPageDown, 0, nil}}, |
| 194 |
{giveKey{tcell.KeyHome, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftHome, 0, nil}}, |
| 195 |
{giveKey{tcell.KeyEnd, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftEnd, 0, nil}}, |
| 196 |
{giveKey{tcell.KeyDelete, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftDelete, 0, nil}}, |
| 197 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftPageUp, 0, nil}}, |
| 198 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlShiftPageDown, 0, nil}}, |
| 199 |
{giveKey{tcell.KeyHome, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltHome, 0, nil}}, |
| 200 |
{giveKey{tcell.KeyEnd, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltEnd, 0, nil}}, |
| 201 |
{giveKey{tcell.KeyDelete, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltDelete, 0, nil}}, |
| 202 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltPageUp, 0, nil}}, |
| 203 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAltPageDown, 0, nil}}, |
| 204 |
{giveKey{tcell.KeyHome, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftHome, 0, nil}}, |
| 205 |
{giveKey{tcell.KeyEnd, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftEnd, 0, nil}}, |
| 206 |
{giveKey{tcell.KeyDelete, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftDelete, 0, nil}}, |
| 207 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftPageUp, 0, nil}}, |
| 208 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModShift | tcell.ModAlt}, wantKey{AltShiftPageDown, 0, nil}}, |
| 209 |
{giveKey{tcell.KeyHome, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftHome, 0, nil}}, |
| 210 |
{giveKey{tcell.KeyEnd, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftEnd, 0, nil}}, |
| 211 |
{giveKey{tcell.KeyDelete, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftDelete, 0, nil}}, |
| 212 |
{giveKey{tcell.KeyPgUp, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftPageUp, 0, nil}}, |
| 213 |
{giveKey{tcell.KeyPgDn, 0, tcell.ModCtrl | tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAltShiftPageDown, 0, nil}}, |
| 214 |
// section 6: (Ctrl+Alt)+'rune' |
| 215 |
{giveKey{tcell.KeyRune, 'a', tcell.ModNone}, wantKey{Rune, 'a', nil}}, |
| 216 |
{giveKey{tcell.KeyRune, 'a', tcell.ModCtrl}, wantKey{Rune, 'a', nil}}, // fabricated |
| 217 |
{giveKey{tcell.KeyRune, 'a', tcell.ModAlt}, wantKey{Alt, 'a', nil}}, |
| 218 |
{giveKey{tcell.KeyRune, 'A', tcell.ModAlt}, wantKey{Alt, 'A', nil}}, |
| 219 |
{giveKey{tcell.KeyRune, '`', tcell.ModAlt}, wantKey{Alt, '`', nil}}, |
| 220 |
/* |
| 221 |
"Input method" in Windows Language options: |
| 222 |
US: "US Keyboard" does not generate any characters (and thus any events) in Ctrl+Alt+[a-z] range |
| 223 |
CS: "Czech keyboard" |
| 224 |
DE: "German keyboard" |
| 225 |
|
| 226 |
Note that right Alt is not just `tcell.ModAlt` on foreign language keyboards, but it is the AltGr `tcell.ModCtrl|tcell.ModAlt`. |
| 227 |
*/ |
| 228 |
{giveKey{tcell.KeyRune, '{', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, '{', nil}}, // CS: Ctrl+Alt+b = "{" // Note that this does not interfere with CtrlB, since the "b" is replaced with "{" on OS level |
| 229 |
{giveKey{tcell.KeyRune, '$', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, '$', nil}}, // CS: Ctrl+Alt+ů = "$" |
| 230 |
{giveKey{tcell.KeyRune, '~', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, '~', nil}}, // CS: Ctrl+Alt++ = "~" |
| 231 |
{giveKey{tcell.KeyRune, '`', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, '`', nil}}, // CS: Ctrl+Alt+ý,Space = "`" // this is dead key, space is required to emit the char |
| 232 |
|
| 233 |
{giveKey{tcell.KeyRune, '{', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, '{', nil}}, // DE: Ctrl+Alt+7 = "{" |
| 234 |
{giveKey{tcell.KeyRune, '@', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, '@', nil}}, // DE: Ctrl+Alt+q = "@" |
| 235 |
{giveKey{tcell.KeyRune, 'µ', tcell.ModCtrl | tcell.ModAlt}, wantKey{Rune, 'µ', nil}}, // DE: Ctrl+Alt+m = "µ" |
| 236 |
|
| 237 |
// section 7: Esc |
| 238 |
// KeyEsc and KeyEscape are aliases for KeyESC |
| 239 |
{giveKey{tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone}, wantKey{Esc, 0, nil}}, // fabricated |
| 240 |
{giveKey{tcell.KeyESC, rune(tcell.KeyESC), tcell.ModNone}, wantKey{Esc, 0, nil}}, // unhandled |
| 241 |
{giveKey{tcell.KeyEscape, rune(tcell.KeyEscape), tcell.ModNone}, wantKey{Esc, 0, nil}}, // fabricated, unhandled |
| 242 |
{giveKey{tcell.KeyESC, rune(tcell.KeyESC), tcell.ModCtrl}, wantKey{Esc, 0, nil}}, // actual Ctrl+[ keystroke |
| 243 |
{giveKey{tcell.KeyCtrlLeftSq, rune(tcell.KeyCtrlLeftSq), tcell.ModCtrl}, wantKey{Esc, 0, nil}}, // fabricated, unhandled |
| 244 |
|
| 245 |
// section 8: Invalid |
| 246 |
{giveKey{tcell.KeyRune, 'a', tcell.ModMeta}, wantKey{Rune, 'a', nil}}, // fabricated |
| 247 |
{giveKey{tcell.KeyF24, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, |
| 248 |
{giveKey{tcell.KeyHelp, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 249 |
{giveKey{tcell.KeyExit, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 250 |
{giveKey{tcell.KeyClear, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // unhandled, actual keystroke Numpad_5 with Numlock OFF |
| 251 |
{giveKey{tcell.KeyCancel, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 252 |
{giveKey{tcell.KeyPrint, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled |
| 253 |
{giveKey{tcell.KeyPause, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // unhandled |
| 254 |
|
| 255 |
} |
| 256 |
r := NewFullscreenRenderer(&ColorTheme{}, false, false, 8) |
| 257 |
r.Init() |
| 258 |
|
| 259 |
// run and evaluate the tests |
| 260 |
initialResizeAsInvalid := true |
| 261 |
for _, test := range tests { |
| 262 |
// generate key event |
| 263 |
giveEvent := tcell.NewEventKey(test.giveKey.Type, test.giveKey.Char, test.giveKey.Mods) |
| 264 |
_screen.PostEventWait(giveEvent) |
| 265 |
t.Logf("giveEvent = %T{key: %v, ch: %q (%[3]v), mod: %#04b}\n", giveEvent, giveEvent.Key(), giveEvent.Rune(), giveEvent.Modifiers()) |
| 266 |
|
| 267 |
// process the event in fzf and evaluate the test |
| 268 |
gotEvent := r.GetChar(true) |
| 269 |
// skip Resize events, those are sometimes put in the buffer outside of this test |
| 270 |
if initialResizeAsInvalid && gotEvent.Type == Invalid { |
| 271 |
t.Logf("Resize as Invalid swallowed") |
| 272 |
initialResizeAsInvalid = false |
| 273 |
gotEvent = r.GetChar(true) |
| 274 |
} |
| 275 |
if gotEvent.Type == Resize { |
| 276 |
t.Logf("Resize swallowed") |
| 277 |
gotEvent = r.GetChar(true) |
| 278 |
} |
| 279 |
t.Logf("wantEvent = %T{Type: %v, Char: %q (%[3]v)}\n", test.wantKey, test.wantKey.Type, test.wantKey.Char) |
| 280 |
t.Logf("gotEvent = %T{Type: %v, Char: %q (%[3]v)}\n", gotEvent, gotEvent.Type, gotEvent.Char) |
| 281 |
|
| 282 |
assert(t, "r.GetChar(true).Type", gotEvent.Type, test.wantKey.Type) |
| 283 |
assert(t, "r.GetChar(true).Char", gotEvent.Char, test.wantKey.Char) |
| 284 |
} |
| 285 |
|
| 286 |
r.Close() |
| 287 |
} |
| 288 |
|
| 289 |
/* |
| 290 |
Quick reference |
| 291 |
--------------- |
| 292 |
|
| 293 |
(tabstop=18) |
| 294 |
(this is not mapping table, it merely puts multiple constants ranges in one table) |
| 295 |
|
| 296 |
¹) the two columns are each other implicit alias |
| 297 |
²) explicit aliases here |
| 298 |
|
| 299 |
%v section # tcell ctrl key¹ tcell ctrl char¹ tcell alias² tui constants tcell named keys tcell mods |
| 300 |
-- --------- -------------- --------------- ----------- ------------- ---------------- ---------- |
| 301 |
0 2 KeyCtrlSpace KeyNUL = ^@ Rune ModNone |
| 302 |
1 1 KeyCtrlA KeySOH = ^A CtrlA ModShift |
| 303 |
2 1 KeyCtrlB KeySTX = ^B CtrlB ModCtrl |
| 304 |
3 1 KeyCtrlC KeyETX = ^C CtrlC |
| 305 |
4 1 KeyCtrlD KeyEOT = ^D CtrlD ModAlt |
| 306 |
5 1 KeyCtrlE KeyENQ = ^E CtrlE |
| 307 |
6 1 KeyCtrlF KeyACK = ^F CtrlF |
| 308 |
7 1 KeyCtrlG KeyBEL = ^G CtrlG |
| 309 |
8 1 KeyCtrlH KeyBS = ^H KeyBackspace CtrlH ModMeta |
| 310 |
9 1 KeyCtrlI KeyTAB = ^I KeyTab Tab |
| 311 |
10 1 KeyCtrlJ KeyLF = ^J CtrlJ |
| 312 |
11 1 KeyCtrlK KeyVT = ^K CtrlK |
| 313 |
12 1 KeyCtrlL KeyFF = ^L CtrlL |
| 314 |
13 1 KeyCtrlM KeyCR = ^M KeyEnter Enter |
| 315 |
14 1 KeyCtrlN KeySO = ^N CtrlN |
| 316 |
15 1 KeyCtrlO KeySI = ^O CtrlO |
| 317 |
16 1 KeyCtrlP KeyDLE = ^P CtrlP |
| 318 |
17 1 KeyCtrlQ KeyDC1 = ^Q CtrlQ |
| 319 |
18 1 KeyCtrlR KeyDC2 = ^R CtrlR |
| 320 |
19 1 KeyCtrlS KeyDC3 = ^S CtrlS |
| 321 |
20 1 KeyCtrlT KeyDC4 = ^T CtrlT |
| 322 |
21 1 KeyCtrlU KeyNAK = ^U CtrlU |
| 323 |
22 1 KeyCtrlV KeySYN = ^V CtrlV |
| 324 |
23 1 KeyCtrlW KeyETB = ^W CtrlW |
| 325 |
24 1 KeyCtrlX KeyCAN = ^X CtrlX |
| 326 |
25 1 KeyCtrlY KeyEM = ^Y CtrlY |
| 327 |
26 1 KeyCtrlZ KeySUB = ^Z CtrlZ |
| 328 |
27 7 KeyCtrlLeftSq KeyESC = ^[ KeyEsc, KeyEscape ESC |
| 329 |
28 2 KeyCtrlBackslash KeyFS = ^\ CtrlSpace |
| 330 |
29 2 KeyCtrlRightSq KeyGS = ^] CtrlBackSlash |
| 331 |
30 2 KeyCtrlCarat KeyRS = ^^ CtrlRightBracket |
| 332 |
31 2 KeyCtrlUnderscore KeyUS = ^_ CtrlCaret |
| 333 |
32 CtrlSlash |
| 334 |
33 Invalid |
| 335 |
34 Resize |
| 336 |
35 Mouse |
| 337 |
36 DoubleClick |
| 338 |
37 LeftClick |
| 339 |
38 RightClick |
| 340 |
39 BTab |
| 341 |
40 Backspace |
| 342 |
41 Del |
| 343 |
42 PgUp |
| 344 |
43 PgDn |
| 345 |
44 Up |
| 346 |
45 Down |
| 347 |
46 Left |
| 348 |
47 Right |
| 349 |
48 Home |
| 350 |
49 End |
| 351 |
50 Insert |
| 352 |
51 SUp |
| 353 |
52 SDown |
| 354 |
53 ShiftLeft |
| 355 |
54 SRight |
| 356 |
55 F1 |
| 357 |
56 F2 |
| 358 |
57 F3 |
| 359 |
58 F4 |
| 360 |
59 F5 |
| 361 |
60 F6 |
| 362 |
61 F7 |
| 363 |
62 F8 |
| 364 |
63 F9 |
| 365 |
64 F10 |
| 366 |
65 F11 |
| 367 |
66 F12 |
| 368 |
67 Change |
| 369 |
68 BackwardEOF |
| 370 |
69 AltBackspace |
| 371 |
70 AltUp |
| 372 |
71 AltDown |
| 373 |
72 AltLeft |
| 374 |
73 AltRight |
| 375 |
74 AltSUp |
| 376 |
75 AltSDown |
| 377 |
76 AltShiftLeft |
| 378 |
77 AltShiftRight |
| 379 |
78 Alt |
| 380 |
79 CtrlAlt |
| 381 |
.. |
| 382 |
127 3 KeyDEL KeyBackspace2 |
| 383 |
.. |
| 384 |
256 6 KeyRune |
| 385 |
257 4 KeyUp |
| 386 |
258 4 KeyDown |
| 387 |
259 4 KeyRight |
| 388 |
260 4 KeyLeft |
| 389 |
261 8 KeyUpLeft |
| 390 |
262 8 KeyUpRight |
| 391 |
263 8 KeyDownLeft |
| 392 |
264 8 KeyDownRight |
| 393 |
265 8 KeyCenter |
| 394 |
266 5 KeyPgUp |
| 395 |
267 5 KeyPgDn |
| 396 |
268 5 KeyHome |
| 397 |
269 5 KeyEnd |
| 398 |
270 5 KeyInsert |
| 399 |
271 5 KeyDelete |
| 400 |
272 8 KeyHelp |
| 401 |
273 8 KeyExit |
| 402 |
274 8 KeyClear |
| 403 |
275 8 KeyCancel |
| 404 |
276 8 KeyPrint |
| 405 |
277 8 KeyPause |
| 406 |
278 5 KeyBacktab |
| 407 |
279 5 KeyF1 |
| 408 |
280 5 KeyF2 |
| 409 |
281 5 KeyF3 |
| 410 |
282 5 KeyF4 |
| 411 |
283 5 KeyF5 |
| 412 |
284 5 KeyF6 |
| 413 |
285 5 KeyF7 |
| 414 |
286 5 KeyF8 |
| 415 |
287 5 KeyF9 |
| 416 |
288 5 KeyF10 |
| 417 |
289 5 KeyF11 |
| 418 |
290 5 KeyF12 |
| 419 |
291 8 KeyF13 |
| 420 |
292 8 KeyF14 |
| 421 |
293 8 KeyF15 |
| 422 |
294 8 KeyF16 |
| 423 |
295 8 KeyF17 |
| 424 |
296 8 KeyF18 |
| 425 |
297 8 KeyF19 |
| 426 |
298 8 KeyF20 |
| 427 |
299 8 KeyF21 |
| 428 |
300 8 KeyF22 |
| 429 |
301 8 KeyF23 |
| 430 |
302 8 KeyF24 |
| 431 |
303 8 KeyF25 |
| 432 |
304 8 KeyF26 |
| 433 |
305 8 KeyF27 |
| 434 |
306 8 KeyF28 |
| 435 |
307 8 KeyF29 |
| 436 |
308 8 KeyF30 |
| 437 |
309 8 KeyF31 |
| 438 |
310 8 KeyF32 |
| 439 |
311 8 KeyF33 |
| 440 |
312 8 KeyF34 |
| 441 |
313 8 KeyF35 |
| 442 |
314 8 KeyF36 |
| 443 |
315 8 KeyF37 |
| 444 |
316 8 KeyF38 |
| 445 |
317 8 KeyF39 |
| 446 |
318 8 KeyF40 |
| 447 |
319 8 KeyF41 |
| 448 |
320 8 KeyF42 |
| 449 |
321 8 KeyF43 |
| 450 |
322 8 KeyF44 |
| 451 |
323 8 KeyF45 |
| 452 |
324 8 KeyF46 |
| 453 |
325 8 KeyF47 |
| 454 |
326 8 KeyF48 |
| 455 |
327 8 KeyF49 |
| 456 |
328 8 KeyF50 |
| 457 |
329 8 KeyF51 |
| 458 |
330 8 KeyF52 |
| 459 |
331 8 KeyF53 |
| 460 |
332 8 KeyF54 |
| 461 |
333 8 KeyF55 |
| 462 |
334 8 KeyF56 |
| 463 |
335 8 KeyF57 |
| 464 |
336 8 KeyF58 |
| 465 |
337 8 KeyF59 |
| 466 |
338 8 KeyF60 |
| 467 |
339 8 KeyF61 |
| 468 |
340 8 KeyF62 |
| 469 |
341 8 KeyF63 |
| 470 |
342 8 KeyF64 |
| 471 |
-- --------- -------------- --------------- ----------- ------------- ---------------- ---------- |
| 472 |
%v section # tcell ctrl key tcell ctrl char tcell alias tui constants tcell named keys tcell mods |
| 473 |
*/ |