From 42ef9a789644980d33e1731aa44055edb66f2388 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thorsten=20Wi=C3=9Fmann?= Date: Sun, 8 Apr 2012 13:15:57 +0200 Subject: [PATCH] Add basic color support This adds optional support for an colorized ui, providing configurable and documented foreground/background colors for the: * header * footer * odd list entries * list-header * list-selection * even list entries * tab-border * tab-label * field-name * field-value --- abookrc.5 | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ color.h | 21 ++++++++++++++++ edit.c | 7 ++++++ list.c | 12 ++++++++- options.c | 23 ++++++++++++++++- options.h | 21 ++++++++++++++++ ui.c | 56 +++++++++++++++++++++++++++++++++++++++++ ui.h | 1 + 8 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 color.h diff --git a/abookrc.5 b/abookrc.5 index 04e5149..fef99d6 100644 --- a/abookrc.5 +++ b/abookrc.5 @@ -146,6 +146,80 @@ Defines the field to be used by the "sort by field" command. Default is "nick" ( \fBshow_cursor\fP=[true|false] Defines if the cursor is visible in main display. Default is false. +.TP +\fBuse_colors\fP=[true|false] +Defines if the output of abook is colorized. Default is false. + +.TP +Color settings: +\fBcolor_header_fg\fP=color +Foreground color for header bar + +\fBcolor_header_bg\fP=color +Background color for header bar + +\fBcolor_footer_fg\fP=color +Foreground color for footer bar + +\fBcolor_footer_bg\fP=color +Background color for footer bar + +\fBcolor_list_even_fg\fP=color +Foreground color for normal list entries with even index in the list (starting +with index 0) + +\fBcolor_list_even_bg\fP=color +Background color for normal list entries with even index in the list (starting +with index 0) + +\fBcolor_list_odd_fg\fP=color +Foreground color for normal list entries with odd index in the list (starting +with index 0) + +\fBcolor_list_odd_bg\fP=color +Background color for normal list entries with odd index in the list (starting +with index 0) + +\fBcolor_list_header_fg\fP=color +Foreground color for the list header + +\fBcolor_list_header_bg\fP=color +Background color for the list header + +\fBcolor_list_highlight_fg\fP=color +Foreground color for highlighted list entries + +\fBcolor_list_highlight_bg\fP=color +Background color for highlighted list entries + +\fBcolor_tab_border_fg\fP=color +Foreground color for tab borders on details page + +\fBcolor_tab_border_bg\fP=color +Background color for tab borders on details page + +\fBcolor_tab_label_fg\fP=color +Foreground color for tab labes on details page + +\fBcolor_tab_label_bg\fP=color +Background color for tab labes on details page + +\fBcolor_field_name_fg\fP=color +Foreground color for field names on details page + +\fBcolor_field_name_bg\fP=color +Background color for field names on details page + +\fBcolor_field_value_fg\fP=color +Foreground color for field values on details page + +\fBcolor_field_value_bg\fP=color +Background color for field values on details page + +Where \fBcolor\fP can be: default, black, red, green, yellow, blue, magenta, cyan, white + + + .SH SAMPLE CONFIGURATION FILE .nf diff --git a/color.h b/color.h new file mode 100644 index 0000000..9063ff5 --- /dev/null +++ b/color.h @@ -0,0 +1,21 @@ + +#ifndef __ABOOK_COLORS_H_ +#define __ABOOK_COLORS_H_ + +#define COLOR_DEFAULT -1 + +enum { + CP_HEADER = 1, + CP_FOOTER, + CP_LIST_EVEN, + CP_LIST_ODD, + CP_LIST_HEADER, + CP_LIST_HIGHLIGHT, + CP_TAB_BORDER, + CP_TAB_LABEL, + CP_FIELD_NAME, + CP_FIELD_VALUE, +}; + +#endif + diff --git a/edit.c b/edit.c index d2cba62..81da889 100644 --- a/edit.c +++ b/edit.c @@ -21,6 +21,7 @@ #include "misc.h" #include "views.h" #include "xmalloc.h" +#include "color.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -47,6 +48,7 @@ editor_tab(const int tab) int x_pos = 2; /* current x pos */ char *tab_name; + wattrset(editw, COLOR_PAIR(CP_TAB_BORDER)); mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS); for(i = 0; i < views_count; i++) { @@ -63,7 +65,9 @@ editor_tab(const int tab) mvwaddch(editw, TABLINE, x_pos, UI_ULCORNER_CHAR); mvwaddch(editw, TABLINE, x_pos + 1, UI_LBOXLINE_CHAR); + wattrset(editw, COLOR_PAIR(CP_TAB_LABEL)); mvwaddstr(editw, TABLINE, x_pos + 2, tab_name); + wattrset(editw, COLOR_PAIR(CP_TAB_BORDER)); mvwaddch(editw, TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR); mvwaddch(editw, TABLINE, x_pos + width - 2, UI_URCORNER_CHAR); @@ -192,6 +196,7 @@ print_editor_header(int item) else snprintf(header, EDITW_COLS, "%s", db_name_get(item)); + wattrset(editw, COLOR_PAIR(CP_TAB_LABEL)); mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header); free(header); @@ -215,6 +220,7 @@ editor_print_data(int tab, int item) } else y = FIELDS_START_Y; + wattrset(editw, COLOR_PAIR(CP_FIELD_NAME)); mvwprintw(editw, y, FIELDS_START_X, "%c - ", (j < 10) ? '0' + j : 'A' + j - 10); mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name, @@ -222,6 +228,7 @@ editor_print_data(int tab, int item) FIELDNAME_MAX_WIDTH)); mvwaddch(editw, y, TAB_COLON_POS, ':'); + wattrset(editw, COLOR_PAIR(CP_FIELD_VALUE)); if((cur->field->type == FIELD_EMAILS) || (cur->field->type == FIELD_LIST)) { abook_list *emails, *e; diff --git a/list.c b/list.c index 7a04397..9eb4ede 100644 --- a/list.c +++ b/list.c @@ -19,6 +19,7 @@ #include "misc.h" #include "options.h" #include "xmalloc.h" +#include "color.h" int curitem = -1; @@ -194,7 +195,10 @@ print_list_field(int item, int line, int *x_pos, struct index_elem *e) static void highlight_line(WINDOW *win, int line) { - wstandout(win); + wattrset(win, COLOR_PAIR(CP_LIST_HIGHLIGHT)); + if(!opt_get_bool(BOOL_USE_COLORS)) { + wstandout(win); + } /* * this is a tricky one @@ -224,6 +228,10 @@ print_list_line(int item, int line, int highlight) struct index_elem *cur; int x_pos = 1; + if(item % 2 == 0) + wattrset(list, COLOR_PAIR(CP_LIST_EVEN)); + else + wattrset(list, COLOR_PAIR(CP_LIST_ODD)); scrollok(list, FALSE); if(highlight) highlight_line(list, line); @@ -300,6 +308,8 @@ list_headerline() #if defined(A_BOLD) && defined(A_NORMAL) attrset(A_BOLD); #endif + attrset(COLOR_PAIR(CP_LIST_HEADER)); + mvhline(2, 0, ' ', COLS); for(e = index_elements; e; e = e->next) if(e->type == INDEX_TEXT) diff --git a/options.c b/options.c index 84f08ca..57cb334 100644 --- a/options.c +++ b/options.c @@ -67,7 +67,28 @@ static struct option abook_vars[] = { { "preserve_fields", OT_STR, STR_PRESERVE_FIELDS, UL "standard" }, { "sort_field", OT_STR, STR_SORT_FIELD, UL "nick" }, { "show_cursor", OT_BOOL, BOOL_SHOW_CURSOR, FALSE }, - + { "use_colors", OT_BOOL, BOOL_USE_COLORS, FALSE }, + { "color_header_fg", OT_STR, STR_COLOR_HEADER_FG, UL "blue" }, + { "color_header_fg", OT_STR, STR_COLOR_HEADER_FG, UL "blue" }, + { "color_header_bg", OT_STR, STR_COLOR_HEADER_BG, UL "red" }, + { "color_footer_fg", OT_STR, STR_COLOR_FOOTER_FG, UL "red" }, + { "color_footer_bg", OT_STR, STR_COLOR_FOOTER_BG, UL "default" }, + { "color_list_even_fg", OT_STR, STR_COLOR_LIST_EVEN_FG, UL "yellow" }, + { "color_list_even_bg", OT_STR, STR_COLOR_LIST_EVEN_BG, UL "default" }, + { "color_list_odd_fg", OT_STR, STR_COLOR_LIST_ODD_FG, UL "default" }, + { "color_list_odd_bg", OT_STR, STR_COLOR_LIST_ODD_BG, UL "default" }, + { "color_list_header_fg", OT_STR, STR_COLOR_LIST_HEADER_FG, UL "white" }, + { "color_list_header_bg", OT_STR, STR_COLOR_LIST_HEADER_BG, UL "blue" }, + { "color_list_highlight_fg", OT_STR, STR_COLOR_LIST_HIGHLIGHT_FG, UL "black" }, + { "color_list_highlight_bg", OT_STR, STR_COLOR_LIST_HIGHLIGHT_BG, UL "green" }, + { "color_tab_border_fg", OT_STR, STR_COLOR_TAB_BORDER_FG, UL "cyan" }, + { "color_tab_border_bg", OT_STR, STR_COLOR_TAB_BORDER_BG, UL "default" }, + { "color_tab_label_fg", OT_STR, STR_COLOR_TAB_LABEL_FG, UL "magenta" }, + { "color_tab_label_bg", OT_STR, STR_COLOR_TAB_LABEL_BG, UL "default" }, + { "color_field_name_fg", OT_STR, STR_COLOR_FIELD_NAME_FG, UL "yellow" }, + { "color_field_name_bg", OT_STR, STR_COLOR_FIELD_NAME_BG, UL "default" }, + { "color_field_value_fg", OT_STR, STR_COLOR_FIELD_VALUE_FG, UL "green" }, + { "color_field_value_bg", OT_STR, STR_COLOR_FIELD_VALUE_BG, UL "default" }, { NULL } }; diff --git a/options.h b/options.h index 138efc2..2dbadc3 100644 --- a/options.h +++ b/options.h @@ -26,6 +26,7 @@ enum bool_opts { BOOL_USE_ASCII_ONLY, BOOL_ADD_EMAIL_PREVENT_DUPLICATES, BOOL_SHOW_CURSOR, + BOOL_USE_COLORS, BOOL_MAX }; @@ -53,6 +54,26 @@ enum str_opts { STR_ADDRESS_STYLE, STR_PRESERVE_FIELDS, STR_SORT_FIELD, + STR_COLOR_HEADER_FG, + STR_COLOR_HEADER_BG, + STR_COLOR_FOOTER_FG, + STR_COLOR_FOOTER_BG, + STR_COLOR_LIST_EVEN_FG, + STR_COLOR_LIST_EVEN_BG, + STR_COLOR_LIST_ODD_FG, + STR_COLOR_LIST_ODD_BG, + STR_COLOR_LIST_HEADER_FG, + STR_COLOR_LIST_HEADER_BG, + STR_COLOR_LIST_HIGHLIGHT_FG, + STR_COLOR_LIST_HIGHLIGHT_BG, + STR_COLOR_TAB_BORDER_FG, + STR_COLOR_TAB_BORDER_BG, + STR_COLOR_TAB_LABEL_FG, + STR_COLOR_TAB_LABEL_BG, + STR_COLOR_FIELD_NAME_FG, + STR_COLOR_FIELD_NAME_BG, + STR_COLOR_FIELD_VALUE_FG, + STR_COLOR_FIELD_VALUE_BG, STR_MAX }; diff --git a/ui.c b/ui.c index 1d5eb43..69fb71d 100644 --- a/ui.c +++ b/ui.c @@ -25,6 +25,7 @@ #include "options.h" #include "filter.h" #include "xmalloc.h" +#include "color.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -130,6 +131,57 @@ ui_init_curses() nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); + if(opt_get_bool(BOOL_USE_COLORS)) { + start_color(); + use_default_colors(); + ui_init_color_pairs_user(); + } +} + + +#define CHECK_COLOR_NAME(value, name, DEFNAME) \ + if(!strcmp((name), (value))){ \ + return DEFNAME; \ + } +short +opt_color_to_color(enum str_opts enum_name) +{ + char* name = opt_get_str(enum_name); + CHECK_COLOR_NAME(name, "default", COLOR_DEFAULT) + else CHECK_COLOR_NAME(name, "black", COLOR_BLACK) + else CHECK_COLOR_NAME(name, "red", COLOR_RED) + else CHECK_COLOR_NAME(name, "green", COLOR_GREEN) + else CHECK_COLOR_NAME(name, "yellow", COLOR_YELLOW) + else CHECK_COLOR_NAME(name, "blue", COLOR_BLUE) + else CHECK_COLOR_NAME(name, "magenta", COLOR_MAGENTA) + else CHECK_COLOR_NAME(name, "cyan", COLOR_CYAN) + else CHECK_COLOR_NAME(name, "white", COLOR_WHITE) + else return COLOR_DEFAULT; +} + +void +ui_init_color_pairs_user() +{ + init_pair(CP_HEADER, opt_color_to_color(STR_COLOR_HEADER_FG), + opt_color_to_color(STR_COLOR_HEADER_BG)); + init_pair(CP_FOOTER, opt_color_to_color(STR_COLOR_FOOTER_FG), + opt_color_to_color(STR_COLOR_FOOTER_BG)); + init_pair(CP_LIST_EVEN, opt_color_to_color(STR_COLOR_LIST_EVEN_FG), + opt_color_to_color(STR_COLOR_LIST_EVEN_BG)); + init_pair(CP_LIST_ODD, opt_color_to_color(STR_COLOR_LIST_ODD_FG), + opt_color_to_color(STR_COLOR_LIST_ODD_BG)); + init_pair(CP_LIST_HEADER, opt_color_to_color(STR_COLOR_LIST_HEADER_FG), + opt_color_to_color(STR_COLOR_LIST_HEADER_BG)); + init_pair(CP_LIST_HIGHLIGHT, opt_color_to_color(STR_COLOR_LIST_HIGHLIGHT_FG), + opt_color_to_color(STR_COLOR_LIST_HIGHLIGHT_BG)); + init_pair(CP_TAB_BORDER, opt_color_to_color(STR_COLOR_TAB_BORDER_FG), + opt_color_to_color(STR_COLOR_TAB_BORDER_BG)); + init_pair(CP_TAB_LABEL, opt_color_to_color(STR_COLOR_TAB_LABEL_FG), + opt_color_to_color(STR_COLOR_TAB_LABEL_BG)); + init_pair(CP_FIELD_NAME, opt_color_to_color(STR_COLOR_FIELD_NAME_FG), + opt_color_to_color(STR_COLOR_FIELD_NAME_BG)); + init_pair(CP_FIELD_VALUE, opt_color_to_color(STR_COLOR_FIELD_VALUE_FG), + opt_color_to_color(STR_COLOR_FIELD_VALUE_BG)); } int @@ -179,6 +231,8 @@ headerline(const char *str) { werase(top); + wattrset(top, COLOR_PAIR(CP_HEADER)); + mvwhline(top, 0, 0, ' ', COLS); mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS); mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str); @@ -355,6 +409,7 @@ refresh_statusline() { werase(bottom); + wattrset(bottom, COLOR_PAIR(CP_FOOTER)); mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS); refresh(); @@ -593,6 +648,7 @@ ui_print_number_of_items() char *str = strdup_printf(" " "|%3d/%3d", selected_items(), db_n_items()); + attrset(COLOR_PAIR(CP_HEADER)); mvaddstr(0, COLS-strlen(str), str); free(str); diff --git a/ui.h b/ui.h index 25bc850..3e7483b 100644 --- a/ui.h +++ b/ui.h @@ -10,6 +10,7 @@ enum { int is_ui_initialized(); void ui_init_curses(); +void ui_init_color_pairs_user(); int init_ui(); void close_ui(); void headerline(const char *str); -- 2.39.2