Added --use-colour option to give finer control over colourisation.
--force-colour is still present but deprecated (will remove in v2)
diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp
index 640b304..6926559 100644
--- a/include/internal/catch_commandline.hpp
+++ b/include/internal/catch_commandline.hpp
@@ -62,6 +62,21 @@
? 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 @@
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 @@
.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 a09210c..2665f47 100644
--- a/include/internal/catch_config.hpp
+++ b/include/internal/catch_config.hpp
@@ -37,14 +37,14 @@
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 @@
bool noThrow;
bool showHelp;
bool showInvisibles;
- bool forceColour;
bool filenamesAsTags;
int abortAfter;
@@ -67,6 +66,7 @@
WarnAbout::What warnings;
ShowDurations::OrNot showDurations;
RunTests::InWhatOrder runOrder;
+ UseColour::YesOrNo useColour;
std::string outputFilename;
std::string name;
@@ -133,7 +133,7 @@
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 9ca915f..f776952 100644
--- a/include/internal/catch_console_colour_impl.hpp
+++ b/include/internal/catch_console_colour_impl.hpp
@@ -95,7 +95,18 @@
IColourImpl* platformColourInstance() {
static Win32ColourImpl s_instance;
- return &s_instance;
+
+ Ptr<IConfig const> 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 @@
IColourImpl* platformColourInstance() {
Ptr<IConfig const> 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 @@
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 1281bd7..17914b4 100644
--- a/include/internal/catch_interfaces_config.h
+++ b/include/internal/catch_interfaces_config.h
@@ -37,6 +37,11 @@
InLexicographicalOrder,
InRandomOrder
}; };
+ struct UseColour { enum YesOrNo {
+ Auto,
+ Yes,
+ No
+ }; };
class TestSpec;
@@ -56,7 +61,7 @@
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 1713b51..c63f469 100644
--- a/projects/SelfTest/TestMain.cpp
+++ b/projects/SelfTest/TestMain.cpp
@@ -195,19 +195,41 @@
}
}
- 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 a87366e..52bcea1 100644
--- a/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj
+++ b/projects/VS2010/TestCatch/TestCatch/TestCatch.vcxproj
@@ -93,7 +93,7 @@
<ItemGroup>
<ClCompile Include="..\..\..\SelfTest\ApproxTests.cpp" />
<ClCompile Include="..\..\..\SelfTest\BDDTests.cpp" />
- <ClCompile Include="..\..\..\SelfTest\SectionTrackerTests.cpp" />
+ <ClCompile Include="..\..\..\SelfTest\PartTrackerTests.cpp" />
<ClCompile Include="..\..\..\SelfTest\TestMain.cpp" />
<ClCompile Include="..\..\..\SelfTest\ClassTests.cpp" />
<ClCompile Include="..\..\..\SelfTest\ConditionTests.cpp" />