Support for setting both RGB and named colors on the same line, so the same config can work for multiple term types

This commit is contained in:
ridiculousfish 2012-02-13 09:52:17 -08:00
parent 0a4c72e78b
commit dce189fc6d
5 changed files with 38 additions and 62 deletions

View file

@ -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; size_t i;
int idx=0; int idx=0;
@ -210,61 +210,11 @@ rgb_color_t highlight_get_rgb_color( int highlight, bool is_background )
if( result2.is_underline() ) if( result2.is_underline() )
result.set_underline(true); result.set_underline(true);
} }
} }
return result; 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<<VAR_COUNT) )
return FISH_COLOR_NORMAL;
for( i=0; i<VAR_COUNT; i++ )
{
if( highlight & (1<<i ))
{
idx = i;
break;
}
}
env_var_t val_wstr = env_get_string( highlight_var[idx]);
// debug( 1, L"%d -> %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. Highligt operators (such as $, ~, %, as well as escaped characters.
*/ */

View file

@ -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 call to highlight_get_color( HIGHLIGHT_ERROR) will return
FISH_COLOR_RED. FISH_COLOR_RED.
*/ */
int highlight_get_color( int highlight, bool is_background ); rgb_color_t highlight_get_color( int highlight, bool is_background );
rgb_color_t highlight_get_rgb_color( int highlight, bool is_background );
#endif #endif

View file

@ -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 parse_color( const wcstring &val, bool is_background ) {
rgb_color_t result;
int is_bold=0; int is_bold=0;
int is_underline=0; int is_underline=0;
std::vector<rgb_color_t> candidates;
wcstring_list_t el; wcstring_list_t el;
tokenize_variable_array( val, 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()) { if (! color_name.empty()) {
result = rgb_color_t(color_name); rgb_color_t color = rgb_color_t(color_name);
if (result.is_none()) { if (! color.is_none()) {
result = rgb_color_t::normal(); 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 #if 0
wcstring desc = result.description(); wcstring desc = result.description();
printf("Parsed %ls from %ls (%s)\n", desc.c_str(), val.c_str(), is_background ? "background" : "foreground"); printf("Parsed %ls from %ls (%s)\n", desc.c_str(), val.c_str(), is_background ? "background" : "foreground");
#endif #endif
if (result.is_none())
result = rgb_color_t::normal();
return result; return result;
} }

View file

@ -164,4 +164,7 @@ void output_set_term( const wchar_t *term );
*/ */
const wchar_t *output_get_term(); const wchar_t *output_get_term();
/** Determines whether term256 colors are supported */
bool allow_term256(void);
#endif #endif

View file

@ -530,8 +530,8 @@ static void s_set_color( screen_t *s, buffer_t *b, int c )
s_writeb_buffer = b; s_writeb_buffer = b;
unsigned int uc = (unsigned int)c; unsigned int uc = (unsigned int)c;
set_color( highlight_get_rgb_color( uc & 0xffff, false ), set_color( highlight_get_color( uc & 0xffff, false ),
highlight_get_rgb_color( (uc>>16)&0xffff, true ) ); highlight_get_color( (uc>>16)&0xffff, true ) );
output_set_writer( writer_old ); output_set_writer( writer_old );