Additional work towards "forcing" term256 mode on supported terms

This commit is contained in:
ridiculousfish 2012-03-05 13:39:01 -08:00
parent 063a465227
commit 0e5578204e
2 changed files with 196 additions and 177 deletions

View file

@ -328,12 +328,12 @@ inline wcstring to_string(const long &x) {
template<>
inline bool from_string(const std::string &x) {
return ! x.empty() && strchr("YTyt", x.at(0));
return ! x.empty() && strchr("YTyt1", x.at(0));
}
template<>
inline bool from_string(const wcstring &x) {
return ! x.empty() && wcschr(L"YTyt", x.at(0));
return ! x.empty() && wcschr(L"YTyt1", x.at(0));
}
template<>

View file

@ -1,6 +1,6 @@
/** \file output.c
Generic output functions
*/
*/
#include "config.h"
@ -59,14 +59,14 @@
/**
Number of color names in the col array
*/
*/
#define COLORS (sizeof(col)/sizeof(wchar_t *))
static int writeb_internal( char c );
/**
Names of different colors.
*/
*/
static const wchar_t *col[]=
{
L"black",
@ -81,14 +81,14 @@ static const wchar_t *col[]=
L"white"
L"normal"
}
;
;
/**
Mapping from color name (the 'col' array) to color index as used in
ANSI color terminals, and also the fish_color_* constants defined
in highlight.h. Non-ANSI terminals will display the wrong colors,
since they use a different mapping.
*/
*/
static const int col_idx[]=
{
0,
@ -106,7 +106,7 @@ static const int col_idx[]=
/**
The function used for output
*/
*/
static int (*out)(char c) = &writeb_internal;
@ -130,6 +130,11 @@ int (*output_get_writer())(char)
return out;
}
static bool term256_support_is_native(void) {
/* Return YES if we think the term256 support is "native" as opposed to forced. */
return max_colors == 256;
}
bool output_get_supports_term256() {
return support_term256;
}
@ -146,6 +151,48 @@ static unsigned char index_for_color(rgb_color_t c) {
}
}
static bool write_color(char *todo, unsigned char idx, bool is_fg) {
bool result = false;
if (idx < 16 || term256_support_is_native()) {
/* Use tparm */
writembs( tparm( todo, idx ) );
result = true;
} else {
/* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
char stridx[128];
format_long_safe(stridx, (long)idx);
char buff[128] = "\x1b[";
strcat(buff, is_fg ? "38;5;" : "48;5;");
strcat(buff, stridx);
strcat(buff, "m");
write_loop(STDOUT_FILENO, buff, strlen(buff));
result = true;
}
return result;
}
static bool write_foreground_color(unsigned char idx) {
if (set_a_foreground && set_a_foreground[0]) {
return write_color(set_a_foreground, idx, true);
} else if (set_foreground && set_foreground[0]) {
return write_color(set_foreground, idx, true);
} else {
return false;
}
}
static bool write_background_color(unsigned char idx) {
if (set_a_background && set_a_background[0]) {
return write_color(set_a_background, idx, false);
} else if (set_background && set_background[0]) {
return write_color(set_background, idx, false);
} else {
return false;
}
}
void set_color(rgb_color_t c, rgb_color_t c2)
{
@ -162,7 +209,6 @@ void set_color(rgb_color_t c, rgb_color_t c2)
static int was_bold=0;
static int was_underline=0;
int bg_set=0, last_bg_set=0;
char *fg = 0, *bg=0;
int is_bold = 0;
int is_underline = 0;
@ -183,31 +229,17 @@ void set_color(rgb_color_t c, rgb_color_t c2)
is_underline |= c.is_underline();
is_underline |= c2.is_underline();
if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) )
{
fg = set_a_foreground;
bg = set_a_background;
}
else if( (set_foreground != 0) && (strlen( set_foreground) != 0 ) )
{
fg = set_foreground;
bg = set_background;
}
if( c.is_reset() || c2.is_reset())
{
c = c2 = normal;
was_bold=0;
was_underline=0;
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 ) );
}
write_foreground_color(0);
writembs( exit_attribute_mode );
return;
}
@ -268,9 +300,8 @@ void set_color(rgb_color_t c, rgb_color_t c2)
We don't know if exit_attribute_mode resets colors, so
we set it to something known.
*/
if( fg )
if( write_foreground_color(0))
{
writembs( tparm( fg, 0 ) );
last_color=rgb_color_t::black();
}
}
@ -280,10 +311,7 @@ void set_color(rgb_color_t c, rgb_color_t c2)
{
if( c.is_normal() )
{
if( fg )
{
writembs( tparm( fg, 0 ) );
}
write_foreground_color(0);
writembs( exit_attribute_mode );
last_color2 = rgb_color_t::normal();
@ -292,10 +320,7 @@ void set_color(rgb_color_t c, rgb_color_t c2)
}
else if( ! c.is_special() )
{
if( fg )
{
writembs( tparm(fg, index_for_color(c)) );
}
write_foreground_color(index_for_color(c));
}
}
@ -305,15 +330,12 @@ void set_color(rgb_color_t c, rgb_color_t c2)
{
if( c2.is_normal() )
{
if( bg )
{
writembs( tparm( bg, 0 ) );
}
write_background_color(0);
writembs( exit_attribute_mode );
if( ! last_color.is_normal() && fg )
if( ! last_color.is_normal())
{
writembs( tparm( fg, index_for_color(last_color) ));
write_foreground_color(index_for_color(last_color));
}
@ -323,10 +345,7 @@ void set_color(rgb_color_t c, rgb_color_t c2)
}
else if ( ! c2.is_special() )
{
if( bg )
{
writembs( tparm( bg, index_for_color(c2) ));
}
write_background_color(index_for_color(c2));
last_color2 = c2;
}
}
@ -573,7 +592,7 @@ void set_color( int c, int c2 )
/**
Default output method, simply calls write() on stdout
*/
*/
static int writeb_internal( char c )
{
write_loop( 1, &c, 1 );
@ -633,8 +652,8 @@ void writestr( const wchar_t *str )
CHECK( str, );
// while( *str )
// writech( *str++ );
// while( *str )
// writech( *str++ );
/*
Check amount of needed space