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