ncurses: colorize the menu

This commit is contained in:
Maxime Coste 2012-09-05 00:49:59 +02:00
parent 3f64c36654
commit 5c4df507c4
2 changed files with 54 additions and 42 deletions

View File

@ -10,27 +10,6 @@
namespace Kakoune namespace Kakoune
{ {
NCursesClient::NCursesClient()
: m_menu(nullptr)
{
// setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
curs_set(0);
start_color();
use_default_colors();
ESCDELAY=25;
}
NCursesClient::~NCursesClient()
{
endwin();
}
static void set_attribute(int attribute, bool on) static void set_attribute(int attribute, bool on)
{ {
if (on) if (on)
@ -58,35 +37,63 @@ static int nc_color(Color color)
} }
} }
static void set_color(Color fg_color, Color bg_color) static int get_color_pair(Color fg_color, Color bg_color)
{ {
static std::map<std::pair<Color, Color>, int> colorpairs; static std::map<std::pair<Color, Color>, int> colorpairs;
static int current_pair = -1;
static int next_pair = 1; static int next_pair = 1;
if (current_pair != -1)
attroff(COLOR_PAIR(current_pair));
if (fg_color == Color::Default and bg_color == Color::Default)
return;
std::pair<Color, Color> colorpair(fg_color, bg_color); std::pair<Color, Color> colorpair(fg_color, bg_color);
auto it = colorpairs.find(colorpair); auto it = colorpairs.find(colorpair);
if (it != colorpairs.end()) if (it != colorpairs.end())
{ return it->second;
current_pair = it->second;
attron(COLOR_PAIR(it->second));
}
else else
{ {
init_pair(next_pair, nc_color(fg_color), nc_color(bg_color)); init_pair(next_pair, nc_color(fg_color), nc_color(bg_color));
colorpairs[colorpair] = next_pair; colorpairs[colorpair] = next_pair;
current_pair = next_pair; return next_pair++;
attron(COLOR_PAIR(next_pair));
++next_pair;
} }
} }
static void set_color(Color fg_color, Color bg_color)
{
static int current_pair = -1;
if (current_pair != -1)
attroff(COLOR_PAIR(current_pair));
if (fg_color != Color::Default or bg_color != Color::Default)
{
current_pair = get_color_pair(fg_color, bg_color);
attron(COLOR_PAIR(current_pair));
}
}
NCursesClient::NCursesClient()
: m_menu(nullptr)
{
// setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
curs_set(0);
start_color();
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);
}
NCursesClient::~NCursesClient()
{
endwin();
}
void NCursesClient::draw_window(Window& window) void NCursesClient::draw_window(Window& window)
{ {
int max_x,max_y; int max_x,max_y;
@ -231,6 +238,8 @@ void NCursesClient::show_menu(const memoryview<String>& choices)
set_menu_sub(m_menu, derwin(stdscr, max_y - pos_y - 1, max_x, pos_y, 0)); set_menu_sub(m_menu, derwin(stdscr, max_y - pos_y - 1, max_x, pos_y, 0));
set_menu_format(m_menu, lines, columns); set_menu_format(m_menu, lines, columns);
set_menu_mark(m_menu, nullptr); set_menu_mark(m_menu, nullptr);
set_menu_fore(m_menu, COLOR_PAIR(m_menu_fg));
set_menu_back(m_menu, COLOR_PAIR(m_menu_bg));
post_menu(m_menu); post_menu(m_menu);
refresh(); refresh();
} }
@ -240,23 +249,23 @@ void NCursesClient::menu_ctrl(MenuCommand command)
switch(command) switch(command)
{ {
case MenuCommand::SelectFirst: case MenuCommand::SelectFirst:
set_menu_fore(m_menu, A_STANDOUT); set_menu_fore(m_menu, COLOR_PAIR(m_menu_fg));
menu_driver(m_menu, REQ_FIRST_ITEM); menu_driver(m_menu, REQ_FIRST_ITEM);
break; break;
case MenuCommand::SelectLast: case MenuCommand::SelectLast:
set_menu_fore(m_menu, A_STANDOUT); set_menu_fore(m_menu, COLOR_PAIR(m_menu_fg));
menu_driver(m_menu, REQ_LAST_ITEM); menu_driver(m_menu, REQ_LAST_ITEM);
break; break;
case MenuCommand::SelectNext: case MenuCommand::SelectNext:
set_menu_fore(m_menu, A_STANDOUT); set_menu_fore(m_menu, COLOR_PAIR(m_menu_fg));
menu_driver(m_menu, REQ_NEXT_ITEM); menu_driver(m_menu, REQ_NEXT_ITEM);
break; break;
case MenuCommand::SelectPrev: case MenuCommand::SelectPrev:
set_menu_fore(m_menu, A_STANDOUT); set_menu_fore(m_menu, COLOR_PAIR(m_menu_fg));
menu_driver(m_menu, REQ_PREV_ITEM); menu_driver(m_menu, REQ_PREV_ITEM);
break; break;
case MenuCommand::SelectNone: case MenuCommand::SelectNone:
set_menu_fore(m_menu, A_NORMAL); set_menu_fore(m_menu, COLOR_PAIR(m_menu_bg));
break; break;
case MenuCommand::Close: case MenuCommand::Close:
{ {

View File

@ -30,6 +30,9 @@ private:
std::vector<ITEM*> m_items; std::vector<ITEM*> m_items;
std::vector<String> m_counts; std::vector<String> m_counts;
std::vector<String> m_choices; std::vector<String> m_choices;
int m_menu_fg;
int m_menu_bg;
}; };
} }