kakoune/src/color_registry.cc

57 lines
2.0 KiB
C++
Raw Normal View History

#include "color_registry.hh"
#include "exception.hh"
namespace Kakoune
{
2013-05-15 14:01:23 +02:00
static ColorPair parse_color_pair(const String& colordesc)
{
auto it = std::find(colordesc.begin(), colordesc.end(), ',');
2013-05-15 14:01:23 +02:00
return { str_to_color({colordesc.begin(), it}),
it != colordesc.end() ? str_to_color({it+1, colordesc.end()})
: Colors::Default };
}
2013-05-15 14:01:23 +02:00
const ColorPair& ColorRegistry::operator[](const String& colordesc)
{
auto it = m_aliases.find(colordesc);
if (it != m_aliases.end())
return it->second;
return (m_aliases[colordesc] = parse_color_pair(colordesc));
}
void ColorRegistry::register_alias(const String& name, const String& colordesc,
bool override)
{
if (not override and m_aliases.find(name) != m_aliases.end())
throw runtime_error("alias '" + name + "' already defined");
2013-05-15 14:01:23 +02:00
if (name.empty() or
find_if(name, [](char c){ return not isalnum(c); }) != name.end())
throw runtime_error("invalid alias name");
2013-05-15 14:01:23 +02:00
auto it = m_aliases.find(colordesc);
m_aliases[name] = (it != m_aliases.end()) ?
it->second : parse_color_pair(colordesc);
}
ColorRegistry::ColorRegistry()
: m_aliases{
{ "PrimarySelection", { Colors::Cyan, Colors::Blue } },
{ "SecondarySelection", { Colors::Black, Colors::Blue } },
{ "PrimaryCursor", { Colors::Black, Colors::White } },
{ "SecondaryCursor", { Colors::Black, Colors::White } },
2013-05-24 18:39:03 +02:00
{ "LineNumbers", { Colors::Default, Colors::Default } },
{ "MenuForeground", { Colors::Blue, Colors::Cyan } },
{ "MenuBackground", { Colors::Cyan, Colors::Blue } },
{ "Information", { Colors::Black, Colors::Yellow } },
{ "Error", { Colors::Black, Colors::Red } },
{ "StatusLine", { Colors::Cyan, Colors::Default } },
{ "StatusCursor", { Colors::Black, Colors::Cyan } },
{ "Prompt", { Colors::Yellow, Colors::Default} },
}
{}
}