Add a ColorRegistry class responsible of color parsing and supporting aliases

colalias command permits to define names for color pairs
This commit is contained in:
Maxime Coste 2012-09-17 19:01:13 +02:00
parent 45bffd8b8a
commit 5cf947f845
7 changed files with 120 additions and 33 deletions

23
src/color.hh Normal file
View File

@ -0,0 +1,23 @@
#ifndef color_hh_INCLUDED
#define color_hh_INCLUDED
namespace Kakoune
{
enum class Color
{
Default,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White
};
}
#endif // color_hh_INCLUDED

56
src/color_registry.cc Normal file
View File

@ -0,0 +1,56 @@
#include "color_registry.hh"
#include "exception.hh"
namespace Kakoune
{
static Color parse_color(const String& color)
{
if (color == "default") return Color::Default;
if (color == "black") return Color::Black;
if (color == "red") return Color::Red;
if (color == "green") return Color::Green;
if (color == "yellow") return Color::Yellow;
if (color == "blue") return Color::Blue;
if (color == "magenta") return Color::Magenta;
if (color == "cyan") return Color::Cyan;
if (color == "white") return Color::White;
throw runtime_error("Unable to parse color '" + color + "'");
return Color::Default;
}
const ColorPair& ColorRegistry::operator[](const String& colordesc)
{
auto alias_it = m_aliases.find(colordesc);
if (alias_it != m_aliases.end())
return alias_it->second;
auto it = std::find(colordesc.begin(), colordesc.end(), ',');
ColorPair colpair{ parse_color(String(colordesc.begin(), it)),
it != colordesc.end() ?
parse_color(String(it+1, colordesc.end()))
: Color::Default };
m_aliases[colordesc] = colpair;
}
void ColorRegistry::register_alias(const String& name, const String& colordesc)
{
if (m_aliases.find(name) != m_aliases.end())
throw runtime_error("alias '" + name + "' already defined");
if (std::find_if(name.begin(), name.end(),
[](Character c) { return not isalnum(c); }) != name.end())
throw runtime_error("alias names are limited to alpha numeric words");
auto it = std::find(colordesc.begin(), colordesc.end(), ',');
auto fg = parse_color(String(colordesc.begin(), it));
auto bg = Color::Default;
if (it != colordesc.end())
bg = parse_color(String(it+1, colordesc.end()));
m_aliases[name] = { fg, bg };
}
}

27
src/color_registry.hh Normal file
View File

@ -0,0 +1,27 @@
#ifndef color_registry_hh_INCLUDED
#define color_registry_hh_INCLUDED
#include <unordered_map>
#include "color.hh"
#include "utils.hh"
namespace Kakoune
{
using ColorPair = std::pair<Color, Color>;
class ColorRegistry : public Singleton<ColorRegistry>
{
public:
const ColorPair& operator[](const String& colordesc);
void register_alias(const String& name, const String& colordesc);
private:
std::unordered_map<String, ColorPair> m_aliases;
};
}
#endif // color_registry_hh_INCLUDED

View File

@ -15,6 +15,7 @@
#include "completion.hh"
#include "shell_manager.hh"
#include "event_manager.hh"
#include "color_registry.hh"
#if defined(__APPLE__)
#include <mach-o/dyld.h>
@ -861,6 +862,12 @@ void register_commands()
[](const Context& context, const String& prefix, CharCount cursor_pos)
{ return context.window().option_manager().complete_option_name(prefix, cursor_pos); }
}));
cm.register_commands({"ca", "colalias"},
[](const CommandParameters& params, Context&) {
if (params.size() != 2) throw wrong_argument_count();
ColorRegistry::instance().register_alias(params[0], params[1]);
});
}
}

View File

@ -4,6 +4,7 @@
#include <vector>
#include "string.hh"
#include "color.hh"
#include "line_and_column.hh"
#include "buffer.hh"
@ -31,19 +32,6 @@ enum Attributes
Bold = 8
};
enum class Color
{
Default,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White
};
struct AtomContent
{
public:

View File

@ -2,6 +2,7 @@
#include "assert.hh"
#include "window.hh"
#include "highlighter_registry.hh"
#include "color_registry.hh"
#include "highlighter_group.hh"
#include "string.hh"
@ -113,20 +114,6 @@ private:
}
};
Color parse_color(const String& color)
{
if (color == "default") return Color::Default;
if (color == "black") return Color::Black;
if (color == "red") return Color::Red;
if (color == "green") return Color::Green;
if (color == "yellow") return Color::Yellow;
if (color == "blue") return Color::Blue;
if (color == "magenta") return Color::Magenta;
if (color == "cyan") return Color::Cyan;
if (color == "white") return Color::White;
return Color::Default;
}
HighlighterAndId colorize_regex_factory(Window& window,
const HighlighterParameters params)
{
@ -135,7 +122,7 @@ HighlighterAndId colorize_regex_factory(Window& window,
try
{
static Regex color_spec_ex(LR"((\d+):(\w+)(,(\w+))?)");
static Regex color_spec_ex(LR"((\d+):(\w+(,\w+)?))");
ColorSpec colors;
for (auto it = params.begin() + 1; it != params.end(); ++it)
{
@ -145,11 +132,8 @@ HighlighterAndId colorize_regex_factory(Window& window,
"' expected <capture>:<fgcolor>[,<bgcolor>]");
int capture = str_to_int(String(res[1].first, res[1].second));
Color fg_color = parse_color(String(res[2].first, res[2].second));
Color bg_color = res[4].matched ?
parse_color(String(res[4].first, res[4].second))
: Color::Default;
colors[capture] = { fg_color, bg_color };
ColorPair& color = colors[capture];
color = ColorRegistry::instance()[String(res[2].first, res[2].second)];
}
String id = "colre'" + params[0] + "'";

View File

@ -18,6 +18,7 @@
#include "context.hh"
#include "ncurses.hh"
#include "string.hh"
#include "color_registry.hh"
#include <unordered_map>
#include <sys/types.h>
@ -445,6 +446,7 @@ int main(int argc, char* argv[])
RegisterManager register_manager;
HighlighterRegistry highlighter_registry;
FilterRegistry filter_registry;
ColorRegistry color_registry;
run_unit_tests();