From 7320103ce09081bd91399522a4eb003c58289679 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Mon, 8 Jun 2009 09:37:53 +0200 Subject: [PATCH] Cosmetics. Better handling of the attribute in color and monochrome modes. --- REVISION_NUMBER | 2 +- selector.cc | 89 ++++++++++++++++++++----------------------------- 2 files changed, 38 insertions(+), 53 deletions(-) diff --git a/REVISION_NUMBER b/REVISION_NUMBER index fa8f08c..1b9cba4 100644 --- a/REVISION_NUMBER +++ b/REVISION_NUMBER @@ -1 +1 @@ -150 +151 diff --git a/selector.cc b/selector.cc index a62d352..a22329c 100644 --- a/selector.cc +++ b/selector.cc @@ -64,6 +64,8 @@ int error_flash = 0; #define COLOR_HIGHLIGHTED_LINE 2 #define COLOR_ERROR 3 +int attr_modeline, attr_highlighted_line, attr_error; + ////////////////////////////////////////////////////////////////////// void inject_into_tty_buffer(char *string) { @@ -125,18 +127,6 @@ void error_feedback() { } } -void print_error_message(const char *message, int width) { - if(with_colors) { - attron(COLOR_PAIR(COLOR_ERROR)); - addnstr(message, width); - attroff(COLOR_PAIR(COLOR_ERROR)); - } else { - attron(A_STANDOUT); - addnstr(message, width); - attroff(A_STANDOUT); - } -} - ////////////////////////////////////////////////////////////////////// // A quick and dirty hash table @@ -367,7 +357,9 @@ void update_screen(int *current_line, int *temporary_line, int motion, addstr("\n"); if(matcher.regexp_error) { - print_error_message("[regexp error]", console_width); + attron(attr_error); + addnstr("[regexp error]", console_width); + attroff(attr_error); } else if(nb_lines > 0) { int new_line; if(match(lines[*current_line], &matcher)) { @@ -463,15 +455,9 @@ void update_screen(int *current_line, int *temporary_line, int motion, // Highlight the highlighted line ... if(l == new_line) { - if(with_colors) { - attron(COLOR_PAIR(COLOR_HIGHLIGHTED_LINE)); - addnstr(buffer, console_width); - attroff(COLOR_PAIR(COLOR_HIGHLIGHTED_LINE)); - } else { - attron(A_STANDOUT); - addnstr(buffer, console_width); - attroff(A_STANDOUT); - } + attron(attr_highlighted_line); + addnstr(buffer, console_width); + attroff(attr_highlighted_line); } else { addnstr(buffer, console_width); } @@ -488,10 +474,14 @@ void update_screen(int *current_line, int *temporary_line, int motion, *temporary_line = new_line; if(nb_printed_lines == 0) { - print_error_message("[no selection]\n", console_width); + attron(attr_error); + addnstr("[no selection]", console_width); + attroff(attr_error); } } else { - print_error_message("[empty choice]\n", console_width); + attron(attr_error); + addnstr("[empty choice]", console_width); + attroff(attr_error); } clrtobot(); @@ -500,11 +490,7 @@ void update_screen(int *current_line, int *temporary_line, int motion, move(0, 0); - if(with_colors) { - attron(COLOR_PAIR(COLOR_MODELINE)); - } else { - attron(A_REVERSE); - } + attron(attr_modeline); for(int k = 0; k < console_width; k++) buffer[k] = ' '; buffer[console_width] = '\0'; @@ -553,11 +539,7 @@ void update_screen(int *current_line, int *temporary_line, int motion, move(0, cursor_x); - if(with_colors) { - attroff(COLOR_PAIR(COLOR_MODELINE)); - } else { - attroff(A_REVERSE); - } + attroff(attr_modeline); // We are done @@ -901,29 +883,32 @@ int main(int argc, char **argv) { // So that the arrow keys work keypad(stdscr, TRUE); - if(with_colors) { + attr_error = A_STANDOUT; + attr_modeline = A_REVERSE; + attr_highlighted_line = A_STANDOUT; - if(has_colors()) { + if(with_colors && has_colors()) { - start_color(); + start_color(); - if(color_fg_modeline < 0 || color_fg_modeline >= COLORS || - color_bg_modeline < 0 || color_bg_modeline >= COLORS || - color_fg_highlight < 0 || color_bg_highlight >= COLORS || - color_bg_highlight < 0 || color_bg_highlight >= COLORS) { - echo(); - endwin(); - cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl; - exit(1); - } + if(color_fg_modeline < 0 || color_fg_modeline >= COLORS || + color_bg_modeline < 0 || color_bg_modeline >= COLORS || + color_fg_highlight < 0 || color_bg_highlight >= COLORS || + color_bg_highlight < 0 || color_bg_highlight >= COLORS) { + echo(); + endwin(); + cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl; + exit(1); + } - init_pair(COLOR_MODELINE, color_fg_modeline, color_bg_modeline); - init_pair(COLOR_HIGHLIGHTED_LINE, color_fg_highlight, color_bg_highlight); - init_pair(COLOR_ERROR, COLOR_WHITE, COLOR_RED); + init_pair(COLOR_MODELINE, color_fg_modeline, color_bg_modeline); + init_pair(COLOR_HIGHLIGHTED_LINE, color_fg_highlight, color_bg_highlight); + init_pair(COLOR_ERROR, COLOR_WHITE, COLOR_RED); + + attr_modeline = COLOR_PAIR(COLOR_MODELINE); + attr_highlighted_line = COLOR_PAIR(COLOR_HIGHLIGHTED_LINE); + attr_error = COLOR_PAIR(COLOR_ERROR); - } else { - with_colors = 0; - } } int key; -- 2.39.5