From d66700a0e4d20a9193ea83490781c0858d98c3f6 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 11 Feb 2012 17:07:56 -0800 Subject: [PATCH] Color work --- fish_pager.cpp | 22 ++--- highlight.cpp | 49 +++++++++- highlight.h | 2 + output.cpp | 246 ++++++++++++++++++++++++++++++++++++++++++++++++- output.h | 5 +- reader.cpp | 8 +- screen.cpp | 4 +- screen.h | 166 ++++++++++++++++++++++++++++++--- set_color.cpp | 6 +- 9 files changed, 471 insertions(+), 37 deletions(-) diff --git a/fish_pager.cpp b/fish_pager.cpp index 822e2426f..f5307d304 100644 --- a/fish_pager.cpp +++ b/fish_pager.cpp @@ -199,14 +199,14 @@ struct comp_t This function translates from a highlight code to a specific color by check invironement variables */ -static int get_color( int highlight ) +static rgb_color_t get_color( int highlight ) { const wchar_t *val; if( highlight < 0 ) - return FISH_COLOR_NORMAL; + return rgb_color_t::normal(); if( highlight >= (4) ) - return FISH_COLOR_NORMAL; + return rgb_color_t::normal(); val = wgetenv( hightlight_var[highlight]); @@ -217,10 +217,10 @@ static int get_color( int highlight ) if( !val ) { - return FISH_COLOR_NORMAL; + return rgb_color_t::normal(); } - return output_color_code( val, false ); + return parse_color( val, false ); } /** @@ -424,9 +424,9 @@ static void completion_print_item( const wchar_t *prefix, comp_t *c, int width ) const wcstring &comp = c->comp.at(i); if( i != 0 ) written += print_max( L" ", comp_width - written, 2 ); - set_color( get_color(HIGHLIGHT_PAGER_PREFIX),FISH_COLOR_NORMAL ); + set_color( get_color(HIGHLIGHT_PAGER_PREFIX),rgb_color_t::normal() ); written += print_max( prefix, comp_width - written, comp.empty()?0:1 ); - set_color( get_color(HIGHLIGHT_PAGER_COMPLETION),FISH_COLOR_IGNORE ); + set_color( get_color(HIGHLIGHT_PAGER_COMPLETION),rgb_color_t::ignore() ); written += print_max( comp.c_str(), comp_width - written, i!=(c->comp.size()-1) ); } @@ -439,8 +439,7 @@ static void completion_print_item( const wchar_t *prefix, comp_t *c, int width ) writech( L' '); } written += print_max( L"(", 1, 0 ); - set_color( get_color( HIGHLIGHT_PAGER_DESCRIPTION ), - FISH_COLOR_IGNORE ); + set_color( get_color( HIGHLIGHT_PAGER_DESCRIPTION ), rgb_color_t::ignore() ); written += print_max( c->desc.c_str(), desc_width, 0 ); written += print_max( L")", 1, 0 ); } @@ -702,8 +701,7 @@ static int completion_try_print( int cols, string_buffer_t msg; sb_init( &msg ); - set_color( FISH_COLOR_BLACK, - get_color(HIGHLIGHT_PAGER_PROGRESS) ); + set_color( rgb_color_t::black(), get_color(HIGHLIGHT_PAGER_PROGRESS) ); sb_printf( &msg, _(L" %d to %d of %d"), pos, @@ -715,7 +713,7 @@ static int completion_try_print( int cols, writestr((wchar_t *)msg.buff); sb_destroy( &msg ); - set_color( FISH_COLOR_NORMAL, FISH_COLOR_NORMAL ); + set_color( rgb_color_t::normal(), rgb_color_t::normal() ); pager_flush(); int c = readch(); diff --git a/highlight.cpp b/highlight.cpp index 84f72fd7b..6c4f38bee 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -166,6 +166,54 @@ static bool is_potential_path( const wcstring &cpath ) } +rgb_color_t highlight_get_rgb_color( int highlight, bool is_background ) +{ + size_t i; + int idx=0; + rgb_color_t result; + + if( highlight < 0 ) + return rgb_color_t::normal(); + if( highlight > (1< %d -> %ls", highlight, idx, val ); + + if (val_wstr.missing()) + val_wstr = env_get_string( highlight_var[0]); + + if( ! val_wstr.missing() ) + result = parse_color( val_wstr, is_background ); + + if( highlight & HIGHLIGHT_VALID_PATH ) + { + env_var_t val2_wstr = env_get_string( L"fish_color_valid_path" ); + const wcstring val2 = val2_wstr.missing() ? L"" : val2_wstr.c_str(); + + rgb_color_t result2 = parse_color( val2, is_background ); + if( result == rgb_color_t::normal() ) + result = result2; + else + { + if( result2.is_bold() ) + result.set_bold(true); + if( result2.is_underline() ) + result.set_underline(true); + } + + } + return result; +} int highlight_get_color( int highlight, bool is_background ) @@ -215,7 +263,6 @@ int highlight_get_color( int highlight, bool is_background ) } return result; - } /** diff --git a/highlight.h b/highlight.h index d55fa1913..73500d446 100644 --- a/highlight.h +++ b/highlight.h @@ -9,6 +9,7 @@ #include "env.h" #include "util.h" +#include "screen.h" /** Internal value representing highlighting of normal text @@ -103,5 +104,6 @@ void highlight_universal( const wchar_t *buff, int *color, int pos, wcstring_lis FISH_COLOR_RED. */ int highlight_get_color( int highlight, bool is_background ); +rgb_color_t highlight_get_rgb_color( int highlight, bool is_background ); #endif diff --git a/output.cpp b/output.cpp index 18b0552e5..fce1105d8 100644 --- a/output.cpp +++ b/output.cpp @@ -126,6 +126,216 @@ int (*output_get_writer())(char) return out; } +void set_color(rgb_color_t c, rgb_color_t c2) +{ + ASSERT_IS_MAIN_THREAD(); + + const rgb_color_t normal = rgb_color_t::normal(); + static rgb_color_t last_color = rgb_color_t::normal(); + static rgb_color_t last_color2 = rgb_color_t::normal(); + static int was_bold=0; + static int was_underline=0; + int bg_set=0, last_bg_set=0; + char *fg = 0, *bg=0; + + int is_bold = 0; + int is_underline = 0; + + /* + Test if we have at least basic support for setting fonts, colors + and related bits - otherwise just give up... + */ + if( !exit_attribute_mode ) + { + return; + } + + + is_bold |= c.is_bold(); + is_bold |= c2.is_bold(); + + is_underline |= c.is_underline(); + is_underline |= c2.is_underline(); + + if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) ) + { + fg = set_a_foreground; + bg = set_a_background; + } + else if( (set_foreground != 0) && (strlen( set_foreground) != 0 ) ) + { + fg = set_foreground; + bg = set_background; + } + + if( c.is_reset() || c2.is_reset()) + { + c = c2 = normal; + was_bold=0; + was_underline=0; + if( fg ) + { + /* + If we exit attibute mode, we must first set a color, or + previously coloured text might lose it's + color. Terminals are weird... + */ + writembs( tparm( fg, 0 ) ); + } + writembs( exit_attribute_mode ); + return; + } + + if( was_bold && !is_bold ) + { + /* + Only way to exit bold mode is a reset of all attributes. + */ + writembs( exit_attribute_mode ); + last_color = normal; + last_color2 = normal; + was_bold=0; + was_underline=0; + } + + if( last_color2 != rgb_color_t::normal() && + last_color2 != rgb_color_t::reset() && + last_color2 != rgb_color_t::ignore() ) + { + /* + Background was set + */ + last_bg_set=1; + } + + if( c2 != rgb_color_t::normal() && + c2 != rgb_color_t::ignore() ) + { + /* + Background is set + */ + bg_set=1; + // PCA color fix + //c = (c2==FISH_COLOR_WHITE)?FISH_COLOR_BLACK:FISH_COLOR_WHITE; + } + + if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0)) + { + if(bg_set && !last_bg_set) + { + /* + Background color changed and is set, so we enter bold + mode to make reading easier. This means bold mode is + _always_ on when the background color is set. + */ + writembs( enter_bold_mode ); + } + if(!bg_set && last_bg_set) + { + /* + Background color changed and is no longer set, so we + exit bold mode + */ + writembs( exit_attribute_mode ); + was_bold=0; + was_underline=0; + /* + We don't know if exit_attribute_mode resets colors, so + we set it to something known. + */ + if( fg ) + { + writembs( tparm( fg, 0 ) ); + last_color=rgb_color_t::black(); + } + } + } + + if( last_color != c ) + { + if( c==rgb_color_t::normal() ) + { + if( fg ) + { + writembs( tparm( fg, 0 ) ); + } + writembs( exit_attribute_mode ); + + last_color2 = rgb_color_t::normal(); + was_bold=0; + was_underline=0; + } + else if( c.is_named() ) + { + if( fg ) + { + writembs( tparm( fg, c.name_index() ) ); + } + } + } + + last_color = c; + + if( last_color2 != c2 ) + { + if( c2 == rgb_color_t::normal() ) + { + if( bg ) + { + writembs( tparm( bg, 0 ) ); + } + + writembs( exit_attribute_mode ); + if( ( last_color != rgb_color_t::normal() ) && fg ) + { + if( fg ) + { + writembs( tparm( fg, last_color.name_index() ) ); + } + } + + + was_bold=0; + was_underline=0; + last_color2 = c2; + } + else if ( c2.is_named() && c2 != normal) + { + if( bg ) + { + writembs( tparm( bg, c2.name_index() ) ); + } + last_color2 = c2; + } + } + + /* + Lastly, we set bold mode and underline mode correctly + */ + if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set ) + { + if( is_bold && !was_bold ) + { + if( enter_bold_mode ) + { + writembs( tparm( enter_bold_mode ) ); + } + } + was_bold = is_bold; + } + + if( was_underline && !is_underline ) + { + writembs( exit_underline_mode ); + } + + if( !was_underline && is_underline ) + { + writembs( enter_underline_mode ); + } + was_underline = is_underline; + +} void set_color( int c, int c2 ) { @@ -563,8 +773,42 @@ int output_color_code( const wcstring &val, bool is_background ) { } - return color | (is_bold?FISH_COLOR_BOLD:0) | (is_underline?FISH_COLOR_UNDERLINE:0); + return color | (is_bold?FISH_COLOR_BOLD:0) | (is_underline?FISH_COLOR_UNDERLINE:0); +} + +rgb_color_t parse_color( const wcstring &val, bool is_background ) { + rgb_color_t result; + + int is_bold=0; + int is_underline=0; + + wcstring_list_t el; + tokenize_variable_array( val, el ); + for(size_t j=0; j < el.size(); j++ ) { + const wcstring &next = el.at(j); + wcstring color_name; + if (is_background) { + // look for something like "--background=red" + const wcstring prefix = L"--background="; + if (string_prefixes_string(prefix, next)) { + color_name = wcstring(next, prefix.size()); + } + + } else { + if (next == L"--bold" || next == L"-o") + is_bold = true; + else if (next == L"--underline" || next == L"-u") + is_underline = true; + else + color_name = next; + } + + if (! color_name.empty()) { + rgb_color_t::parse(color_name, result); + } + } + return result; } void output_set_term( const wchar_t *term ) diff --git a/output.h b/output.h index 89254a38c..d1a1ad898 100644 --- a/output.h +++ b/output.h @@ -9,6 +9,7 @@ #define FISH_OUTPUT_H #include +#include "screen.h" /** Constants for various colors as used by the set_color function. @@ -71,7 +72,8 @@ enum */ -void set_color( int c, int c2 ); +//void set_color( int c, int c2 ); +void set_color(rgb_color_t c, rgb_color_t c2); /** Write specified multibyte string @@ -129,6 +131,7 @@ int write_escaped_str( const wchar_t *str, int max_len ); Return the internal color code representing the specified color */ int output_color_code( const wcstring &val, bool is_background ); +rgb_color_t parse_color( const wcstring &val, bool is_background ); /** This is for writing process notification messages. Has to write to diff --git a/reader.cpp b/reader.cpp index b70c2ea73..1de988fe7 100644 --- a/reader.cpp +++ b/reader.cpp @@ -373,7 +373,7 @@ static int exit_forced; */ static void term_donate() { - set_color(FISH_COLOR_NORMAL, FISH_COLOR_NORMAL); + set_color(rgb_color_t::normal(), rgb_color_t::normal()); while( 1 ) { @@ -697,7 +697,7 @@ void reader_write_title() } } proc_pop_interactive(); - set_color( FISH_COLOR_RESET, FISH_COLOR_RESET ); + set_color( rgb_color_t::reset(), rgb_color_t::reset() ); } /** @@ -1708,7 +1708,7 @@ static void reader_interactive_destroy() { kill_destroy(); writestr( L"\n" ); - set_color( FISH_COLOR_RESET, FISH_COLOR_RESET ); + set_color( rgb_color_t::reset(), rgb_color_t::reset() ); input_destroy(); } @@ -3300,7 +3300,7 @@ const wchar_t *reader_readline() wperror(L"tcsetattr"); } - set_color( FISH_COLOR_RESET, FISH_COLOR_RESET ); + set_color( rgb_color_t::reset(), rgb_color_t::reset() ); } return finished ? data->command_line.c_str() : 0; diff --git a/screen.cpp b/screen.cpp index e74d4e8be..388d474b1 100644 --- a/screen.cpp +++ b/screen.cpp @@ -530,8 +530,8 @@ static void s_set_color( screen_t *s, buffer_t *b, int c ) s_writeb_buffer = b; unsigned int uc = (unsigned int)c; - set_color( highlight_get_color( uc & 0xffff, false ), - highlight_get_color( (uc>>16)&0xffff, true ) ); + set_color( highlight_get_rgb_color( uc & 0xffff, false ), + highlight_get_rgb_color( (uc>>16)&0xffff, true ) ); output_set_writer( writer_old ); diff --git a/screen.h b/screen.h index 3d6eb72db..aa88be118 100644 --- a/screen.h +++ b/screen.h @@ -14,24 +14,166 @@ #include +/* A type that represents a color */ +class rgb_color_t { + enum { + type_none, + type_named, + type_rgb, + type_reset, + type_ignore + }; + unsigned char type; + union { + unsigned char name_idx; //0-10 + unsigned char rgb[3]; + } data; + unsigned flags; + + /* Try parsing an explicit color name like "magenta" */ + bool try_parse_named(const wcstring &str) { + bzero(&data, sizeof data); + const struct {const wchar_t * name; unsigned char idx;} names[] = { + {L"black", 0}, + {L"red", 1}, + {L"green", 2}, + {L"brown", 3}, + {L"yellow", 3}, + {L"blue", 4}, + {L"magenta", 5}, + {L"purple", 5}, + {L"cyan", 6}, + {L"white", 7}, + {L"normal", 8} + }; + size_t max = sizeof names / sizeof *names; + for (size_t idx=0; idx < max; idx++) { + if (0 == wcscasecmp(str.c_str(), names[idx].name)) { + data.name_idx = names[idx].idx; + return true; + } + } + return false; + } + + static int parse_hex_digit(wchar_t x) { + switch (x) { + case L'0': return 0x0; + case L'1': return 0x1; + case L'2': return 0x2; + case L'3': return 0x3; + case L'4': return 0x4; + case L'5': return 0x5; + case L'6': return 0x6; + case L'7': return 0x7; + case L'8': return 0x8; + case L'9': return 0x9; + case L'a':case L'A': return 0xA; + case L'b':case L'B': return 0xB; + case L'c':case L'C': return 0xC; + case L'd':case L'D': return 0xD; + case L'e':case L'E': return 0xE; + case L'f':case L'F': return 0xF; + default: return -1; + } + } + + bool try_parse_rgb(const wcstring &name) { + bzero(&data, sizeof data); + /* We support the following style of rgb formats (case insensitive): + #FA3 + #F3A035 + */ + size_t i; + if (name.size() == 4 && name.at(0) == L'#') { + // type #FA3 + for (i=0; i < 3; i++) { + int val = parse_hex_digit(name.at(i)); + if (val < 0) break; + data.rgb[i] = val*16+val; + } + return i == 3; + } else if (name.size() == 7 && name.at(0) == L'#') { + // type #F3A035 + for (i=0; i < 6;) { + int hi = parse_hex_digit(name.at(i++)); + int lo = parse_hex_digit(name.at(i++)); + if (lo < 0 || hi < 0) break; + data.rgb[i] = hi*16+lo; + } + return i == 6; + } else { + return false; + } + } + + static rgb_color_t named_color(unsigned char which) + { + rgb_color_t result(type_named); + result.data.name_idx = which; + return result; + } + + public: + + explicit rgb_color_t(unsigned char t = type_none) : type(t), data(), flags() {} + + static rgb_color_t normal() { return named_color(8); } + static rgb_color_t white() { return named_color(7); } + static rgb_color_t black() { return named_color(0); } + static rgb_color_t reset() { return rgb_color_t(type_reset); } + static rgb_color_t ignore() { return rgb_color_t(type_ignore); } + + rgb_color_t(unsigned char r, unsigned char g, unsigned char b) + { + type = type_rgb; + data.rgb[0] = r; + data.rgb[1] = g; + data.rgb[2] = b; + } + + static bool parse(const wcstring &str, rgb_color_t &color) { + if (color.try_parse_named(str)) { + color.type = type_named; + } else if (color.try_parse_rgb(str)) { + color.type = type_rgb; + } else { + bzero(color.data.rgb, sizeof color.data.rgb); + color.type = type_none; + } + return color.type != type_none; + } + + unsigned char name_index() const { + assert(type == type_named); + return data.name_idx; + } + + bool is_bold() const { return flags & 1; } + void set_bold(bool x) { if (x) flags |= 1; else flags &= ~1; } + bool is_underline() const { return flags & 2; } + void set_underline(bool x) { if (x) flags |= 2; else flags &= ~2; } + + bool is_reset(void) const { return type == type_reset; } + bool is_ignore(void) const { return type == type_ignore; } + bool is_named(void) const { return type == type_named; } + + bool operator==(const rgb_color_t &other) const { + return type == other.type && ! memcmp(&data, &other.data, sizeof data); + } + + bool operator!=(const rgb_color_t &other) const { + return !(*this == other); + } + +}; + struct line_entry_t { wchar_t text; int color; }; -class rgb_color_t { - unsigned char rgb[3]; - public: - - rgb_color_t(unsigned char r, unsigned char g, unsigned char b) - { - rgb[0] = r; - rgb[1] = g; - rgb[2] = b; - } -}; - /** A class representing a single line of a screen. */ diff --git a/set_color.cpp b/set_color.cpp index 61c6312f5..181ad9ab2 100644 --- a/set_color.cpp +++ b/set_color.cpp @@ -76,8 +76,7 @@ const char *col[]= "cyan", "white", "normal" -} -; +}; const int col_idx[]= { @@ -92,8 +91,7 @@ const int col_idx[]= 6, 7, 8 -} -; +}; int translate_color( char *str ) {