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; 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> template<typename T>
wcstring to_string(const T &x) { wcstring to_string(const T &x) {
std::wstringstream stream; std::wstringstream stream;
@ -318,6 +326,16 @@ inline wcstring to_string(const long &x) {
return wcstring(buff); 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<> template<>
inline wcstring to_string(const int &x) { inline wcstring to_string(const int &x) {
return to_string(static_cast<long>(x)); return to_string(static_cast<long>(x));

View file

@ -1089,6 +1089,15 @@ static void init( int mangle_descriptors, int out )
free( wterm ); 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

@ -325,9 +325,18 @@ int input_init()
input_terminfo_init(); input_terminfo_init();
/* /* Infer term256 support. Consider using t_Co */
If we have no keybindings, add a few simple defaults 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( mapping_list.size() ) if( mapping_list.size() )
{ {
input_mapping_add( L"", L"self-insert" ); input_mapping_add( L"", L"self-insert" );

View file

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

View file

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