mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Cleaned up terminal colouring impl and added more config options
This commit is contained in:
parent
a5dca3d370
commit
bde3567f3e
@ -12,10 +12,6 @@
|
||||
|
||||
namespace Catch {
|
||||
|
||||
namespace Detail {
|
||||
struct IColourImpl;
|
||||
}
|
||||
|
||||
struct Colour {
|
||||
enum Code {
|
||||
None = 0,
|
||||
@ -61,7 +57,6 @@ namespace Catch {
|
||||
static void use( Code _colourCode );
|
||||
|
||||
private:
|
||||
static Detail::IColourImpl* impl();
|
||||
bool m_moved;
|
||||
};
|
||||
|
||||
|
@ -10,14 +10,36 @@
|
||||
|
||||
#include "catch_console_colour.hpp"
|
||||
|
||||
namespace Catch { namespace Detail {
|
||||
namespace Catch {
|
||||
namespace {
|
||||
|
||||
struct IColourImpl {
|
||||
virtual ~IColourImpl() {}
|
||||
virtual void use( Colour::Code _colourCode ) = 0;
|
||||
};
|
||||
}}
|
||||
|
||||
#if defined ( CATCH_PLATFORM_WINDOWS ) /////////////////////////////////////////
|
||||
struct NoColourImpl : IColourImpl {
|
||||
void use( Colour::Code ) {}
|
||||
|
||||
static IColourImpl* instance() {
|
||||
static NoColourImpl s_instance;
|
||||
return &s_instance;
|
||||
}
|
||||
};
|
||||
|
||||
} // anon namespace
|
||||
} // namespace Catch
|
||||
|
||||
#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
|
||||
|
||||
|
||||
#if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) /////////////////////////////////////////
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
@ -32,7 +54,7 @@ namespace Catch { namespace Detail {
|
||||
namespace Catch {
|
||||
namespace {
|
||||
|
||||
class Win32ColourImpl : public Detail::IColourImpl {
|
||||
class Win32ColourImpl : public IColourImpl {
|
||||
public:
|
||||
Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) )
|
||||
{
|
||||
@ -69,11 +91,7 @@ namespace {
|
||||
WORD originalAttributes;
|
||||
};
|
||||
|
||||
inline bool shouldUseColourForPlatform() {
|
||||
return true;
|
||||
}
|
||||
|
||||
static Detail::IColourImpl* platformColourInstance() {
|
||||
IColourImpl* platformColourInstance() {
|
||||
static Win32ColourImpl s_instance;
|
||||
return &s_instance;
|
||||
}
|
||||
@ -81,7 +99,7 @@ namespace {
|
||||
} // end anon namespace
|
||||
} // end namespace Catch
|
||||
|
||||
#else // Not Windows - assumed to be POSIX compatible //////////////////////////
|
||||
#elif defined( CATCH_CONFIG_COLOUR_ANSI ) //////////////////////////////////////
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
@ -92,7 +110,7 @@ namespace {
|
||||
// Thanks to Adam Strzelecki for original contribution
|
||||
// (http://github.com/nanoant)
|
||||
// https://github.com/philsquared/Catch/pull/131
|
||||
class PosixColourImpl : public Detail::IColourImpl {
|
||||
class PosixColourImpl : public IColourImpl {
|
||||
public:
|
||||
virtual void use( Colour::Code _colourCode ) {
|
||||
switch( _colourCode ) {
|
||||
@ -113,53 +131,47 @@ namespace {
|
||||
case Colour::Bright: throw std::logic_error( "not a colour" );
|
||||
}
|
||||
}
|
||||
static IColourImpl* instance() {
|
||||
static PosixColourImpl s_instance;
|
||||
return &s_instance;
|
||||
}
|
||||
|
||||
private:
|
||||
void setColour( const char* _escapeCode ) {
|
||||
Catch::cout() << '\033' << _escapeCode;
|
||||
}
|
||||
};
|
||||
|
||||
inline bool shouldUseColourForPlatform() {
|
||||
return isatty(STDOUT_FILENO);
|
||||
}
|
||||
|
||||
static Detail::IColourImpl* platformColourInstance() {
|
||||
static PosixColourImpl s_instance;
|
||||
return &s_instance;
|
||||
IColourImpl* platformColourInstance() {
|
||||
return isatty(STDOUT_FILENO)
|
||||
? PosixColourImpl::instance()
|
||||
: NoColourImpl::instance();
|
||||
}
|
||||
|
||||
} // end anon namespace
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // not Windows
|
||||
#else // not Windows or ANSI ///////////////////////////////////////////////
|
||||
|
||||
namespace Catch {
|
||||
|
||||
namespace {
|
||||
struct NoColourImpl : Detail::IColourImpl {
|
||||
void use( Colour::Code ) {}
|
||||
static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); }
|
||||
|
||||
static IColourImpl* instance() {
|
||||
static NoColourImpl s_instance;
|
||||
return &s_instance;
|
||||
}
|
||||
};
|
||||
static bool shouldUseColour() {
|
||||
return shouldUseColourForPlatform() && !isDebuggerActive();
|
||||
}
|
||||
}
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // Windows/ ANSI/ None
|
||||
|
||||
namespace Catch {
|
||||
|
||||
Colour::Colour( Code _colourCode ) : m_moved( false ) { use( _colourCode ); }
|
||||
Colour::Colour( Colour const& _other ) : m_moved( false ) { const_cast<Colour&>( _other ).m_moved = true; }
|
||||
Colour::~Colour(){ if( !m_moved ) use( None ); }
|
||||
void Colour::use( Code _colourCode ) {
|
||||
impl()->use( _colourCode );
|
||||
}
|
||||
|
||||
Detail::IColourImpl* Colour::impl() {
|
||||
return shouldUseColour()
|
||||
? platformColourInstance()
|
||||
: NoColourImpl::instance();
|
||||
void Colour::use( Code _colourCode ) {
|
||||
static IColourImpl* impl = isDebuggerActive()
|
||||
? NoColourImpl::instance()
|
||||
: platformColourInstance();
|
||||
impl->use( _colourCode );
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
@ -28,7 +28,9 @@ namespace Catch {
|
||||
|
||||
namespace Catch {
|
||||
|
||||
struct FatalConditionHandler {};
|
||||
struct FatalConditionHandler {
|
||||
void reset() {}
|
||||
};
|
||||
|
||||
} // namespace Catch
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user