From 6ffdfd77353748f855b1528cdc6ca44b7154bfe3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 4 Apr 2013 18:47:34 +0200 Subject: [PATCH] Add get_color helper function --- src/color_registry.hh | 5 +++++ src/commands.cc | 3 +-- src/highlighters.cc | 12 ++++++------ src/input_handler.cc | 15 ++++++--------- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/color_registry.hh b/src/color_registry.hh index a33c9162..ed130e03 100644 --- a/src/color_registry.hh +++ b/src/color_registry.hh @@ -22,6 +22,11 @@ private: std::unordered_map m_aliases; }; +inline const ColorPair& get_color(const String& colordesc) +{ + return ColorRegistry::instance()[colordesc]; +} + } #endif // color_registry_hh_INCLUDED diff --git a/src/commands.cc b/src/commands.cc index af771891..819cebb7 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -684,8 +684,7 @@ void info(const CommandParameters& params, Context& context) pos = context.window().display_position(it); } const String& message = parser.has_option("assist") ? assist(parser[0], dimensions.column) : parser[0]; - ColorPair colors = ColorRegistry::instance()["Information"]; - context.ui().info_show(message, pos, colors, style); + context.ui().info_show(message, pos, get_color("Information"), style); } } diff --git a/src/highlighters.cc b/src/highlighters.cc index b8d1c348..ae0591f0 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -135,7 +135,7 @@ HighlighterAndId colorize_regex_factory(const HighlighterParameters params, cons int capture = str_to_int(String(res[1].first, res[1].second)); const ColorPair*& color = colors[capture]; - color = &ColorRegistry::instance()[String(res[2].first, res[2].second)]; + color = &get_color(String(res[2].first, res[2].second)); } String id = "colre'" + params[0] + "'"; @@ -185,7 +185,7 @@ HighlighterAndId highlight_search_factory(const HighlighterParameters params, co throw runtime_error("wrong parameter count"); try { - ColorSpec colors { { 0, &ColorRegistry::instance()[params[0]] } }; + ColorSpec colors { { 0, &get_color(params[0]) } }; auto get_regex = []{ auto s = RegisterManager::instance()['/'].values(Context{}); return s.empty() ? Regex{} : Regex{s[0].begin(), s[0].end()}; @@ -203,7 +203,7 @@ HighlighterAndId highlight_regex_option_factory(const HighlighterParameters para if (params.size() != 2) throw runtime_error("wrong parameter count"); - ColorSpec colors { { 0, &ColorRegistry::instance()[params[1]] } }; + ColorSpec colors { { 0, &get_color(params[1]) } }; String option_name = params[0]; const OptionManager& options = window.options(); // verify option type now @@ -266,7 +266,7 @@ void show_line_numbers(DisplayBuffer& display_buffer) char format[] = "%?d "; format[1] = '0' + digit_count; - auto& colors = ColorRegistry::instance()["LineNumbers"]; + auto& colors = get_color("LineNumbers"); for (auto& line : display_buffer.lines()) { char buffer[10]; @@ -290,11 +290,11 @@ void highlight_selections(const Window& window, DisplayBuffer& display_buffer) const bool primary = (i == window.main_selection_index()); if (not only_cursor) { - ColorPair sel_colors = ColorRegistry::instance()[primary ? "PrimarySelection" : "SecondarySelection"]; + ColorPair sel_colors = get_color(primary ? "PrimarySelection" : "SecondarySelection"); highlight_range(display_buffer, begin, end, false, [&](DisplayAtom& atom) { atom.colors = sel_colors; }); } - ColorPair cur_colors = ColorRegistry::instance()[primary ? "PrimaryCursor" : "SecondaryCursor"]; + ColorPair cur_colors = get_color(primary ? "PrimaryCursor" : "SecondaryCursor"); highlight_range(display_buffer, sel.last(), utf8::next(sel.last()), false, [&](DisplayAtom& atom) { atom.colors = cur_colors; }); } diff --git a/src/input_handler.cc b/src/input_handler.cc index ef2afd2a..f8a2fc64 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -154,9 +154,8 @@ public: m_selected(m_choices.begin()) { DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char }; - ColorRegistry& colreg = ColorRegistry::instance(); - context().ui().menu_show(choices, menu_pos, colreg["MenuForeground"], - colreg["MenuBackground"], MenuStyle::Prompt); + context().ui().menu_show(choices, menu_pos, get_color("MenuForeground"), + get_color("MenuBackground"), MenuStyle::Prompt); } void on_key(const Key& key) override @@ -378,9 +377,8 @@ public: context().ui().menu_hide(); DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char }; - ColorRegistry& colreg = ColorRegistry::instance(); - context().ui().menu_show(candidates, menu_pos, colreg["MenuForeground"], - colreg["MenuBackground"], MenuStyle::Prompt); + context().ui().menu_show(candidates, menu_pos, get_color("MenuForeground"), + get_color("MenuBackground"), MenuStyle::Prompt); bool use_common_prefix = context().options()["complete_prefix"].get(); String prefix = use_common_prefix ? common_prefix(candidates) : String(); @@ -608,10 +606,9 @@ private: { DisplayCoord menu_pos = m_context.window().display_position(m_completions.begin); - ColorRegistry& colreg = ColorRegistry::instance(); m_context.ui().menu_show(m_matching_candidates, menu_pos, - colreg["MenuForeground"], - colreg["MenuBackground"], + get_color("MenuForeground"), + get_color("MenuBackground"), MenuStyle::Inline); m_context.ui().menu_select(m_current_candidate); }