diff options
| author | Martin Hořeňovský | 2022-03-27 23:35:41 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-03-28 13:10:13 +0200 |
| commit | 1a8a793178d50b74b0f9a0adb3eec937b61039a9 (patch) | |
| tree | 2a8cadf3fa4478f1a11548485a4a0389d4920d2d /src/catch2/internal/catch_console_colour.cpp | |
| parent | a4e4e82474f894d56cbe86e13a2d23bb1b46744d (diff) | |
| download | catch3-1a8a793178d50b74b0f9a0adb3eec937b61039a9.tar.gz | |
Refactor colour handling to prepare for per-reporter colour modes
This includes always compiling the ANSI and None colour
implementations, as they don't need to touch any platform
specific APIs, and removing their respective compile-time
configuration options.
Because the Win32 colour implementation requires Win32-specific
APIs, it is still hidden behind a compile-time toggle,
`CATCH_CONFIG_COLOUR_WIN32` (renamed from `..._COLOUR_WINDOWS`).
The commandline options for colours were also changed. The
option now uses different name, and allows to select between
different implementations, rather than changing whether
the compiled-in colour implementation is used through
"yes/no/default" options.
Diffstat (limited to 'src/catch2/internal/catch_console_colour.cpp')
| -rw-r--r-- | src/catch2/internal/catch_console_colour.cpp | 110 |
1 files changed, 54 insertions, 56 deletions
diff --git a/src/catch2/internal/catch_console_colour.cpp b/src/catch2/internal/catch_console_colour.cpp index b7b4b4fb..67f0d98a 100644 --- a/src/catch2/internal/catch_console_colour.cpp +++ b/src/catch2/internal/catch_console_colour.cpp @@ -20,6 +20,7 @@ #include <catch2/internal/catch_platform.hpp> #include <catch2/internal/catch_debugger.hpp> #include <catch2/internal/catch_windows_h_proxy.hpp> +#include <catch2/internal/catch_compiler_capabilities.hpp> #include <ostream> @@ -31,20 +32,6 @@ namespace Catch { return ColourGuard(colourCode, this ); } - 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 {} - }; - - } // namespace - void ColourImpl::ColourGuard::engageImpl( std::ostream& stream ) { assert( &stream == &m_colourImpl->m_stream->stream() && "Engaging colour guard for different stream than used by the " @@ -92,18 +79,24 @@ namespace Catch { return CATCH_MOVE(*this); } -} // namespace Catch + 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; } -#if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) -# ifdef CATCH_PLATFORM_WINDOWS -# define CATCH_CONFIG_COLOUR_WINDOWS -# else -# define CATCH_CONFIG_COLOUR_ANSI -# endif -#endif + private: + void use( Colour::Code ) const override {} + }; + } // namespace -#if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) ///////////////////////////////////////// +} // namespace Catch + + +#if defined ( CATCH_CONFIG_COLOUR_WIN32 ) ///////////////////////////////////////// namespace Catch { namespace { @@ -174,13 +167,9 @@ namespace { namespace Catch { namespace { - // use POSIX/ ANSI console terminal codes - // Thanks to Adam Strzelecki for original contribution - // (http://github.com/nanoant) - // https://github.com/philsquared/Catch/pull/131 - class PosixColourImpl : public ColourImpl { + class ANSIColourImpl : public ColourImpl { public: - PosixColourImpl( IStream const* stream ): ColourImpl( stream ) {} + ANSIColourImpl( IStream const* stream ): ColourImpl( stream ) {} static bool useColourOnPlatform(IStream const& stream) { // This is kinda messy due to trying to support a bunch of @@ -239,41 +228,50 @@ namespace { namespace Catch { - Detail::unique_ptr<ColourImpl> makeColourImpl(IConfig const* config, IStream const* stream) { - UseColour colourMode = config ? config->useColour() : UseColour::Auto; - - bool createPlatformInstance = false; - if ( colourMode == UseColour::No ) { - createPlatformInstance = false; + Detail::unique_ptr<ColourImpl> makeColourImpl( ColourMode implSelection, + IStream const* stream ) { + if ( implSelection == ColourMode::None ) { + return Detail::make_unique<NoColourImpl>( stream ); } - - if ( colourMode == UseColour::Yes ) { - createPlatformInstance = true; + if ( implSelection == ColourMode::ANSI ) { + return Detail::make_unique<ANSIColourImpl>( stream ); } +#if defined( CATCH_CONFIG_COLOUR_WIN32 ) + if ( implSelection == ColourMode::Win32 ) { + return Detail::make_unique<Win32ColourImpl>( stream ); + } +#endif - if ( colourMode == UseColour::Auto ) { - createPlatformInstance = -#if defined( CATCH_CONFIG_COLOUR_ANSI ) - PosixColourImpl::useColourOnPlatform( *stream ) -#elif defined( CATCH_CONFIG_COLOUR_WINDOWS ) - Win32ColourImpl::useColourOnPlatform( *stream ) -#else - false + // todo: check win32 eligibility under ifdef, otherwise ansi + if ( implSelection == ColourMode::PlatformDefault) { +#if defined (CATCH_CONFIG_COLOUR_WIN32) + if ( Win32ColourImpl::useColourOnPlatform( *stream ) ) { + return Detail::make_unique<Win32ColourImpl>( stream ); + } else { + return Detail::make_unique<NoColourImpl>( stream ); + } #endif - ; + if ( ANSIColourImpl::useColourOnPlatform( *stream ) ) { + return Detail::make_unique<ANSIColourImpl>( stream ); + } + return Detail::make_unique<NoColourImpl>( stream ); } - 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); + CATCH_ERROR( "Could not create colour impl for selection " << static_cast<int>(implSelection) ); + } + + bool isColourImplAvailable( ColourMode colourSelection ) { + switch ( colourSelection ) { +#if defined( CATCH_CONFIG_COLOUR_WIN32 ) + case ColourMode::Win32: #endif + case ColourMode::ANSI: + case ColourMode::None: + case ColourMode::PlatformDefault: + return true; + default: + return false; } - return Detail::make_unique<NoColourImpl>(stream); } |