diff --git a/common.h b/common.h index 7eaed8665..8bb5b8bdb 100644 --- a/common.h +++ b/common.h @@ -303,6 +303,14 @@ T from_string(const wcstring &x) { return result; } +template +T from_string(const std::string &x) { + T result = T(); + std::stringstream stream(x); + stream >> result; + return result; +} + template 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(x)); diff --git a/fish_pager.cpp b/fish_pager.cpp index 8c81e1380..c351a88b1 100644 --- a/fish_pager.cpp +++ b/fish_pager.cpp @@ -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(fish_term256); + } else { + support_term256 = term && strstr(term, "256color"); + } + output_set_supports_term256(support_term256); } /** diff --git a/input.cpp b/input.cpp index 14e434dc6..26138cfe0 100644 --- a/input.cpp +++ b/input.cpp @@ -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(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" ); diff --git a/output.cpp b/output.cpp index 7fcf4c925..99de8cb68 100644 --- a/output.cpp +++ b/output.cpp @@ -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; diff --git a/output.h b/output.h index 2b7d9d318..30fb23251 100644 --- a/output.h +++ b/output.h @@ -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