Ttibsi / rawterm

C++20 library for building TUI applications
MIT License
7 stars 3 forks source link

Simplify checking code and modifiers #22

Closed Ttibsi closed 1 year ago

Ttibsi commented 1 year ago

Currently I'm doing this to prevent segfaults if the mods list has any items in it:

if (k.code == 'q' && !(k.mod.empty()) && k.mod[0] == rawterm::Mod::Control) {}

Potential fix: Just make k.mod of type rawterm::Mod instead of a vector, as we never have multiple elements. Only time I can tell is if we do shift + alt + letter, which currently isn't parsed anyway, but would be great if it was in the future) or potentially if we implement alt_gr in the future.

Ttibsi commented 1 year ago

An idea:

if (isMod(k) == rawterm::Mod::Control && k.code == 'q') {}

We can also add rawterm::Mod::None

isMod() could look something vaguely like this and for multiple modifiers, loop through the results of isMod() until it's equal to [None](rawterm::Mod::None) (I'm not sure how to implement this, but another approach there is almost to treat None in the same way std::string using ::npos)

rawterm::Mod isMod(Key k) { return (!(k.mod.empty()) ? k.mod.pop_back() : rawterm::Mod::None }