Skip to content

Commit

Permalink
Transitions between faint and bold go through normal
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Chaplain authored and KazDragon committed Jan 2, 2024
1 parent 83ed059 commit 4c7b3a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion include/terminalpp/detail/element_difference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,26 @@ void change_effect(
bool &change_appended,
WriteContinuation &&wc)
{
static byte_storage const separator = {ansi::ps};

if (source != dest)
{
if constexpr (effect_has_normal<EffectType>::value)
{
if (source.value_ != EffectType::normal
&& dest.value_ != EffectType::normal)
{
if (std::exchange(change_appended, true))
{
wc(separator);
}

wc(to_bytes(fmt::format("{}", int(EffectType::normal))));
}
}

if (std::exchange(change_appended, true))
{
static byte_storage const separator = {ansi::ps};
wc(separator);
}

Expand Down
10 changes: 10 additions & 0 deletions include/terminalpp/effect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <boost/container_hash/hash.hpp>
#include <boost/operators.hpp>
#include <iostream>
#include <type_traits>

namespace terminalpp {

Expand Down Expand Up @@ -117,6 +118,15 @@ using underlining = effect<terminalpp::graphics::underlining>;
using polarity = effect<terminalpp::graphics::polarity>;
using blinking = effect<terminalpp::graphics::blinking>;

template <typename EffectType>
struct effect_has_normal : std::bool_constant<false>
{};

template <>
struct effect_has_normal<terminalpp::graphics::intensity>
: std::bool_constant<true>
{};

//* =========================================================================
/// \brief Streaming output operator for intensities. Prints the text
/// equivalent of the intensity (e.g. "normal", "bold", "faint").
Expand Down
6 changes: 6 additions & 0 deletions test/terminal_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ static streaming_text_data const streaming_text_data_table[] = {
streaming_text_data{ "\\i>abc"_ets, "\\i=de"_ets, "\x1B[0mde"_tb },
streaming_text_data{ "\\i>abc"_ets, "\\ixde"_ets, "\x1B[0mde"_tb },

// Bold-faint transitions must go through an intermediate normal state
// because certain terminals have a "bold-and-faint" combination that is
// not useful.
streaming_text_data{ "\\i<a"_ets, "\\i>b"_ets, "\x1B[22;1mb"_tb },
streaming_text_data{ "\\i>a"_ets, "\\i<b"_ets, "\x1B[22;2mb"_tb },

// Test polarity changes
streaming_text_data{ ""_ets, "\\p+abcde"_ets, "abcde"_tb },
streaming_text_data{ ""_ets, "\\p-abcde"_ets, "\x1B[7mabcde"_tb },
Expand Down

0 comments on commit 4c7b3a8

Please sign in to comment.