Use a ColorPair in DisplayAtoms instead of separate fg/bg colors

This commit is contained in:
Maxime Coste 2013-03-06 20:25:23 +01:00
parent 594ee65949
commit f677d4ba32
7 changed files with 31 additions and 42 deletions

View File

@ -17,6 +17,8 @@ enum class Color : char
White
};
using ColorPair = std::pair<Color, Color>;
}
#endif // color_hh_INCLUDED

View File

@ -9,8 +9,6 @@
namespace Kakoune
{
using ColorPair = std::pair<Color, Color>;
class ColorRegistry : public Singleton<ColorRegistry>
{
public:

View File

@ -30,8 +30,7 @@ void DisplayLine::optimize()
auto& atom = *atom_it;
auto& next_atom = *next_atom_it;
if (atom.fg_color == next_atom.fg_color and
atom.bg_color == next_atom.bg_color and
if (atom.colors == next_atom.colors and
atom.attribute == next_atom.attribute and
atom.content.type() == AtomContent::BufferRange and
next_atom.content.type() == AtomContent::BufferRange and

View File

@ -110,15 +110,14 @@ private:
struct DisplayAtom
{
Color fg_color;
Color bg_color;
ColorPair colors;
Attribute attribute;
AtomContent content;
DisplayAtom(AtomContent content)
: content(std::move(content)), attribute(Normal),
fg_color(Color::Default), bg_color(Color::Default) {}
: content{std::move(content)}, attribute{Normal},
colors{Color::Default, Color::Default} {}
};
class DisplayLine

View File

@ -81,10 +81,7 @@ public:
continue;
highlight_range(display_buffer, match[n].first, match[n].second, true,
[&](DisplayAtom& atom) {
atom.fg_color = col_it->second->first;
atom.bg_color = col_it->second->second;
});
[&](DisplayAtom& atom) { atom.colors = *col_it->second; });
}
}
}
@ -257,8 +254,7 @@ void show_line_numbers(DisplayBuffer& display_buffer)
char buffer[10];
snprintf(buffer, 10, format, (int)line.buffer_line() + 1);
DisplayAtom atom = DisplayAtom(AtomContent(buffer));
atom.fg_color = Color::Black;
atom.bg_color = Color::White;
atom.colors = { Color::Black, Color::White };
line.insert(line.begin(), std::move(atom));
}
}
@ -272,12 +268,12 @@ void highlight_selections(const SelectionList& selections, DisplayBuffer& displa
BufferIterator begin = forward ? sel.first() : utf8::next(sel.last());
BufferIterator end = forward ? sel.last() : utf8::next(sel.first());
Color fg_color = (i == selections.size() - 1) ? Color::Cyan : Color::Black;
Color bg_color = (i == selections.size() - 1) ? Color::Blue : Color::Blue;
ColorPair colors = (i == selections.size() - 1) ? ColorPair{ Color::Cyan, Color::Blue }
: ColorPair{ Color::Black, Color::Blue };
highlight_range(display_buffer, begin, end, false,
[&](DisplayAtom& atom) { atom.fg_color = fg_color; atom.bg_color = bg_color; });
[&](DisplayAtom& atom) { atom.colors = colors; });
highlight_range(display_buffer, sel.last(), utf8::next(sel.last()), false,
[](DisplayAtom& atom) { atom.fg_color = Color::Black; atom.bg_color = Color::White; });
[](DisplayAtom& atom) { atom.colors = { Color::Black, Color::White}; });
}
}
@ -301,8 +297,7 @@ void expand_unprintable(DisplayBuffer& display_buffer)
highlight_range(display_buffer,
it.underlying_iterator(), (it+1).underlying_iterator(),
true, [&str](DisplayAtom& atom){ atom.content.replace(str);
atom.bg_color = Color::Red;
atom.fg_color = Color::Black; });
atom.colors = { Color::Red, Color::Black }; });
}
}
}
@ -329,8 +324,7 @@ public:
{
const bool flagged = contains(lines, (int)line.buffer_line() + 1);
DisplayAtom atom{AtomContent(flagged ? m_flag : empty)};
atom.fg_color = Color::Blue;
atom.bg_color = Color::Cyan;
atom.colors = { Color::Blue, Color::Cyan };
line.insert(line.begin(), std::move(atom));
}
}

View File

@ -43,34 +43,32 @@ static int nc_color(Color color)
}
}
static int get_color_pair(Color fg_color, Color bg_color)
static int get_color_pair(const ColorPair& colors)
{
static std::map<std::pair<Color, Color>, int> colorpairs;
static std::map<ColorPair, int> colorpairs;
static int next_pair = 1;
std::pair<Color, Color> colorpair(fg_color, bg_color);
auto it = colorpairs.find(colorpair);
auto it = colorpairs.find(colors);
if (it != colorpairs.end())
return it->second;
else
{
init_pair(next_pair, nc_color(fg_color), nc_color(bg_color));
colorpairs[colorpair] = next_pair;
init_pair(next_pair, nc_color(colors.first), nc_color(colors.second));
colorpairs[colors] = next_pair;
return next_pair++;
}
}
static void set_color(Color fg_color, Color bg_color)
static void set_color(const ColorPair colors)
{
static int current_pair = -1;
if (current_pair != -1)
attroff(COLOR_PAIR(current_pair));
if (fg_color != Color::Default or bg_color != Color::Default)
if (colors.first != Color::Default or colors.second != Color::Default)
{
current_pair = get_color_pair(fg_color, bg_color);
current_pair = get_color_pair(colors);
attron(COLOR_PAIR(current_pair));
}
}
@ -101,8 +99,8 @@ NCursesUI::NCursesUI()
use_default_colors();
ESCDELAY=25;
m_menu_fg = get_color_pair(Color::Blue, Color::Cyan);
m_menu_bg = get_color_pair(Color::Cyan, Color::Blue);
m_menu_fg = get_color_pair({ Color::Blue, Color::Cyan });
m_menu_bg = get_color_pair({ Color::Cyan, Color::Blue });
signal(SIGWINCH, on_term_resize);
signal(SIGINT, on_sigint);
@ -175,7 +173,7 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer,
set_attribute(A_BLINK, atom.attribute & Blink);
set_attribute(A_BOLD, atom.attribute & Bold);
set_color(atom.fg_color, atom.bg_color);
set_color(atom.colors);
String content = atom.content.content();
if (content[content.length()-1] == '\n' and
@ -200,7 +198,7 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer,
set_attribute(A_REVERSE, 0);
set_attribute(A_BLINK, 0);
set_attribute(A_BOLD, 0);
set_color(Color::Blue, Color::Default);
set_color({ Color::Blue, Color::Default });
for (;line_index < m_dimensions.line; ++line_index)
{
move((int)line_index, 0);
@ -208,7 +206,7 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer,
addch('~');
}
set_color(Color::Cyan, Color::Default);
set_color({ Color::Cyan, Color::Default });
draw_status();
CharCount status_len = mode_line.char_length();
// only draw mode_line if it does not overlap one status line
@ -461,7 +459,7 @@ void NCursesUI::info_show(const String& content, const DisplayCoord& anchor, Men
m_info_win = newwin((int)size.line, (int)size.column,
(int)pos.line, (int)pos.column);
wbkgd(m_info_win, COLOR_PAIR(get_color_pair(Color::Black, Color::Yellow)));
wbkgd(m_info_win, COLOR_PAIR(get_color_pair({ Color::Black, Color::Yellow })));
wmove(m_info_win, 0, 0);
addutf8str(m_info_win, Utf8Iterator(content.begin()),
Utf8Iterator(content.end()));

View File

@ -70,8 +70,8 @@ public:
void write(const DisplayAtom& atom)
{
write(atom.fg_color);
write(atom.bg_color);
write(atom.colors.first);
write(atom.colors.second);
write(atom.attribute);
write(atom.content.content());
}
@ -144,8 +144,7 @@ DisplayAtom read<DisplayAtom>(int socket)
Color bg_color = read<Color>(socket);
Attribute attribute = read<Attribute>(socket);
DisplayAtom atom(AtomContent(read<String>(socket)));
atom.fg_color = fg_color;
atom.bg_color = bg_color;
atom.colors = { fg_color, bg_color };
atom.attribute = attribute;
return atom;
}