Add support for bold highligting

darcs-hash:20060527133930-ac50b-515d0887567c0e89fdfe10fe8310d6ebb4485974.gz
This commit is contained in:
axel 2006-05-27 23:39:30 +10:00
parent 6bdb0cde8b
commit bffff77d17
5 changed files with 80 additions and 36 deletions

View file

@ -20,17 +20,6 @@
*/ */
#define MAX_UTF8_BYTES 6 #define MAX_UTF8_BYTES 6
/**
Color code for set_color. Does not update the color.
*/
#define FISH_COLOR_IGNORE -1
/**
Color code for set_color. Sets the default color.
*/
#define FISH_COLOR_RESET -2
/** /**
This is in the unicode private use area. This is in the unicode private use area.
*/ */

View file

@ -68,11 +68,11 @@ end
set_default fish_color_normal normal set_default fish_color_normal normal
set_default fish_color_command green set_default fish_color_command green
set_default fish_color_redirection normal set_default fish_color_redirection normal
set_default fish_color_comment brown set_default fish_color_comment red
set_default fish_color_error red set_default fish_color_error red --bold
set_default fish_color_escape cyan set_default fish_color_escape cyan
set_default fish_color_operator cyan set_default fish_color_operator cyan
set_default fish_color_quote blue set_default fish_color_quote brown
set_default fish_color_cwd green set_default fish_color_cwd green

View file

@ -128,9 +128,20 @@ void set_color( int c, int c2 )
{ {
static int last_color = FISH_COLOR_NORMAL; static int last_color = FISH_COLOR_NORMAL;
static int last_color2 = FISH_COLOR_NORMAL; static int last_color2 = FISH_COLOR_NORMAL;
static int was_bold=0;
int bg_set=0, last_bg_set=0; int bg_set=0, last_bg_set=0;
char *fg = 0, *bg=0; char *fg = 0, *bg=0;
int is_bold = 0;
is_bold |= (c&FISH_COLOR_BOLD)!=0;
is_bold |= (c2&FISH_COLOR_BOLD)!=0;
// debug( 1, L"WOO %d %d %d", is_bold, c&FISH_COLOR_BOLD,c2&FISH_COLOR_BOLD);
c = c&(~FISH_COLOR_BOLD);
c2 = c2&(~FISH_COLOR_BOLD);
if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) ) if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) )
{ {
fg = set_a_foreground; fg = set_a_foreground;
@ -145,14 +156,31 @@ void set_color( int c, int c2 )
if( (c == FISH_COLOR_RESET) || (c2 == FISH_COLOR_RESET)) if( (c == FISH_COLOR_RESET) || (c2 == FISH_COLOR_RESET))
{ {
c = c2 = FISH_COLOR_NORMAL; c = c2 = FISH_COLOR_NORMAL;
was_bold=0;
if( fg ) 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( tparm( fg, 0 ) );
} }
writembs( exit_attribute_mode ); writembs( exit_attribute_mode );
return; return;
} }
if( was_bold && !is_bold )
{
/*
Only way to exit bold mode is a reset of all attributes.
*/
writembs( exit_attribute_mode );
last_color = FISH_COLOR_NORMAL;
last_color2 = FISH_COLOR_NORMAL;
was_bold=0;
}
if( last_color2 != FISH_COLOR_NORMAL && if( last_color2 != FISH_COLOR_NORMAL &&
last_color2 != FISH_COLOR_RESET && last_color2 != FISH_COLOR_RESET &&
last_color2 != FISH_COLOR_IGNORE ) last_color2 != FISH_COLOR_IGNORE )
@ -164,7 +192,6 @@ void set_color( int c, int c2 )
} }
if( c2 != FISH_COLOR_NORMAL && if( c2 != FISH_COLOR_NORMAL &&
c2 != FISH_COLOR_RESET &&
c2 != FISH_COLOR_IGNORE ) c2 != FISH_COLOR_IGNORE )
{ {
/* /*
@ -180,7 +207,8 @@ void set_color( int c, int c2 )
{ {
/* /*
Background color changed and is set, so we enter bold Background color changed and is set, so we enter bold
mode to make reading easier mode to make reading easier. This means bold mode is
_always_ on when the background color is set.
*/ */
writembs( enter_bold_mode ); writembs( enter_bold_mode );
} }
@ -252,8 +280,21 @@ void set_color( int c, int c2 )
last_color2 = c2; last_color2 = c2;
} }
} }
/*
Lastly, we set bold mode correctly
*/
if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set )
{
if( is_bold && !was_bold )
{
writembs( tparm( enter_bold_mode ) );
}
was_bold = is_bold;
}
} }
/** /**
perm_left_cursor and parm_right_cursor don't seem to be properly perm_left_cursor and parm_right_cursor don't seem to be properly
emulated by many terminal emulators, so we only use plain emulated by many terminal emulators, so we only use plain
@ -475,23 +516,34 @@ int writespace( int c )
int output_color_code( const wchar_t *val ) int output_color_code( const wchar_t *val )
{ {
int i, color=-1; int j, i, color=FISH_COLOR_NORMAL;
array_list_t el;
int is_bold=0;
if( !val )
return FISH_COLOR_NORMAL;
al_init( &el );
expand_variable_array( val, &el );
for( j=0; j<al_get_count( &el ); j++ )
{
wchar_t *next = (wchar_t *)al_get( &el, j );
is_bold |= (wcsncmp( next, L"--bold", wcslen(next) ) == 0 ) && wcslen(next)>=3;
is_bold |= wcscmp( next, L"-b" ) == 0;
for( i=0; i<COLORS; i++ ) for( i=0; i<COLORS; i++ )
{ {
if( wcscasecmp( col[i], val ) == 0 ) if( wcscasecmp( col[i], next ) == 0 )
{ {
color = col_idx[i]; color = col_idx[i];
break; break;
} }
} }
if( color >= 0 )
{
return color;
}
else
{
return FISH_COLOR_NORMAL;
} }
return color | (is_bold?FISH_COLOR_BOLD:0);
} }

View file

@ -41,10 +41,13 @@ enum
FISH_COLOR_CYAN, FISH_COLOR_CYAN,
FISH_COLOR_WHITE, FISH_COLOR_WHITE,
/** The default fg color of the terminal */ /** The default fg color of the terminal */
FISH_COLOR_NORMAL FISH_COLOR_NORMAL,
FISH_COLOR_IGNORE,
FISH_COLOR_RESET
} }
; ;
#define FISH_COLOR_BOLD 0x80
/** /**
Sets the fg and bg color. May be called as often as you like, since Sets the fg and bg color. May be called as often as you like, since

View file

@ -917,7 +917,7 @@ void repaint()
if( steps ) if( steps )
move_cursor( -steps ); move_cursor( -steps );
set_color( FISH_COLOR_NORMAL, -1 ); set_color( FISH_COLOR_NORMAL, FISH_COLOR_IGNORE );
reader_save_status(); reader_save_status();
} }
@ -1144,7 +1144,7 @@ static int insert_char( int c )
} }
else else
writech(c); writech(c);
set_color( FISH_COLOR_NORMAL, -1 ); set_color( FISH_COLOR_NORMAL, FISH_COLOR_IGNORE );
} }
else else
{ {