From b116b899771630d7c02c5d0ddb9f84c04da1bd12 Mon Sep 17 00:00:00 2001 From: trilader Date: Sun, 22 Nov 2020 21:38:14 +0100 Subject: [PATCH] Initialize TUI lookup tables in tp_init instead of using static init --- tui.cpp | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/tui.cpp b/tui.cpp index ee25fe1..f36f887 100644 --- a/tui.cpp +++ b/tui.cpp @@ -12,6 +12,15 @@ termpaint_terminal *terminal; termpaint_surface *surface; AttributeSet attributes[ASetTypeCount]; +std::unordered_map key_symbols; + +struct button_info { + Button button; + std::string string; + size_t width; +}; + +std::vector all_buttons; std::deque eventqueue; @@ -52,6 +61,20 @@ std::optional wait_for_event(termpaint_integration *integration, int time void tp_init() { + all_buttons = { + {Button::Ok, "Ok", 2}, + {Button::Cancel, "Cancel", 6}, + {Button::Yes, "Yes", 3}, + {Button::No, "No", 2}, + }; + key_symbols = { + {"ArrowLeft", "←"}, + {"ArrowUp", "↑"}, + {"ArrowRight", "→"}, + {"ArrowDown", "↓"}, + {"Escape", "Esc"}, + }; + auto new_attr_set = [](AttributeSet &set, int color, int style = 0) { set.normal = termpaint_attr_new(color, TERMPAINT_DEFAULT_COLOR); termpaint_attr_set_style(set.normal, style); @@ -335,20 +358,8 @@ std::vector split(const std::string &str, const char delim, const u return parts; } -struct button_info { - Button button; - std::string string; - size_t width; -}; - -static std::vector all_buttons = { - {Button::Ok, "Ok", 2}, - {Button::Cancel, "Cancel", 6}, - {Button::Yes, "Yes", 3}, - {Button::No, "No", 2}, -}; -static const char *button_gfx_left[]={"[", " "}; -const char *button_gfx_right[]={"]", " "}; +static const char *button_gfx_left[] = {"[", " "}; +static const char *button_gfx_right[] = {"]", " "}; Button message_box(const std::string &caption, const std::string &text, const Button buttons, const Button default_button, const Align align) { @@ -616,14 +627,6 @@ static void draw_help(const std::vector &items) message_box("Help", to_show.c_str()); } -static std::unordered_map key_symbols = { - {"ArrowLeft", "←"}, - {"ArrowUp", "↑"}, - {"ArrowRight", "→"}, - {"ArrowDown", "↓"}, - {"Escape", "Esc"}, -}; - static std::string format_key(const action &action) { std::string str; if(action.modifier & TERMPAINT_MOD_CTRL)