Each reporter keeps its own colour implementation

This opens path to per-reporter colour output customization,
and fixes multiple issues with the old colour implementation.

Under the old implementation, using Win32-backed colouring
would always change the colour used by the console, even if the
actual output was written elsewhere, such as a file passed by
the `--out` flag. This will no longer happen, as the reporter's
colour impl will check that the reporter's stream is pointed
to console before trying to change the colours.

POSIX/ANSI colour implementation suffered a similar-ish issue,
in that it only wrote the colour escape codes into the default
output stream, even if the reporter asking for colouring was
actually writing to a completely different output stream.
This commit is contained in:
Martin Hořeňovský
2022-03-08 12:51:13 +01:00
parent 06f74a0f8e
commit 913f79a661
11 changed files with 271 additions and 229 deletions
+11 -6
View File
@@ -151,14 +151,16 @@ namespace Catch {
getCurrentMutableContext().setConfig(m_config.get());
m_startupExceptions = true;
Colour colourGuard( Colour::Red );
Catch::cerr() << "Errors occurred during startup!" << '\n';
auto errStream = makeStream( "%stderr" );
auto colourImpl = makeColourImpl( &config(), errStream.get() );
auto guard = colourImpl->startColour( Colour::Red );
errStream->stream() << "Errors occurred during startup!" << '\n';
// iterate over all exceptions and notify user
for ( const auto& ex_ptr : exceptions ) {
try {
std::rethrow_exception(ex_ptr);
} catch ( std::exception const& ex ) {
Catch::cerr() << TextFlow::Column( ex.what() ).indent(2) << '\n';
errStream->stream() << TextFlow::Column( ex.what() ).indent(2) << '\n';
}
}
}
@@ -194,12 +196,15 @@ namespace Catch {
if( !result ) {
config();
getCurrentMutableContext().setConfig(m_config.get());
Catch::cerr()
<< Colour( Colour::Red )
auto errStream = makeStream( "%stderr" );
auto colour = makeColourImpl( &config(), errStream.get() );
errStream->stream()
<< colour->startColour( Colour::Red )
<< "\nError(s) in input:\n"
<< TextFlow::Column( result.errorMessage() ).indent( 2 )
<< "\n\n";
Catch::cerr() << "Run with -? for usage\n\n" << std::flush;
errStream->stream() << "Run with -? for usage\n\n" << std::flush;
return MaxExitCode;
}