diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp index 640b304d..6926559a 100644 --- a/include/internal/catch_commandline.hpp +++ b/include/internal/catch_commandline.hpp @@ -62,6 +62,21 @@ namespace Catch { ? ShowDurations::Always : ShowDurations::Never; } + inline void setUseColour( ConfigData& config, std::string const& value ) { + std::string mode = toLower( value ); + + if( mode == "yes" ) + config.useColour = UseColour::Yes; + else if( mode == "no" ) + config.useColour = UseColour::No; + else if( mode == "auto" ) + config.useColour = UseColour::Auto; + else + throw std::runtime_error( "colour mode must be one of: auto, yes or no" ); + } + inline void forceColour( ConfigData& config ) { + config.useColour = UseColour::Yes; + } inline void loadTestNamesFromFile( ConfigData& config, std::string const& _filename ) { std::ifstream f( _filename.c_str() ); if( !f.is_open() ) @@ -148,7 +163,7 @@ namespace Catch { cli["-d"]["--durations"] .describe( "show test durations" ) - .bind( &setShowDurations, "yes/no" ); + .bind( &setShowDurations, "yes|no" ); cli["-f"]["--input-file"] .describe( "load test names to run from a file" ) @@ -176,8 +191,12 @@ namespace Catch { .bind( &setRngSeed, "'time'|number" ); cli["--force-colour"] - .describe( "force colourised output" ) - .bind( &ConfigData::forceColour ); + .describe( "force colourised output (deprecated)" ) + .bind( &forceColour ); + + cli["--use-colour"] + .describe( "should output be colourised" ) + .bind( &setUseColour, "yes|no" ); return cli; } diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp index a09210cb..2665f474 100644 --- a/include/internal/catch_config.hpp +++ b/include/internal/catch_config.hpp @@ -37,14 +37,14 @@ namespace Catch { noThrow( false ), showHelp( false ), showInvisibles( false ), - forceColour( false ), filenamesAsTags( false ), abortAfter( -1 ), rngSeed( 0 ), verbosity( Verbosity::Normal ), warnings( WarnAbout::Nothing ), showDurations( ShowDurations::DefaultForReporter ), - runOrder( RunTests::InDeclarationOrder ) + runOrder( RunTests::InDeclarationOrder ), + useColour( UseColour::Auto ) {} bool listTests; @@ -57,7 +57,6 @@ namespace Catch { bool noThrow; bool showHelp; bool showInvisibles; - bool forceColour; bool filenamesAsTags; int abortAfter; @@ -67,6 +66,7 @@ namespace Catch { WarnAbout::What warnings; ShowDurations::OrNot showDurations; RunTests::InWhatOrder runOrder; + UseColour::YesOrNo useColour; std::string outputFilename; std::string name; @@ -133,7 +133,7 @@ namespace Catch { virtual ShowDurations::OrNot showDurations() const { return m_data.showDurations; } virtual RunTests::InWhatOrder runOrder() const { return m_data.runOrder; } virtual unsigned int rngSeed() const { return m_data.rngSeed; } - virtual bool forceColour() const { return m_data.forceColour; } + virtual UseColour::YesOrNo useColour() const { return m_data.useColour; } private: diff --git a/include/internal/catch_console_colour_impl.hpp b/include/internal/catch_console_colour_impl.hpp index 9ca915ff..f776952b 100644 --- a/include/internal/catch_console_colour_impl.hpp +++ b/include/internal/catch_console_colour_impl.hpp @@ -95,7 +95,18 @@ namespace { IColourImpl* platformColourInstance() { static Win32ColourImpl s_instance; - return &s_instance; + + Ptr config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = !isDebuggerActive() + ? UseColour::Yes + : UseColour::No; + return colourMode == UseColour::Yes + ? &s_instance + : NoColourImpl::instance(); } } // end anon namespace @@ -146,7 +157,14 @@ namespace { IColourImpl* platformColourInstance() { Ptr config = getCurrentContext().getConfig(); - return (config && config->forceColour()) || isatty(STDOUT_FILENO) + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = (!isDebuggerActive() && isatty(STDOUT_FILENO) ) + ? UseColour::Yes + : UseColour::No; + return colourMode == UseColour::Yes ? PosixColourImpl::instance() : NoColourImpl::instance(); } @@ -171,9 +189,7 @@ namespace Catch { Colour::~Colour(){ if( !m_moved ) use( None ); } void Colour::use( Code _colourCode ) { - static IColourImpl* impl = isDebuggerActive() - ? NoColourImpl::instance() - : platformColourInstance(); + static IColourImpl* impl = platformColourInstance(); impl->use( _colourCode ); } diff --git a/include/internal/catch_interfaces_config.h b/include/internal/catch_interfaces_config.h index 1281bd7e..17914b4e 100644 --- a/include/internal/catch_interfaces_config.h +++ b/include/internal/catch_interfaces_config.h @@ -37,6 +37,11 @@ namespace Catch { InLexicographicalOrder, InRandomOrder }; }; + struct UseColour { enum YesOrNo { + Auto, + Yes, + No + }; }; class TestSpec; @@ -56,7 +61,7 @@ namespace Catch { virtual TestSpec const& testSpec() const = 0; virtual RunTests::InWhatOrder runOrder() const = 0; virtual unsigned int rngSeed() const = 0; - virtual bool forceColour() const = 0; + virtual UseColour::YesOrNo useColour() const = 0; }; } diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp index 1713b51a..c63f469f 100644 --- a/projects/SelfTest/TestMain.cpp +++ b/projects/SelfTest/TestMain.cpp @@ -195,19 +195,41 @@ TEST_CASE( "Process can be configured on command line", "[config][command-line]" } } - SECTION( "force-colour", "") { - SECTION( "--force-colour", "" ) { - const char* argv[] = { "test", "--force-colour" }; - CHECK_NOTHROW( parseIntoConfig( argv, config ) ); - - REQUIRE( config.forceColour ); - } - - SECTION( "without --force-colour", "" ) { + SECTION( "use-colour", "") { + + using Catch::UseColour; + + SECTION( "without option", "" ) { const char* argv[] = { "test" }; CHECK_NOTHROW( parseIntoConfig( argv, config ) ); + + REQUIRE( config.useColour == UseColour::Auto ); + } - REQUIRE( !config.forceColour ); + SECTION( "auto", "" ) { + const char* argv[] = { "test", "--use-colour", "auto" }; + CHECK_NOTHROW( parseIntoConfig( argv, config ) ); + + REQUIRE( config.useColour == UseColour::Auto ); + } + + SECTION( "yes", "" ) { + const char* argv[] = { "test", "--use-colour", "yes" }; + CHECK_NOTHROW( parseIntoConfig( argv, config ) ); + + REQUIRE( config.useColour == UseColour::Yes ); + } + + SECTION( "no", "" ) { + const char* argv[] = { "test", "--use-colour", "no" }; + CHECK_NOTHROW( parseIntoConfig( argv, config ) ); + + REQUIRE( config.useColour == UseColour::No ); + } + + SECTION( "error", "" ) { + const char* argv[] = { "test", "--use-colour", "wrong" }; + REQUIRE_THROWS_WITH( parseIntoConfig( argv, config ), Contains( "colour mode must be one of" ) ); } } } diff --git a/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj b/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj index a87366e7..52bcea11 100644 --- a/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj +++ b/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj @@ -93,7 +93,7 @@ - +