Extract structure of key interpretation from the side effect of reading keystrokes#29
Conversation
|
|
||
| import terminus.effect.Ascii | ||
|
|
||
| class KeySequence(val root: Key, val sequences: Map[String, Key]): |
There was a problem hiding this comment.
Happy to make this less generic. My thinking here is maybe down the line, it might be nice to support telnet control sequences (They start with '\x00ff'), or maybe someone might want to build custom control sequences into an application.
| case '=' => Key('≠') | ||
| case other: Char => Key.unknown(s"${Ascii.ESC}${other}") | ||
| @tailrec | ||
| private def readKeySequence(acc: String, sequence: KeySequence): Eof | Key = { |
There was a problem hiding this comment.
The problem this still has that the previous implementation has is that if it picks up a second user keystroke in < 100ms, it could get swallowed up.
noelwelsh
left a comment
There was a problem hiding this comment.
I'm a bit concerned about the impact of this change on performance. Reading keys is likely to be in the inner loop of many applications, and I think this change adds of lot of overhead.
I think the intention here is to use something very close to a trie, aka prefix tree, and I think the implementation would be clearer if that was made explicit.
Create an inital structure for key code interpretation.
Split keycode structure into base keys and escapeSequences. Create escapeSubsequnces to support key parsing.
I chose this structure over a trie for a couple of reasons.
As for performance:
|
|
I don't have time to look at this at the moment, but just wanted to let you know I appreciate the work. I should have some time for an in-depth look later next week. |
|
Ok, I can't really argument with the performance improvement! Also interesting that it didn't match my mental performance at all. This seems like a win all around then. |
Refactors
readKey()to interpret a structure instead of having the key mappings embedded in the function. This makes the structure re-usable for alternate interpreters.I'm open to re-naming and re-ordering anything. Full disclosure, re-structuring everything looked like a bit of a slog, so I used Claude Code to extract all of the escape sequences.
Resolves: #28