extract parse_color to color.cc as str_to_color, and add color_to_str

This commit is contained in:
Maxime Coste 2013-03-25 23:35:59 +01:00
parent 9429b662ca
commit d02133d382
3 changed files with 52 additions and 19 deletions

41
src/color.cc Normal file
View File

@ -0,0 +1,41 @@
#include "color.hh"
#include "exception.hh"
namespace Kakoune
{
Color str_to_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;
}
String color_to_str(const Color& color)
{
switch (color)
{
case Color::Default: return "default";
case Color::Black: return "black";
case Color::Red: return "red";
case Color::Green: return "green";
case Color::Yellow: return "yellow";
case Color::Blue: return "blue";
case Color::Magenta: return "magenta";
case Color::Cyan: return "cyan";
case Color::White: return "white";
}
assert(false);
return "default";
}
}

View File

@ -1,9 +1,13 @@
#ifndef color_hh_INCLUDED
#define color_hh_INCLUDED
#include <utility>
namespace Kakoune
{
class String;
enum class Color : char
{
Default,
@ -19,6 +23,9 @@ enum class Color : char
using ColorPair = std::pair<Color, Color>;
Color str_to_color(const String& color);
String color_to_str(const Color& color);
}
#endif // color_hh_INCLUDED

View File

@ -5,21 +5,6 @@
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);
@ -27,9 +12,9 @@ const ColorPair& ColorRegistry::operator[](const String& colordesc)
return alias_it->second;
auto it = std::find(colordesc.begin(), colordesc.end(), ',');
ColorPair colpair{ parse_color(String(colordesc.begin(), it)),
ColorPair colpair{ str_to_color(String(colordesc.begin(), it)),
it != colordesc.end() ?
parse_color(String(it+1, colordesc.end()))
str_to_color(String(it+1, colordesc.end()))
: Color::Default };
return (m_aliases[colordesc] = colpair);
@ -46,10 +31,10 @@ void ColorRegistry::register_alias(const String& name, const String& colordesc,
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 fg = str_to_color(String(colordesc.begin(), it));
auto bg = Color::Default;
if (it != colordesc.end())
bg = parse_color(String(it+1, colordesc.end()));
bg = str_to_color(String(it+1, colordesc.end()));
m_aliases[name] = { fg, bg };
}