2012-02-25 21:36:22 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 25/2/2012.
|
|
|
|
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
|
2017-09-07 16:51:33 +02:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wexit-time-destructors"
|
|
|
|
#endif
|
2018-01-26 23:04:54 +01:00
|
|
|
|
|
|
|
|
2017-09-07 12:24:33 +02:00
|
|
|
#include "catch_console_colour.h"
|
2017-08-01 18:46:33 +02:00
|
|
|
#include "catch_enforce.h"
|
2017-07-06 22:28:42 +02:00
|
|
|
#include "catch_errno_guard.h"
|
|
|
|
#include "catch_interfaces_config.h"
|
|
|
|
#include "catch_stream.h"
|
|
|
|
#include "catch_context.h"
|
|
|
|
#include "catch_platform.h"
|
2017-09-19 16:25:33 +02:00
|
|
|
#include "catch_debugger.h"
|
2017-10-12 11:37:03 +02:00
|
|
|
#include "catch_windows_h_proxy.h"
|
2012-02-25 21:36:22 +01:00
|
|
|
|
2017-11-07 19:01:10 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
namespace Catch {
|
|
|
|
namespace {
|
2013-04-05 08:47:36 +02:00
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
struct IColourImpl {
|
2017-07-20 00:27:28 +02:00
|
|
|
virtual ~IColourImpl() = default;
|
2014-10-21 08:24:30 +02:00
|
|
|
virtual void use( Colour::Code _colourCode ) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 ) /////////////////////////////////////////
|
2013-03-04 12:19:15 +01:00
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
namespace Catch {
|
2013-03-11 19:38:29 +01:00
|
|
|
namespace {
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
class Win32ColourImpl : public IColourImpl {
|
2013-04-05 08:47:36 +02:00
|
|
|
public:
|
|
|
|
Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) )
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo );
|
2015-07-03 12:02:40 +02:00
|
|
|
originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY );
|
|
|
|
originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
2013-04-05 21:55:57 +02:00
|
|
|
|
2018-07-12 14:27:06 +02:00
|
|
|
void use( Colour::Code _colourCode ) override {
|
2013-04-05 08:47:36 +02:00
|
|
|
switch( _colourCode ) {
|
2015-07-03 12:02:40 +02:00
|
|
|
case Colour::None: return setTextAttribute( originalForegroundAttributes );
|
2013-04-05 08:47:36 +02:00
|
|
|
case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE );
|
|
|
|
case Colour::Red: return setTextAttribute( FOREGROUND_RED );
|
|
|
|
case Colour::Green: return setTextAttribute( FOREGROUND_GREEN );
|
|
|
|
case Colour::Blue: return setTextAttribute( FOREGROUND_BLUE );
|
|
|
|
case Colour::Cyan: return setTextAttribute( FOREGROUND_BLUE | FOREGROUND_GREEN );
|
|
|
|
case Colour::Yellow: return setTextAttribute( FOREGROUND_RED | FOREGROUND_GREEN );
|
|
|
|
case Colour::Grey: return setTextAttribute( 0 );
|
|
|
|
|
|
|
|
case Colour::LightGrey: return setTextAttribute( FOREGROUND_INTENSITY );
|
|
|
|
case Colour::BrightRed: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED );
|
|
|
|
case Colour::BrightGreen: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN );
|
2013-06-14 20:18:13 +02:00
|
|
|
case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE );
|
2018-01-26 23:04:54 +01:00
|
|
|
case Colour::BrightYellow: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN );
|
2013-04-05 08:47:36 +02:00
|
|
|
|
2017-05-05 16:42:57 +02:00
|
|
|
case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" );
|
2018-01-26 23:04:54 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
CATCH_ERROR( "Unknown colour requested" );
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:14:59 +02:00
|
|
|
private:
|
2013-04-05 08:47:36 +02:00
|
|
|
void setTextAttribute( WORD _textAttribute ) {
|
2015-07-03 12:02:40 +02:00
|
|
|
SetConsoleTextAttribute( stdoutHandle, _textAttribute | originalBackgroundAttributes );
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
|
|
|
HANDLE stdoutHandle;
|
2015-07-03 12:02:40 +02:00
|
|
|
WORD originalForegroundAttributes;
|
|
|
|
WORD originalBackgroundAttributes;
|
2013-04-05 08:47:36 +02:00
|
|
|
};
|
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
IColourImpl* platformColourInstance() {
|
2014-04-15 19:44:37 +02:00
|
|
|
static Win32ColourImpl s_instance;
|
2016-02-24 19:51:01 +01:00
|
|
|
|
2017-04-25 21:18:02 +02:00
|
|
|
IConfigPtr config = getCurrentContext().getConfig();
|
2016-02-24 19:51:01 +01:00
|
|
|
UseColour::YesOrNo colourMode = config
|
|
|
|
? config->useColour()
|
|
|
|
: UseColour::Auto;
|
|
|
|
if( colourMode == UseColour::Auto )
|
2017-05-22 00:41:31 +02:00
|
|
|
colourMode = UseColour::Yes;
|
2016-02-24 19:51:01 +01:00
|
|
|
return colourMode == UseColour::Yes
|
|
|
|
? &s_instance
|
|
|
|
: NoColourImpl::instance();
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2013-03-11 19:38:29 +01:00
|
|
|
|
|
|
|
} // end anon namespace
|
2013-04-05 08:47:36 +02:00
|
|
|
} // end namespace Catch
|
2013-03-11 19:38:29 +01:00
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
#elif defined( CATCH_CONFIG_COLOUR_ANSI ) //////////////////////////////////////
|
2012-10-31 19:04:22 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2012-02-25 21:36:22 +01:00
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
namespace Catch {
|
2013-03-11 19:38:29 +01:00
|
|
|
namespace {
|
2013-04-05 08:47:36 +02:00
|
|
|
|
2013-04-05 08:59:28 +02:00
|
|
|
// use POSIX/ ANSI console terminal codes
|
|
|
|
// Thanks to Adam Strzelecki for original contribution
|
|
|
|
// (http://github.com/nanoant)
|
|
|
|
// https://github.com/philsquared/Catch/pull/131
|
2014-10-21 08:24:30 +02:00
|
|
|
class PosixColourImpl : public IColourImpl {
|
2013-04-05 08:47:36 +02:00
|
|
|
public:
|
2018-07-12 14:27:06 +02:00
|
|
|
void use( Colour::Code _colourCode ) override {
|
2013-04-05 08:47:36 +02:00
|
|
|
switch( _colourCode ) {
|
|
|
|
case Colour::None:
|
|
|
|
case Colour::White: return setColour( "[0m" );
|
|
|
|
case Colour::Red: return setColour( "[0;31m" );
|
|
|
|
case Colour::Green: return setColour( "[0;32m" );
|
2015-06-17 01:17:33 +02:00
|
|
|
case Colour::Blue: return setColour( "[0;34m" );
|
2013-04-05 08:47:36 +02:00
|
|
|
case Colour::Cyan: return setColour( "[0;36m" );
|
|
|
|
case Colour::Yellow: return setColour( "[0;33m" );
|
|
|
|
case Colour::Grey: return setColour( "[1;30m" );
|
|
|
|
|
|
|
|
case Colour::LightGrey: return setColour( "[0;37m" );
|
|
|
|
case Colour::BrightRed: return setColour( "[1;31m" );
|
2013-04-09 20:49:58 +02:00
|
|
|
case Colour::BrightGreen: return setColour( "[1;32m" );
|
2013-04-05 08:47:36 +02:00
|
|
|
case Colour::BrightWhite: return setColour( "[1;37m" );
|
2018-01-26 23:04:54 +01:00
|
|
|
case Colour::BrightYellow: return setColour( "[1;33m" );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-05-05 16:42:57 +02:00
|
|
|
case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" );
|
2018-01-26 23:04:54 +01:00
|
|
|
default: CATCH_INTERNAL_ERROR( "Unknown colour requested" );
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-21 08:24:30 +02:00
|
|
|
static IColourImpl* instance() {
|
|
|
|
static PosixColourImpl s_instance;
|
|
|
|
return &s_instance;
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:14:59 +02:00
|
|
|
private:
|
2013-04-05 08:47:36 +02:00
|
|
|
void setColour( const char* _escapeCode ) {
|
2019-01-19 23:03:25 +01:00
|
|
|
getCurrentContext().getConfig()->stream()
|
2019-01-19 13:29:53 +01:00
|
|
|
<< '\033' << _escapeCode;
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
|
|
|
};
|
2012-02-25 21:36:22 +01:00
|
|
|
|
2017-09-19 16:25:33 +02:00
|
|
|
bool useColourOnPlatform() {
|
|
|
|
return
|
|
|
|
#ifdef CATCH_PLATFORM_MAC
|
|
|
|
!isDebuggerActive() &&
|
|
|
|
#endif
|
2018-03-02 14:39:01 +01:00
|
|
|
#if !(defined(__DJGPP__) && defined(__STRICT_ANSI__))
|
|
|
|
isatty(STDOUT_FILENO)
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
2017-09-19 16:25:33 +02:00
|
|
|
}
|
2014-10-21 08:24:30 +02:00
|
|
|
IColourImpl* platformColourInstance() {
|
2017-03-06 22:07:33 +01:00
|
|
|
ErrnoGuard guard;
|
2017-04-25 21:18:02 +02:00
|
|
|
IConfigPtr config = getCurrentContext().getConfig();
|
2016-02-24 19:51:01 +01:00
|
|
|
UseColour::YesOrNo colourMode = config
|
|
|
|
? config->useColour()
|
|
|
|
: UseColour::Auto;
|
|
|
|
if( colourMode == UseColour::Auto )
|
2017-09-19 16:25:33 +02:00
|
|
|
colourMode = useColourOnPlatform()
|
2016-02-24 19:51:01 +01:00
|
|
|
? UseColour::Yes
|
|
|
|
: UseColour::No;
|
|
|
|
return colourMode == UseColour::Yes
|
2014-10-21 08:24:30 +02:00
|
|
|
? PosixColourImpl::instance()
|
|
|
|
: NoColourImpl::instance();
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
} // end anon namespace
|
|
|
|
} // end namespace Catch
|
2012-10-31 19:04:22 +01:00
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
#else // not Windows or ANSI ///////////////////////////////////////////////
|
2012-10-29 21:46:45 +01:00
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); }
|
2014-04-15 19:44:37 +02:00
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // Windows/ ANSI/ None
|
|
|
|
|
|
|
|
namespace Catch {
|
2013-04-05 08:47:36 +02:00
|
|
|
|
2017-07-20 00:27:28 +02:00
|
|
|
Colour::Colour( Code _colourCode ) { use( _colourCode ); }
|
2018-01-26 23:04:54 +01:00
|
|
|
Colour::Colour( Colour&& rhs ) noexcept {
|
2017-07-25 17:16:28 +02:00
|
|
|
m_moved = rhs.m_moved;
|
|
|
|
rhs.m_moved = true;
|
|
|
|
}
|
|
|
|
Colour& Colour::operator=( Colour&& rhs ) noexcept {
|
|
|
|
m_moved = rhs.m_moved;
|
|
|
|
rhs.m_moved = true;
|
2017-07-20 00:27:28 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2018-01-26 23:04:54 +01:00
|
|
|
|
2014-07-09 08:36:12 +02:00
|
|
|
Colour::~Colour(){ if( !m_moved ) use( None ); }
|
2013-04-05 08:47:36 +02:00
|
|
|
|
2014-10-21 08:24:30 +02:00
|
|
|
void Colour::use( Code _colourCode ) {
|
2016-02-24 19:51:01 +01:00
|
|
|
static IColourImpl* impl = platformColourInstance();
|
2019-05-14 14:14:39 +02:00
|
|
|
// Strictly speaking, this cannot possibly happen.
|
|
|
|
// However, under some conditions it does happen (see #1626),
|
|
|
|
// and this change is small enough that we can let practicality
|
|
|
|
// triumph over purity in this case.
|
|
|
|
if (impl != NULL) {
|
|
|
|
impl->use( _colourCode );
|
|
|
|
}
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2013-04-05 08:47:36 +02:00
|
|
|
|
2017-07-10 14:25:38 +02:00
|
|
|
std::ostream& operator << ( std::ostream& os, Colour const& ) {
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
} // end namespace Catch
|
2017-09-07 16:51:33 +02:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|