From 5afd939f3e7558c91404e8c563c53351b71cfb43 Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Sat, 23 Jul 2016 13:40:01 -0700 Subject: [PATCH] Stop swallowing the cartesian product This should work: > env TERM=vt100 ./fish -c 'echo (set_color red)"hi"' We do a ::reset() if setting the color doesn't happen. Fixes #2951 --- src/builtin_set_color.cpp | 7 ++++++- src/output.cpp | 5 +++-- src/output.h | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/builtin_set_color.cpp b/src/builtin_set_color.cpp index e6679562f..35917a63d 100644 --- a/src/builtin_set_color.cpp +++ b/src/builtin_set_color.cpp @@ -179,7 +179,12 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) { write_color(rgb_color_t::black(), true /* is_fg */); writembs(tparm(exit_attribute_mode)); } else { - write_color(fg, true /* is_fg */); + if (!write_color(fg, true /* is_fg */)) { + // We need to do *something* or the lack of any output messes up + // when the cartesian product here would make "foo" disappear: + // $ echo (set_color foo)bar + set_color(rgb_color_t::reset(), rgb_color_t::none()); + } } } diff --git a/src/output.cpp b/src/output.cpp index 937e78114..4905c5a50 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -106,12 +106,12 @@ static bool write_background_color(unsigned char idx) { } // Exported for builtin_set_color's usage only. -void write_color(rgb_color_t color, bool is_fg) { +bool write_color(rgb_color_t color, bool is_fg) { bool supports_term24bit = !!(output_get_color_support() & color_support_term24bit); if (!supports_term24bit || !color.is_rgb()) { // Indexed or non-24 bit color. unsigned char idx = index_for_color(color); - (is_fg ? write_foreground_color : write_background_color)(idx); + return (is_fg ? write_foreground_color : write_background_color)(idx); } else { // 24 bit! No tparm here, just ANSI escape sequences. // Foreground: ^[38;2;;;m @@ -127,6 +127,7 @@ void write_color(rgb_color_t color, bool is_fg) { } } } + return true; } /// Sets the fg and bg color. May be called as often as you like, since if the new color is the same diff --git a/src/output.h b/src/output.h index 71f94de80..6ca31d92c 100644 --- a/src/output.h +++ b/src/output.h @@ -51,7 +51,7 @@ void output_set_color_support(color_support_t support); rgb_color_t best_color(const std::vector &colors, color_support_t support); -void write_color(rgb_color_t color, bool is_fg); +bool write_color(rgb_color_t color, bool is_fg); unsigned char index_for_color(rgb_color_t c); #endif