diff --git a/src/main.cc b/src/main.cc index e5aa3b65..312fbbe6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -9,6 +9,7 @@ #include "assert.hh" #include +#include #include using namespace Kakoune; @@ -22,6 +23,51 @@ void set_attribute(int attribute, bool on) attroff(attribute); } +int nc_color(Color color) +{ + switch (color) + { + case Color::Black: return COLOR_BLACK; + case Color::Red: return COLOR_RED; + case Color::Green: return COLOR_GREEN; + case Color::Yellow: return COLOR_YELLOW; + case Color::Blue: return COLOR_BLUE; + case Color::Magenta: return COLOR_MAGENTA; + case Color::Cyan: return COLOR_CYAN; + case Color::White: return COLOR_WHITE; + + case Color::Default: + default: + return COLOR_BLACK; + } +} + +void set_color(Color fg_color, Color bg_color) +{ + static std::map, int> colorpairs; + static int current_pair = -1; + static int next_pair = 0; + + if (current_pair != -1) + attroff(COLOR_PAIR(current_pair)); + + std::pair colorpair(fg_color, bg_color); + auto it = colorpairs.find(colorpair); + if (it != colorpairs.end()) + { + current_pair = it->second; + attron(COLOR_PAIR(it->second)); + } + else + { + init_pair(next_pair, nc_color(fg_color), nc_color(bg_color)); + colorpairs[colorpair] = next_pair; + current_pair = next_pair; + attron(COLOR_PAIR(next_pair)); + ++next_pair; + } +} + void draw_window(Window& window) { int max_x,max_y; @@ -41,6 +87,8 @@ void draw_window(Window& window) set_attribute(A_BLINK, atom.attribute & Blink); set_attribute(A_BOLD, atom.attribute & Bold); + set_color(atom.fg_color, atom.bg_color); + size_t pos = 0; size_t end; while (true) @@ -89,6 +137,7 @@ void init_ncurses() intrflush(stdscr, false); keypad(stdscr, true); curs_set(2); + start_color(); } void deinit_ncurses()