Begin to rework term256 support

This commit is contained in:
ridiculousfish 2012-03-05 10:44:08 -08:00
parent 230fb921ec
commit 063a465227
5 changed files with 56 additions and 19 deletions

View File

@ -303,6 +303,14 @@ T from_string(const wcstring &x) {
return result;
}
template<typename T>
T from_string(const std::string &x) {
T result = T();
std::stringstream stream(x);
stream >> result;
return result;
}
template<typename T>
wcstring to_string(const T &x) {
std::wstringstream stream;
@ -318,6 +326,16 @@ inline wcstring to_string(const long &x) {
return wcstring(buff);
}
template<>
inline bool from_string(const std::string &x) {
return ! x.empty() && strchr("YTyt", x.at(0));
}
template<>
inline bool from_string(const wcstring &x) {
return ! x.empty() && wcschr(L"YTyt", x.at(0));
}
template<>
inline wcstring to_string(const int &x) {
return to_string(static_cast<long>(x));

View File

@ -1089,6 +1089,15 @@ static void init( int mangle_descriptors, int out )
free( wterm );
}
/* Infer term256 support */
char *fish_term256 = getenv("fish_term256");
bool support_term256;
if (fish_term256) {
support_term256 = from_string<bool>(fish_term256);
} else {
support_term256 = term && strstr(term, "256color");
}
output_set_supports_term256(support_term256);
}
/**

View File

@ -324,10 +324,19 @@ int input_init()
output_set_term( term.c_str() );
input_terminfo_init();
/* Infer term256 support. Consider using t_Co */
env_var_t fish_term256 = env_get_string(L"fish_term256");
bool support_term256;
if (! fish_term256.missing_or_empty()) {
support_term256 = from_string<bool>(fish_term256);
} else {
env_var_t term = env_get_string(L"TERM");
support_term256 = ! term.missing() && term.find(L"256color") != wcstring::npos;
}
output_set_supports_term256(support_term256);
/*
If we have no keybindings, add a few simple defaults
*/
/* If we have no keybindings, add a few simple defaults */
if( mapping_list.size() )
{
input_mapping_add( L"", L"self-insert" );

View File

@ -115,6 +115,9 @@ static int (*out)(char c) = &writeb_internal;
*/
static wcstring current_term;
/* Whether term256 is supported */
static bool support_term256 = false;
void output_set_writer( int (*writer)(char) )
{
@ -127,16 +130,16 @@ int (*output_get_writer())(char)
return out;
}
bool allow_term256(void)
{
//consider using t_Co
ASSERT_IS_MAIN_THREAD();
const wchar_t *t = output_get_term();
return t && wcsstr(t, L"256color");
bool output_get_supports_term256() {
return support_term256;
}
void output_set_supports_term256(bool val) {
support_term256 = val;
}
static unsigned char index_for_color(rgb_color_t c) {
if (c.is_named() || ! allow_term256()) {
if (c.is_named() || ! output_get_supports_term256()) {
return c.to_name_index();
} else {
return c.to_term256_index();
@ -842,7 +845,7 @@ rgb_color_t parse_color( const wcstring &val, bool is_background ) {
// If we have both RGB and named colors, then prefer rgb if term256 is supported
rgb_color_t result;
if ((!first_rgb.is_none() && allow_term256()) || first_named.is_none()) {
if ((!first_rgb.is_none() && output_get_supports_term256()) || first_named.is_none()) {
result = first_rgb;
} else {
result = first_named;

View File

@ -154,16 +154,14 @@ void output_set_writer( int (*writer)(char) );
*/
int (*output_get_writer())(char) ;
/**
Set the terminal name
*/
/** Set the terminal name */
void output_set_term( const wchar_t *term );
/**
Return the terminal name
*/
/** Return the terminal name */
const wchar_t *output_get_term();
/** Determines whether term256 colors are supported */
bool allow_term256(void);
/** Sets whether term256 colors are supported */
bool output_get_supports_term256();
void output_set_supports_term256(bool val);
#endif