Reuse color definitions between SourceReferenceFormatter and CommonSyntaxTest

This commit is contained in:
Kamil Śliwak 2023-09-29 22:43:49 +02:00
parent 7857562a3b
commit 6c1ed8c2d0
3 changed files with 49 additions and 28 deletions

View File

@ -55,6 +55,28 @@ std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error
); );
} }
char const* SourceReferenceFormatter::errorTextColor(Error::Severity _severity)
{
switch (_severity)
{
case Error::Severity::Error: return RED;
case Error::Severity::Warning: return YELLOW;
case Error::Severity::Info: return WHITE;
}
util::unreachable();
}
char const* SourceReferenceFormatter::errorHighlightColor(Error::Severity _severity)
{
switch (_severity)
{
case Error::Severity::Error: return RED_BACKGROUND;
case Error::Severity::Warning: return ORANGE_BACKGROUND_256;
case Error::Severity::Info: return GRAY_BACKGROUND;
}
util::unreachable();
}
AnsiColorized SourceReferenceFormatter::normalColored() const AnsiColorized SourceReferenceFormatter::normalColored() const
{ {
return AnsiColorized(m_stream, m_colored, {WHITE}); return AnsiColorized(m_stream, m_colored, {WHITE});
@ -67,18 +89,7 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
{ {
// We used to color messages of any severity as errors so this seems like a good default return AnsiColorized(m_stream, m_colored, {BOLD, errorTextColor(_severity)});
// for cases where severity cannot be determined.
char const* textColor = RED;
switch (_severity)
{
case Error::Severity::Error: textColor = RED; break;
case Error::Severity::Warning: textColor = YELLOW; break;
case Error::Severity::Info: textColor = WHITE; break;
}
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
} }
AnsiColorized SourceReferenceFormatter::messageColored() const AnsiColorized SourceReferenceFormatter::messageColored() const

View File

@ -118,6 +118,15 @@ public:
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream); static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
/// The default text color for printing error messages of a given severity in the terminal.
/// Only suitable for color schemes with a dark background.
static char const* errorTextColor(Error::Severity _severity);
/// The default background color for highlighting source fragments corresponding to an error
/// of a given severity in the terminal.
/// Only suitable for color schemes with a light text.
static char const* errorHighlightColor(Error::Severity _severity);
private: private:
util::AnsiColorized normalColored() const; util::AnsiColorized normalColored() const;
util::AnsiColorized frameColored() const; util::AnsiColorized frameColored() const;

View File

@ -19,11 +19,16 @@
#include <test/CommonSyntaxTest.h> #include <test/CommonSyntaxTest.h>
#include <test/Common.h> #include <test/Common.h>
#include <test/TestCase.h> #include <test/TestCase.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <libsolutil/CommonIO.h> #include <libsolutil/CommonIO.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <fstream> #include <fstream>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
@ -115,21 +120,17 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix,
assert(static_cast<size_t>(error.locationStart) <= source.length()); assert(static_cast<size_t>(error.locationStart) <= source.length());
assert(static_cast<size_t>(error.locationEnd) <= source.length()); assert(static_cast<size_t>(error.locationEnd) <= source.length());
for (int i = error.locationStart; i < error.locationEnd; i++) for (int i = error.locationStart; i < error.locationEnd; i++)
if (error.type == Error::Type::Info) {
{ char const*& cellFormat = sourceFormatting[static_cast<size_t>(i)];
if (sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET) char const* infoBgColor = SourceReferenceFormatter::errorHighlightColor(Error::Severity::Info);
sourceFormatting[static_cast<size_t>(i)] = util::formatting::GRAY_BACKGROUND;
} if (
else if (error.type == Error::Type::Warning) (error.type != Error::Type::Warning && error.type != Error::Type::Info) ||
{ (error.type == Error::Type::Warning && (cellFormat == RESET || cellFormat == infoBgColor)) ||
if ( (error.type == Error::Type::Info && cellFormat == RESET)
sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET || )
sourceFormatting[static_cast<size_t>(i)] == util::formatting::GRAY_BACKGROUND cellFormat = SourceReferenceFormatter::errorHighlightColor(Error::errorSeverity(error.type));
) }
sourceFormatting[static_cast<size_t>(i)] = util::formatting::ORANGE_BACKGROUND_256;
}
else
sourceFormatting[static_cast<size_t>(i)] = util::formatting::RED_BACKGROUND;
} }
_stream << _linePrefix << sourceFormatting.front() << source.front(); _stream << _linePrefix << sourceFormatting.front() << source.front();
@ -202,7 +203,7 @@ void CommonSyntaxTest::printErrorList(
util::AnsiColorized scope( util::AnsiColorized scope(
_stream, _stream,
_formatted, _formatted,
{BOLD, error.type == Error::Type::Info ? WHITE : (error.type == Error::Type::Warning ? YELLOW : RED)} {BOLD, SourceReferenceFormatter::errorTextColor(Error::errorSeverity(error.type))}
); );
_stream << _linePrefix << Error::formatErrorType(error.type); _stream << _linePrefix << Error::formatErrorType(error.type);
if (error.errorId.has_value()) if (error.errorId.has_value())