From dce189fc6d482cbbc49b9ad0fe9843235224ec9a Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 13 Feb 2012 09:52:17 -0800 Subject: [PATCH] Support for setting both RGB and named colors on the same line, so the same config can work for multiple term types --- highlight.cpp | 52 +-------------------------------------------------- highlight.h | 3 +-- output.cpp | 38 ++++++++++++++++++++++++++++++------- output.h | 3 +++ screen.cpp | 4 ++-- 5 files changed, 38 insertions(+), 62 deletions(-) diff --git a/highlight.cpp b/highlight.cpp index 490b201e2..8355e8a3e 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -166,7 +166,7 @@ static bool is_potential_path( const wcstring &cpath ) } -rgb_color_t highlight_get_rgb_color( int highlight, bool is_background ) +rgb_color_t highlight_get_color( int highlight, bool is_background ) { size_t i; int idx=0; @@ -210,61 +210,11 @@ rgb_color_t highlight_get_rgb_color( int highlight, bool is_background ) if( result2.is_underline() ) result.set_underline(true); } - } return result; } -int highlight_get_color( int highlight, bool is_background ) -{ - size_t i; - int idx=0; - int result = 0; - - if( highlight < 0 ) - return FISH_COLOR_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 = output_color_code( val_wstr, is_background ); - - if( highlight & HIGHLIGHT_VALID_PATH ) - { - env_var_t val2_wstr = env_get_string( L"fish_color_valid_path" ); - const wchar_t *val2 = val2_wstr.missing() ? NULL : val2_wstr.c_str(); - - int result2 = output_color_code( val2, is_background ); - if( result == FISH_COLOR_NORMAL ) - result = result2; - else - { - if( result2 & FISH_COLOR_BOLD ) - result |= FISH_COLOR_BOLD; - if( result2 & FISH_COLOR_UNDERLINE ) - result |= FISH_COLOR_UNDERLINE; - } - - } - return result; -} - /** Highligt operators (such as $, ~, %, as well as escaped characters. */ diff --git a/highlight.h b/highlight.h index 6f77ea5bd..fe56df5a3 100644 --- a/highlight.h +++ b/highlight.h @@ -104,7 +104,6 @@ void highlight_universal( const wchar_t *buff, int *color, int pos, wcstring_lis call to highlight_get_color( HIGHLIGHT_ERROR) will return FISH_COLOR_RED. */ -int highlight_get_color( int highlight, bool is_background ); -rgb_color_t highlight_get_rgb_color( int highlight, bool is_background ); +rgb_color_t highlight_get_color( int highlight, bool is_background ); #endif diff --git a/output.cpp b/output.cpp index 12ecd6634..7fcf4c925 100644 --- a/output.cpp +++ b/output.cpp @@ -796,11 +796,11 @@ int output_color_code( const wcstring &val, bool is_background ) { } rgb_color_t parse_color( const wcstring &val, bool is_background ) { - rgb_color_t result; - int is_bold=0; int is_underline=0; + std::vector candidates; + wcstring_list_t el; tokenize_variable_array( val, el ); @@ -823,18 +823,42 @@ rgb_color_t parse_color( const wcstring &val, bool is_background ) { } if (! color_name.empty()) { - result = rgb_color_t(color_name); - if (result.is_none()) { - result = rgb_color_t::normal(); + rgb_color_t color = rgb_color_t(color_name); + if (! color.is_none()) { + candidates.push_back(color); } } } + + // Pick the best candidate + rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none(); + for (size_t i=0; i < candidates.size(); i++) { + const rgb_color_t &color = candidates.at(i); + if (color.is_rgb() && first_rgb.is_none()) + first_rgb = color; + if (color.is_named() && first_named.is_none()) + first_named = color; + } + + // If we have both RGB and named colors, then prefer rgb if term256 is supported + rgb_color_t result; + if ((!first_rgb.is_none() && allow_term256()) || first_named.is_none()) { + result = first_rgb; + } else { + result = first_named; + } + + if (result.is_none()) + result = rgb_color_t::normal(); + + result.set_bold(is_bold); + result.set_underline(is_underline); + #if 0 wcstring desc = result.description(); printf("Parsed %ls from %ls (%s)\n", desc.c_str(), val.c_str(), is_background ? "background" : "foreground"); #endif - if (result.is_none()) - result = rgb_color_t::normal(); + return result; } diff --git a/output.h b/output.h index c277b322c..e456e3463 100644 --- a/output.h +++ b/output.h @@ -164,4 +164,7 @@ void output_set_term( const wchar_t *term ); */ const wchar_t *output_get_term(); +/** Determines whether term256 colors are supported */ +bool allow_term256(void); + #endif diff --git a/screen.cpp b/screen.cpp index 388d474b1..e74d4e8be 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_rgb_color( uc & 0xffff, false ), - highlight_get_rgb_color( (uc>>16)&0xffff, true ) ); + set_color( highlight_get_color( uc & 0xffff, false ), + highlight_get_color( (uc>>16)&0xffff, true ) ); output_set_writer( writer_old );