Support 256color (and assume 256color terminal). Closes #3 github issue.

This commit is contained in:
Keith Winstein
2012-03-16 04:40:18 -04:00
parent bdcdad7081
commit 57d97209c0
5 changed files with 63 additions and 10 deletions
+1 -1
View File
@@ -95,7 +95,7 @@ int main( void )
if ( child == 0 ) {
/* child */
if ( setenv( "TERM", "xterm", true ) < 0 ) {
if ( setenv( "TERM", "xterm-256color", true ) < 0 ) {
perror( "setenv" );
exit( 1 );
}
+1 -1
View File
@@ -182,7 +182,7 @@ int run_server( const char *desired_ip, const char *desired_port ) {
fatal_assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
/* set TERM */
if ( setenv( "TERM", "xterm", true ) < 0 ) {
if ( setenv( "TERM", "xterm-256color", true ) < 0 ) {
perror( "setenv" );
exit( 1 );
}
+38 -4
View File
@@ -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;
+5 -1
View File
@@ -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 );
+15
View File
@@ -332,11 +332,26 @@ static Function func_Ctrl_BEL( CONTROL, "\x07", Ctrl_BEL );
/* select graphics rendition -- e.g., bold, blinking, etc. */
void CSI_SGR( Framebuffer *fb, Dispatcher *dispatch )
{
/* 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 );
}
}
}
static Function func_CSI_SGR( CSI, "m", CSI_SGR, false ); /* changing renditions doesn't clear wrap flag */