Make Renditions::sgr() more compact in both code and output.

This commit is contained in:
John Hood
2017-11-06 10:14:25 -05:00
parent ce7ba37ad4
commit 88bb01a50a
+14 -28
View File
@@ -522,6 +522,7 @@ void Renditions::set_background_color( int num )
std::string Renditions::sgr( void ) const std::string Renditions::sgr( void ) const
{ {
std::string ret; std::string ret;
char col[64];
ret.append( "\033[0" ); ret.append( "\033[0" );
if ( get_attribute( bold ) ) ret.append( ";1" ); if ( get_attribute( bold ) ) ret.append( ";1" );
@@ -531,48 +532,33 @@ std::string Renditions::sgr( void ) const
if ( get_attribute( inverse ) ) ret.append( ";7" ); if ( get_attribute( inverse ) ) ret.append( ";7" );
if ( get_attribute( invisible ) ) ret.append( ";8" ); if ( get_attribute( invisible ) ) ret.append( ";8" );
if ( foreground_color if ( foreground_color ) {
&& (foreground_color <= 37) ) {
/* ANSI foreground color */
char col[ 8 ];
snprintf( col, 8, ";%u", static_cast<unsigned int>( foreground_color ) );
ret.append( col );
}
if ( background_color
&& (background_color <= 47) ) {
char col[ 8 ];
snprintf( col, 8, ";%u", static_cast<unsigned int>( background_color ) );
ret.append( col );
}
ret.append( "m" );
if ( is_true_color( foreground_color ) ) { if ( is_true_color( foreground_color ) ) {
char col[64]; snprintf( col, sizeof( col ), ";38;2;%u;%u;%u",
snprintf( col, 64, "\033[38;2;%u;%u;%um",
(foreground_color >> 16) & 0xff, (foreground_color >> 16) & 0xff,
(foreground_color >> 8) & 0xff, (foreground_color >> 8) & 0xff,
foreground_color & 0xff); foreground_color & 0xff);
ret.append( col );
} else if ( foreground_color > 37 ) { /* use 256-color set */ } else if ( foreground_color > 37 ) { /* use 256-color set */
char col[ 64 ]; snprintf( col, sizeof( col ), ";38;5;%u", foreground_color - 30 );
snprintf( col, 64, "\033[38;5;%um", foreground_color - 30 ); } else { /* ANSI foreground color */
snprintf( col, sizeof( col ), ";%u", static_cast<unsigned int>( foreground_color ) );
}
ret.append( col ); ret.append( col );
} }
if ( background_color ) {
if ( is_true_color( background_color ) ) { if ( is_true_color( background_color ) ) {
char col[64]; snprintf( col, sizeof( col ), ";48;2;%u;%u;%u",
snprintf( col, 64, "\033[48;2;%u;%u;%um",
(background_color >> 16) & 0xff, (background_color >> 16) & 0xff,
(background_color >> 8) & 0xff, (background_color >> 8) & 0xff,
background_color & 0xff); background_color & 0xff);
ret.append( col );
} else if ( background_color > 47 ) { /* use 256-color set */ } else if ( background_color > 47 ) { /* use 256-color set */
char col[ 64 ]; snprintf( col, sizeof( col ), ";48;5;%u", background_color - 40 );
snprintf( col, 64, "\033[48;5;%um", background_color - 40 ); } else { /* ANSI background color */
snprintf( col, sizeof( col ), ";%u", static_cast<unsigned int>( background_color ) );
}
ret.append( col ); ret.append( col );
} }
ret.append( "m" );
return ret; return ret;
} }