From f7b2bf8229294e52a4f7276eb9a0e75321e5d812 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 11 Jan 2021 20:53:11 +0100 Subject: [PATCH] output: Simplify some duplicated code Becomes a bit boring after a while --- src/output.cpp | 95 +++++++++----------------------------------------- src/output.h | 8 +++++ 2 files changed, 25 insertions(+), 78 deletions(-) diff --git a/src/output.cpp b/src/output.cpp index 5eb76c199..05b3bd683 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -147,44 +147,21 @@ bool outputter_t::write_color(rgb_color_t color, bool is_fg) { /// \param c Foreground color. /// \param c2 Background color. void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { - if (!cur_term) return; + // Test if we have at least basic support for setting fonts, colors and related bits - otherwise + // just give up... + if (!cur_term || !exit_attribute_mode) return; const rgb_color_t normal = rgb_color_t::normal(); bool bg_set = false, last_bg_set = false; - bool is_bold = false; - bool is_underline = false; - bool is_italics = false; - bool is_dim = false; - bool is_reverse = false; - - // Test if we have at least basic support for setting fonts, colors and related bits - otherwise - // just give up... - if (!exit_attribute_mode) { - return; - } - - is_bold |= c.is_bold(); - is_bold |= c2.is_bold(); - - is_underline |= c.is_underline(); - is_underline |= c2.is_underline(); - - is_italics |= c.is_italics(); - is_italics |= c2.is_italics(); - - is_dim |= c.is_dim(); - is_dim |= c2.is_dim(); - - is_reverse |= c.is_reverse(); - is_reverse |= c2.is_reverse(); + bool is_bold = c.is_bold() || c2.is_bold(); + bool is_underline = c.is_underline() || c2.is_underline(); + bool is_italics = c.is_italics() || c2.is_italics(); + bool is_dim = c.is_dim() || c2.is_dim(); + bool is_reverse = c.is_reverse() || c2.is_reverse(); if (c.is_reset() || c2.is_reset()) { c = c2 = normal; - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; + reset_modes(); // If we exit attibute mode, we must first set a color, or previously colored text might // lose it's color. Terminals are weird... write_foreground_color(*this, 0); @@ -192,40 +169,14 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { return; } - if (was_bold && !is_bold) { - // Only way to exit bold mode is a reset of all attributes. + if ((was_bold && !is_bold) + || (was_dim && !is_dim) + || (was_reverse && !is_reverse)) { + // Only way to exit bold/dim/reverse mode is a reset of all attributes. writembs(*this, exit_attribute_mode); last_color = normal; last_color2 = normal; - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; - } - - if (was_dim && !is_dim) { - // Only way to exit dim mode is a reset of all attributes. - writembs(*this, exit_attribute_mode); - last_color = normal; - last_color2 = normal; - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; - } - - if (was_reverse && !is_reverse) { - // Only way to exit reverse mode is a reset of all attributes. - writembs(*this, exit_attribute_mode); - last_color = normal; - last_color2 = normal; - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; + reset_modes(); } if (!last_color2.is_normal() && !last_color2.is_reset()) { @@ -248,11 +199,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { if (!bg_set && last_bg_set) { // Background color changed and is no longer set, so we exit bold mode. writembs(*this, exit_attribute_mode); - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; + reset_modes(); // We don't know if exit_attribute_mode resets colors, so we set it to something known. if (write_foreground_color(*this, 0)) { last_color = rgb_color_t::black(); @@ -266,11 +213,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { writembs(*this, exit_attribute_mode); last_color2 = rgb_color_t::normal(); - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; + reset_modes(); } else if (!c.is_special()) { write_color(c, true /* foreground */); } @@ -287,11 +230,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { write_color(last_color, true /* foreground */); } - was_bold = false; - was_underline = false; - was_italics = false; - was_dim = false; - was_reverse = false; + reset_modes(); last_color2 = c2; } else if (!c2.is_special()) { write_color(c2, false /* not foreground */); diff --git a/src/output.h b/src/output.h index 326364d9c..e3d2d1c11 100644 --- a/src/output.h +++ b/src/output.h @@ -32,6 +32,14 @@ class outputter_t { bool was_dim = false; bool was_reverse = false; + void reset_modes() { + was_bold = false; + was_underline = false; + was_italics = false; + was_dim = false; + was_reverse = false; + } + /// Construct an outputter which outputs to a given fd. explicit outputter_t(int fd) : fd_(fd) {}