Support 256color (and assume 256color terminal). Closes #3 github issue.
This commit is contained in:
@@ -369,6 +369,7 @@ Renditions::Renditions( int s_background )
|
||||
background_color( s_background )
|
||||
{}
|
||||
|
||||
/* This routine cannot be used to set a color beyond the 8-color set. */
|
||||
void Renditions::set_rendition( int num )
|
||||
{
|
||||
if ( num == 0 ) {
|
||||
@@ -377,10 +378,18 @@ void Renditions::set_rendition( int num )
|
||||
return;
|
||||
}
|
||||
|
||||
if ( (30 <= num) && (num <= 39) ) { /* foreground color */
|
||||
if ( num == 39 ) {
|
||||
foreground_color = 0;
|
||||
return;
|
||||
} else if ( num == 49 ) {
|
||||
background_color = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( (30 <= num) && (num <= 37) ) { /* foreground color in 8-color set */
|
||||
foreground_color = num;
|
||||
return;
|
||||
} else if ( (40 <= num) && (num <= 49) ) { /* background color */
|
||||
} else if ( (40 <= num) && (num <= 47) ) { /* background color in 8-color set */
|
||||
background_color = num;
|
||||
return;
|
||||
}
|
||||
@@ -394,6 +403,20 @@ void Renditions::set_rendition( int num )
|
||||
}
|
||||
}
|
||||
|
||||
void Renditions::set_foreground_color( int num )
|
||||
{
|
||||
if ( (0 <= num) && (num <= 255) ) {
|
||||
foreground_color = 30 + num;
|
||||
}
|
||||
}
|
||||
|
||||
void Renditions::set_background_color( int num )
|
||||
{
|
||||
if ( (0 <= num) && (num <= 255) ) {
|
||||
background_color = 40 + num;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Renditions::sgr( void ) const
|
||||
{
|
||||
std::string ret;
|
||||
@@ -404,16 +427,27 @@ std::string Renditions::sgr( void ) const
|
||||
if ( blink ) ret.append( ";5" );
|
||||
if ( inverse ) ret.append( ";7" );
|
||||
if ( invisible ) ret.append( ";8" );
|
||||
if ( foreground_color ) {
|
||||
|
||||
if ( foreground_color > 37 ) { /* use 256-color set */
|
||||
char col[ 64 ];
|
||||
snprintf( col, 64, "m\033[38;5;%d", foreground_color - 30 );
|
||||
ret.append( col );
|
||||
} else if ( foreground_color ) {
|
||||
char col[ 8 ];
|
||||
snprintf( col, 8, ";%d", foreground_color );
|
||||
ret.append( col );
|
||||
}
|
||||
if ( background_color ) {
|
||||
|
||||
if ( background_color > 47 ) { /* use 256-color set */
|
||||
char col[ 64 ];
|
||||
snprintf( col, 64, "m\033[48;5;%d", background_color - 40 );
|
||||
ret.append( col );
|
||||
} else if ( background_color ) {
|
||||
char col[ 8 ];
|
||||
snprintf( col, 8, ";%d", background_color );
|
||||
ret.append( col );
|
||||
}
|
||||
|
||||
ret.append( "m" );
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -35,6 +35,8 @@ namespace Terminal {
|
||||
int background_color;
|
||||
|
||||
Renditions( int s_background );
|
||||
void set_foreground_color( int num );
|
||||
void set_background_color( int num );
|
||||
void set_rendition( int num );
|
||||
std::string sgr( void ) const;
|
||||
|
||||
@@ -182,9 +184,11 @@ namespace Terminal {
|
||||
int limit_top( void );
|
||||
int limit_bottom( void );
|
||||
|
||||
void set_foreground_color( int x ) { renditions.set_foreground_color( x ); }
|
||||
void set_background_color( int x ) { renditions.set_background_color( x ); }
|
||||
void add_rendition( int x ) { renditions.set_rendition( x ); }
|
||||
Renditions get_renditions( void ) const { return renditions; }
|
||||
int get_background_rendition( void ) { return renditions.background_color; }
|
||||
int get_background_rendition( void ) const { return renditions.background_color; }
|
||||
|
||||
void save_cursor( void );
|
||||
void restore_cursor( void );
|
||||
|
||||
@@ -332,9 +332,24 @@ static Function func_Ctrl_BEL( CONTROL, "\x07", Ctrl_BEL );
|
||||
/* select graphics rendition -- e.g., bold, blinking, etc. */
|
||||
void CSI_SGR( Framebuffer *fb, Dispatcher *dispatch )
|
||||
{
|
||||
for ( int i = 0; i < dispatch->param_count(); i++ ) {
|
||||
int rendition = dispatch->getparam( i, 0 );
|
||||
fb->ds.add_rendition( rendition );
|
||||
/* We need to special-case the handling of CSI [34]8 ; 5 ; Ps m,
|
||||
because Ps of 0 in that case does not mean reset to default, even
|
||||
though it means that otherwise (as usually renditions are applied
|
||||
in order). */
|
||||
|
||||
if ( (dispatch->param_count() == 3)
|
||||
&& (dispatch->getparam( 0, -1 ) == 38)
|
||||
&& (dispatch->getparam( 1, -1 ) == 5) ) {
|
||||
fb->ds.set_foreground_color( dispatch->getparam( 2, 0 ) );
|
||||
} else if ( (dispatch->param_count() == 3)
|
||||
&& (dispatch->getparam( 0, -1 ) == 48)
|
||||
&& (dispatch->getparam( 1, -1 ) == 5) ) {
|
||||
fb->ds.set_background_color( dispatch->getparam( 2, 0 ) );
|
||||
} else {
|
||||
for ( int i = 0; i < dispatch->param_count(); i++ ) {
|
||||
int rendition = dispatch->getparam( i, 0 );
|
||||
fb->ds.add_rendition( rendition );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user