diff options
| author | Martin Hořeňovský | 2022-03-06 19:20:53 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-03-08 12:51:13 +0100 |
| commit | 913f79a661a02be5791be9bdf2b8ee123579b51d (patch) | |
| tree | 10ea8909ab64d3dcece21da18514baa54594b435 /src/catch2/internal | |
| parent | 06f74a0f8e505a08fff58e63beeb88569d8629d6 (diff) | |
| download | catch3-913f79a661a02be5791be9bdf2b8ee123579b51d.tar.gz | |
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.
Diffstat (limited to 'src/catch2/internal')
| -rw-r--r-- | src/catch2/internal/catch_console_colour.cpp | 208 | ||||
| -rw-r--r-- | src/catch2/internal/catch_console_colour.hpp | 44 |
2 files changed, 142 insertions, 110 deletions
diff --git a/src/catch2/internal/catch_console_colour.cpp b/src/catch2/internal/catch_console_colour.cpp index b3d583d2..4504eddc 100644 --- a/src/catch2/internal/catch_console_colour.cpp +++ b/src/catch2/internal/catch_console_colour.cpp @@ -24,23 +24,48 @@ #include <ostream> namespace Catch { - namespace { - struct IColourImpl { - virtual ~IColourImpl() = default; - virtual void use( Colour::Code _colourCode ) = 0; - }; + ColourImpl::~ColourImpl() = default; - struct NoColourImpl : IColourImpl { - void use( Colour::Code ) override {} + ColourImpl::ColourGuard ColourImpl::startColour( Colour::Code colourCode ) { + return ColourGuard(colourCode, this ); + } - static IColourImpl* instance() { - static NoColourImpl s_instance; - return &s_instance; - } + namespace { + //! A do-nothing implementation of colour, used as fallback for unknown + //! platforms, and when the user asks to deactivate all colours. + class NoColourImpl : public ColourImpl { + public: + NoColourImpl( IStream const* stream ): ColourImpl( stream ) {} + static bool useColourOnPlatform() { return true; } + + private: + void use( Colour::Code ) const override {} }; - } // anon namespace + } // namespace + + ColourImpl::ColourGuard::ColourGuard( Colour::Code code, + ColourImpl const* colour ): + m_colourImpl( colour ) { + m_colourImpl->use( code ); + } + ColourImpl::ColourGuard::ColourGuard( ColourGuard&& rhs ): + m_colourImpl( rhs.m_colourImpl ) { + rhs.m_moved = true; + } + ColourImpl::ColourGuard& + ColourImpl::ColourGuard::operator=( ColourGuard&& rhs ) { + m_colourImpl = rhs.m_colourImpl; + rhs.m_moved = true; + return *this; + } + ColourImpl::ColourGuard::~ColourGuard() { + if ( !m_moved ) { + m_colourImpl->use( Colour::None ); + } + } + } // namespace Catch #if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) @@ -57,9 +82,10 @@ namespace Catch { namespace Catch { namespace { - class Win32ColourImpl : public IColourImpl { + class Win32ColourImpl : public ColourImpl { public: - Win32ColourImpl() { + Win32ColourImpl(IStream const* stream): + ColourImpl(stream) { CONSOLE_SCREEN_BUFFER_INFO csbiInfo; GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbiInfo ); @@ -67,7 +93,16 @@ namespace { originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); } - void use( Colour::Code _colourCode ) override { + static bool useColourOnPlatform() { return true; } + + private: + void use( Colour::Code _colourCode ) const override { + // Early exit if we are not writing to the console, because + // Win32 API can only change colour of the console. + if ( !m_stream->isStdout() ) { + return; + } + switch( _colourCode ) { case Colour::None: return setTextAttribute( originalForegroundAttributes ); case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); @@ -91,8 +126,7 @@ namespace { } } - private: - void setTextAttribute( WORD _textAttribute ) { + void setTextAttribute( WORD _textAttribute ) const { SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), _textAttribute | originalBackgroundAttributes ); @@ -101,19 +135,6 @@ namespace { WORD originalBackgroundAttributes; }; - IColourImpl* platformColourInstance() { - static Win32ColourImpl s_instance; - - auto const* config = getCurrentContext().getConfig(); - UseColour colourMode = config? - config->useColour() : UseColour::Auto; - if( colourMode == UseColour::Auto ) - colourMode = UseColour::Yes; - return colourMode == UseColour::Yes - ? &s_instance - : NoColourImpl::instance(); - } - } // end anon namespace } // end namespace Catch @@ -128,9 +149,33 @@ namespace { // Thanks to Adam Strzelecki for original contribution // (http://github.com/nanoant) // https://github.com/philsquared/Catch/pull/131 - class PosixColourImpl : public IColourImpl { + class PosixColourImpl : public ColourImpl { public: - void use( Colour::Code _colourCode ) override { + PosixColourImpl( IStream const* stream ): ColourImpl( stream ) {} + + static bool useColourOnPlatform() { + ErrnoGuard _; // for isatty + return +# if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE ) + !isDebuggerActive() && +# endif +# if !( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) ) + isatty( STDOUT_FILENO ) +# else + false +# endif + ; + } + + private: + void use( Colour::Code _colourCode ) const override { + auto setColour = [&out = + m_stream->stream()]( char const* escapeCode ) { + // The escape sequence must be flushed to console, otherwise + // if stdin and stderr are intermixed, we'd get accidentally + // coloured output. + out << '\033' << escapeCode << std::flush; + }; switch( _colourCode ) { case Colour::None: case Colour::White: return setColour( "[0m" ); @@ -151,89 +196,52 @@ namespace { default: CATCH_INTERNAL_ERROR( "Unknown colour requested" ); } } - static IColourImpl* instance() { - static PosixColourImpl s_instance; - return &s_instance; - } - - private: - void setColour( const char* _escapeCode ) { - // The escape sequence must be flushed to console, otherwise if - // stdin and stderr are intermixed, we'd get accidentally coloured output. - getCurrentContext().getConfig()->defaultStream()->stream() - << '\033' << _escapeCode << std::flush; - } }; - bool useColourOnPlatform() { - return -#if defined(CATCH_PLATFORM_MAC) || defined(CATCH_PLATFORM_IPHONE) - !isDebuggerActive() && -#endif -#if !(defined(__DJGPP__) && defined(__STRICT_ANSI__)) - isatty(STDOUT_FILENO) -#else - false -#endif - ; - } - IColourImpl* platformColourInstance() { - ErrnoGuard guard; - auto const* config = getCurrentContext().getConfig(); - UseColour colourMode = config - ? config->useColour() - : UseColour::Auto; - if( colourMode == UseColour::Auto ) - colourMode = useColourOnPlatform() - ? UseColour::Yes - : UseColour::No; - return colourMode == UseColour::Yes - ? PosixColourImpl::instance() - : NoColourImpl::instance(); - } - } // end anon namespace } // end namespace Catch -#else // not Windows or ANSI /////////////////////////////////////////////// +#endif // Windows/ ANSI/ None namespace Catch { - static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); } - -} // end namespace Catch - -#endif // Windows/ ANSI/ None + Detail::unique_ptr<ColourImpl> makeColourImpl(IConfig const* config, IStream const* stream) { + UseColour colourMode = config ? config->useColour() : UseColour::Auto; -namespace Catch { + bool createPlatformInstance = false; + if ( colourMode == UseColour::No ) { + createPlatformInstance = false; + } - Colour::Colour( Code _colourCode ) { use( _colourCode ); } - Colour::Colour( Colour&& other ) noexcept { - m_moved = other.m_moved; - other.m_moved = true; - } - Colour& Colour::operator=( Colour&& other ) noexcept { - m_moved = other.m_moved; - other.m_moved = true; - return *this; - } + if ( colourMode == UseColour::Yes ) { + createPlatformInstance = true; + } - Colour::~Colour(){ if( !m_moved ) use( None ); } + if ( colourMode == UseColour::Auto ) { + createPlatformInstance = +#if defined( CATCH_CONFIG_COLOUR_ANSI ) + PosixColourImpl::useColourOnPlatform() +#elif defined( CATCH_CONFIG_COLOUR_WINDOWS ) + Win32ColourImpl::useColourOnPlatform() +#else + NoColourImpl::useColourOnPlatform() +#endif + ; + } - void Colour::use( Code _colourCode ) { - static IColourImpl* impl = platformColourInstance(); - // Strictly speaking, this cannot possibly happen. - // However, under some conditions it does happen (see #1626), - // and this change is small enough that we can let practicality - // triumph over purity in this case. - if (impl != nullptr) { - impl->use( _colourCode ); + if ( createPlatformInstance ) { + return +#if defined( CATCH_CONFIG_COLOUR_ANSI ) + Detail::make_unique<PosixColourImpl>(stream); +#elif defined( CATCH_CONFIG_COLOUR_WINDOWS ) + Detail::make_unique<Win32ColourImpl>(stream); +#else + Detail::make_unique<NoColourImpl>(stream); +#endif } + return Detail::make_unique<NoColourImpl>(stream); } - std::ostream& operator << ( std::ostream& os, Colour const& ) { - return os; - } } // end namespace Catch diff --git a/src/catch2/internal/catch_console_colour.hpp b/src/catch2/internal/catch_console_colour.hpp index 7b102013..e6a37967 100644 --- a/src/catch2/internal/catch_console_colour.hpp +++ b/src/catch2/internal/catch_console_colour.hpp @@ -8,10 +8,15 @@ #ifndef CATCH_CONSOLE_COLOUR_HPP_INCLUDED #define CATCH_CONSOLE_COLOUR_HPP_INCLUDED +#include <catch2/internal/catch_unique_ptr.hpp> + #include <iosfwd> namespace Catch { + struct IConfig; + class IStream; + struct Colour { enum Code { None = 0, @@ -48,22 +53,41 @@ namespace Catch { SecondaryText = LightGrey, Headers = White }; + }; - // Use constructed object for RAII guard - Colour( Code _colourCode ); - Colour( Colour&& other ) noexcept; - Colour& operator=( Colour&& other ) noexcept; - ~Colour(); + class ColourImpl { + protected: + //! The associated stream of this ColourImpl instance + IStream const* m_stream; + public: + ColourImpl( IStream const* stream ): m_stream( stream ) {} + + class ColourGuard { + ColourImpl const* m_colourImpl; + bool m_moved = false; + friend std::ostream& operator<<(std::ostream& lhs, + ColourGuard const&) { + return lhs; + } + public: + ColourGuard( Colour::Code code, + ColourImpl const* colour ); + ColourGuard( ColourGuard const& rhs ) = delete; + ColourGuard& operator=( ColourGuard const& rhs ) = delete; + ColourGuard( ColourGuard&& rhs ); + ColourGuard& operator=( ColourGuard&& rhs ); + ~ColourGuard(); + }; - // Use static method for one-shot changes - static void use( Code _colourCode ); + virtual ~ColourImpl(); // = default + ColourGuard startColour( Colour::Code colourCode ); private: - bool m_moved = false; - - friend std::ostream& operator << (std::ostream& os, Colour const&); + virtual void use( Colour::Code colourCode ) const = 0; }; + //! Provides ColourImpl based on global config and target compilation platform + Detail::unique_ptr<ColourImpl> makeColourImpl( IConfig const* config, IStream const* stream ); } // end namespace Catch |