From f7c1702965795e7166801847d253050f81e32c26 Mon Sep 17 00:00:00 2001 From: Tim Allen Date: Fri, 26 Mar 2021 15:17:41 +1100 Subject: [PATCH] src/ncurses_ui: move the parse_mask() helper outside parse_csi(). It's useful for parsing modifier masks in all kinds of sequences, not just CSI sequences. Also, since the modifier mask always has "1" as "no modifiers", do the subtraction inside parse_mask() instead of when calling it. --- src/ncurses_ui.cc | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index 17006c134..9c5262c62 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -657,7 +657,19 @@ Optional NCursesUI::get_next_key() return Key{utf8::codepoint(CharIterator{c}, Sentinel{})}; }; - auto parse_csi = [this, &convert]() -> Optional { + auto parse_mask = [](int mask) { + mask = std::max(mask - 1, 0); + Key::Modifiers mod = Key::Modifiers::None; + if (mask & 1) + mod |= Key::Modifiers::Shift; + if (mask & 2) + mod |= Key::Modifiers::Alt; + if (mask & 4) + mod |= Key::Modifiers::Control; + return mod; + }; + + auto parse_csi = [this, &convert, &parse_mask]() -> Optional { auto next_char = [] { return get_char().value_or((unsigned char)0xff); }; int params[16] = {}; auto c = next_char(); @@ -679,17 +691,6 @@ Optional NCursesUI::get_next_key() if (c != '$' and (c < 0x40 or c > 0x7e)) return {}; - auto parse_mask = [](int mask) { - Key::Modifiers mod = Key::Modifiers::None; - if (mask & 1) - mod |= Key::Modifiers::Shift; - if (mask & 2) - mod |= Key::Modifiers::Alt; - if (mask & 4) - mod |= Key::Modifiers::Control; - return mod; - }; - auto mouse_button = [this](Key::Modifiers mod, Key::MouseButton button, Codepoint coord, bool release) { auto mask = 1 << (int)button; if (not release) @@ -710,7 +711,7 @@ Optional NCursesUI::get_next_key() (Codepoint)((down ? 1 : -1) * m_wheel_scroll_amount)}; }; - auto masked_key = [&](Codepoint key) { return Key{parse_mask(std::max(params[1] - 1, 0)), key}; }; + auto masked_key = [&](Codepoint key) { return Key{parse_mask(params[1]), key}; }; switch (c) {