summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2011-09-30 19:15:14 +0000
committerMaxime Coste <frrrwww@gmail.com>2011-09-30 19:15:14 +0000
commit36c3bb6ae34eda6ec316f109b903ffb3fc007e29 (patch)
tree4cafb621cce1e8366eed6e81be25271f4e02ccde /src
parente351acd997c0b2bf3a8026f16f6e70baa4388080 (diff)
ncurses: quick'n'dirty color support
Diffstat (limited to 'src')
-rw-r--r--src/main.cc49
1 files changed, 49 insertions, 0 deletions
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 <unordered_map>
+#include <map>
#include <ncurses.h>
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<std::pair<Color, Color>, int> colorpairs;
+ static int current_pair = -1;
+ static int next_pair = 0;
+
+ if (current_pair != -1)
+ attroff(COLOR_PAIR(current_pair));
+
+ std::pair<Color, Color> 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()