mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-25 20:33:08 +00:00
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:
parent
0a4c72e78b
commit
dce189fc6d
5 changed files with 38 additions and 62 deletions
|
@ -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<<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.
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
|
|
38
output.cpp
38
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<rgb_color_t> 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;
|
||||
}
|
||||
|
||||
|
|
3
output.h
3
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
|
||||
|
|
|
@ -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 );
|
||||
|
||||
|
|
Loading…
Reference in a new issue