From 581c1b487af428d343a39df775ddd0ec848dbab4 Mon Sep 17 00:00:00 2001 From: Corentin Schreiber Date: Wed, 12 Apr 2023 09:18:20 +0100 Subject: [PATCH] Prevent color tags being truncated for #82 --- include/snitch/snitch.hpp | 5 ++++- src/snitch.cpp | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/snitch/snitch.hpp b/include/snitch/snitch.hpp index 68c57015..52211750 100644 --- a/include/snitch/snitch.hpp +++ b/include/snitch/snitch.hpp @@ -2102,8 +2102,11 @@ class registry { template void print(Args&&... args) const noexcept { small_string message; - append_or_truncate(message, std::forward(args)...); + const bool could_fit = append(message, std::forward(args)...); this->print_callback(message); + if (!could_fit) { + this->print_callback("..."); + } } // Requires: number of tests + 1 <= max_test_cases, well-formed test ID. diff --git a/src/snitch.cpp b/src/snitch.cpp index cb427a91..2a2c2073 100644 --- a/src/snitch.cpp +++ b/src/snitch.cpp @@ -15,7 +15,7 @@ namespace { using namespace std::literals; -using color_t = const char*; +using color_t = std::string_view; namespace color { constexpr color_t error [[maybe_unused]] = "\x1b[1;31m"; @@ -312,7 +312,17 @@ using snitch::small_string; template bool append(small_string_span ss, const colored& colored_value) noexcept { - return append(ss, colored_value.color_start, colored_value.value, colored_value.color_end); + if (ss.available() <= colored_value.color_start.size() + colored_value.color_end.size()) { + return false; + } + + bool could_fit = true; + if (!append(ss, colored_value.color_start, colored_value.value)) { + ss.resize(ss.capacity() - colored_value.color_end.size()); + could_fit = false; + } + + return append(ss, colored_value.color_end) && could_fit; } template