ColorRegistry: permit overriding an alias

This commit is contained in:
Maxime Coste 2012-09-17 21:01:11 +02:00
parent 4d369d6290
commit a92a6dc7f3
3 changed files with 15 additions and 8 deletions

View File

@ -35,9 +35,10 @@ const ColorPair& ColorRegistry::operator[](const String& colordesc)
m_aliases[colordesc] = colpair; m_aliases[colordesc] = colpair;
} }
void ColorRegistry::register_alias(const String& name, const String& colordesc) void ColorRegistry::register_alias(const String& name, const String& colordesc,
bool override)
{ {
if (m_aliases.find(name) != m_aliases.end()) if (not override and m_aliases.find(name) != m_aliases.end())
throw runtime_error("alias '" + name + "' already defined"); throw runtime_error("alias '" + name + "' already defined");
if (std::find_if(name.begin(), name.end(), if (std::find_if(name.begin(), name.end(),

View File

@ -15,7 +15,8 @@ class ColorRegistry : public Singleton<ColorRegistry>
{ {
public: public:
const ColorPair& operator[](const String& colordesc); const ColorPair& operator[](const String& colordesc);
void register_alias(const String& name, const String& colordesc); void register_alias(const String& name, const String& colordesc,
bool override = false);
private: private:
std::unordered_map<String, ColorPair> m_aliases; std::unordered_map<String, ColorPair> m_aliases;

View File

@ -750,6 +750,15 @@ void try_catch(const CommandParameters& params, Context& context)
} }
} }
void define_color_alias(const CommandParameters& params, Context& context)
{
ParametersParser parser(params, { { "allow-override", false } });
if (parser.positional_count() != 2)
throw wrong_argument_count();
ColorRegistry::instance().register_alias(
parser[0], parser[1], parser.has_option("allow-override"));
}
} }
void register_commands() void register_commands()
@ -863,11 +872,7 @@ void register_commands()
{ return context.window().option_manager().complete_option_name(prefix, cursor_pos); } { return context.window().option_manager().complete_option_name(prefix, cursor_pos); }
})); }));
cm.register_commands({"ca", "colalias"}, cm.register_commands({"ca", "colalias"}, define_color_alias);
[](const CommandParameters& params, Context&) {
if (params.size() != 2) throw wrong_argument_count();
ColorRegistry::instance().register_alias(params[0], params[1]);
});
} }
} }