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)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_console_colour.hpp"
|
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
namespace Catch { namespace Detail {
|
|
|
|
struct IColourImpl {
|
|
|
|
virtual ~IColourImpl() {}
|
|
|
|
virtual void use( Colour::Code _colourCode ) = 0;
|
|
|
|
};
|
|
|
|
}}
|
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
#if defined ( CATCH_PLATFORM_WINDOWS ) /////////////////////////////////////////
|
2013-03-04 12:19:15 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
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
|
|
|
class Win32ColourImpl : public Detail::IColourImpl {
|
|
|
|
public:
|
|
|
|
Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) )
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo );
|
|
|
|
originalAttributes = csbiInfo.wAttributes;
|
|
|
|
}
|
|
|
|
~Win32ColourImpl() {
|
|
|
|
use( Colour::None );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void use( Colour::Code _colourCode ) {
|
|
|
|
switch( _colourCode ) {
|
|
|
|
case Colour::None: return setTextAttribute( originalAttributes );
|
|
|
|
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 );
|
|
|
|
case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE );
|
|
|
|
|
|
|
|
case Colour::Bright: throw std::logic_error( "not a colour" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setTextAttribute( WORD _textAttribute ) {
|
|
|
|
SetConsoleTextAttribute( stdoutHandle, _textAttribute );
|
|
|
|
}
|
|
|
|
HANDLE stdoutHandle;
|
|
|
|
WORD originalAttributes;
|
|
|
|
};
|
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
WORD mapConsoleColour( IConsoleColourCodes::Colours colour ) {
|
2013-03-29 22:39:43 +01:00
|
|
|
enum Win32Colours {
|
|
|
|
Grey = FOREGROUND_INTENSITY,
|
|
|
|
BrightRed = FOREGROUND_RED | FOREGROUND_INTENSITY,
|
|
|
|
BrightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
|
2013-04-01 12:25:54 +02:00
|
|
|
BrightWhite = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
2013-03-29 22:39:43 +01:00
|
|
|
DarkGreen = FOREGROUND_GREEN,
|
2013-04-01 12:25:54 +02:00
|
|
|
Cyan = FOREGROUND_BLUE | FOREGROUND_GREEN,
|
2013-03-29 22:39:43 +01:00
|
|
|
Yellow = FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
};
|
2013-03-11 19:38:29 +01:00
|
|
|
switch( colour ) {
|
2013-03-29 22:39:43 +01:00
|
|
|
case IConsoleColourCodes::FileName: return Grey;
|
|
|
|
case IConsoleColourCodes::ResultError: return BrightRed;
|
|
|
|
case IConsoleColourCodes::ResultSuccess: return BrightGreen;
|
|
|
|
case IConsoleColourCodes::Error: return BrightRed;
|
|
|
|
case IConsoleColourCodes::Success: return DarkGreen;
|
2013-04-01 12:25:54 +02:00
|
|
|
case IConsoleColourCodes::OriginalExpression: return Cyan;
|
2013-03-29 22:39:43 +01:00
|
|
|
case IConsoleColourCodes::ReconstructedExpression: return Yellow;
|
|
|
|
case IConsoleColourCodes::SecondaryText: return Grey;
|
2013-04-01 12:25:54 +02:00
|
|
|
case IConsoleColourCodes::Headers: return 0;
|
2013-03-11 19:38:29 +01:00
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WindowsConsoleColourCodes : IConsoleColourCodes {
|
|
|
|
|
|
|
|
WindowsConsoleColourCodes()
|
|
|
|
: hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ),
|
|
|
|
wOldColorAttrs( 0 )
|
|
|
|
{
|
|
|
|
GetConsoleScreenBufferInfo( hStdout, &csbiInfo );
|
|
|
|
wOldColorAttrs = csbiInfo.wAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
~WindowsConsoleColourCodes() {
|
|
|
|
SetConsoleTextAttribute( hStdout, wOldColorAttrs );
|
|
|
|
}
|
|
|
|
|
|
|
|
void set( Colours colour ) {
|
|
|
|
WORD consoleColour = mapConsoleColour( colour );
|
|
|
|
if( consoleColour > 0 )
|
|
|
|
SetConsoleTextAttribute( hStdout, consoleColour );
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hStdout;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
WORD wOldColorAttrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool shouldUseColourForPlatform() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef WindowsConsoleColourCodes PlatformConsoleColourCodes;
|
2013-04-05 08:47:36 +02:00
|
|
|
Win32ColourImpl platformColourImpl;
|
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
|
|
|
|
|
|
|
#else // Not Windows - assumed to be POSIX compatible //////////////////////////
|
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
|
|
|
|
|
|
|
class PosixColourImpl : public Detail::IColourImpl {
|
|
|
|
public:
|
|
|
|
PosixColourImpl() {
|
|
|
|
use( Colour::None );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void use( Colour::Code _colourCode ) {
|
|
|
|
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" );
|
|
|
|
case Colour::Blue: return setColour( "[0:34m" );
|
|
|
|
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" );
|
|
|
|
case Colour::BrightGreen: return setColour( "[1;33m" );
|
|
|
|
case Colour::BrightWhite: return setColour( "[1;37m" );
|
|
|
|
|
|
|
|
case Colour::Bright: throw std::logic_error( "not a colour" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void setColour( const char* _escapeCode ) {
|
|
|
|
std::cout << '\033' << _escapeCode;
|
|
|
|
}
|
|
|
|
};
|
2012-02-25 21:36:22 +01:00
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
// use POSIX/ ANSI console terminal codes
|
2012-10-29 21:46:45 +01:00
|
|
|
// Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
|
|
|
|
// https://github.com/philsquared/Catch/pull/131
|
2013-03-29 22:39:43 +01:00
|
|
|
|
|
|
|
const char* WhiteOrNormal = "[0m";
|
2013-04-01 12:25:54 +02:00
|
|
|
const char* BrightRed = "[1;31m";
|
|
|
|
const char* BrightGreen = "[1;32m";
|
|
|
|
// const char* BrightWhite = "[1;37m";
|
2013-03-29 22:39:43 +01:00
|
|
|
const char* Green = "[0;32m";
|
|
|
|
const char* Cyan = "[0;36m";
|
|
|
|
const char* Yellow = "[0;33m";
|
|
|
|
const char* LightGrey = "[0;37m";
|
2013-03-29 22:55:19 +01:00
|
|
|
// const char* DarkGrey = "[1;30m";
|
2013-03-29 22:39:43 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
struct AnsiConsoleColourCodes : IConsoleColourCodes {
|
2012-10-29 21:46:45 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
~AnsiConsoleColourCodes() {
|
|
|
|
set( None );
|
|
|
|
}
|
2012-10-31 19:04:22 +01:00
|
|
|
|
2013-03-29 22:39:43 +01:00
|
|
|
const char* escapeCodeForColour( Colours colour ) {
|
2012-10-29 21:46:45 +01:00
|
|
|
switch( colour ) {
|
2013-03-29 22:39:43 +01:00
|
|
|
case FileName: return WhiteOrNormal;
|
2013-04-01 12:25:54 +02:00
|
|
|
case ResultError: return BrightRed;
|
|
|
|
case ResultSuccess: return BrightGreen;
|
|
|
|
case Error: return BrightRed;
|
2013-03-29 22:39:43 +01:00
|
|
|
case Success: return Green;
|
|
|
|
case OriginalExpression: return Cyan;
|
|
|
|
case ReconstructedExpression: return Yellow;
|
|
|
|
case SecondaryText: return LightGrey;
|
2013-04-01 12:25:54 +02:00
|
|
|
case Headers: return WhiteOrNormal;
|
2013-03-29 22:39:43 +01:00
|
|
|
case None: return WhiteOrNormal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set( Colours colour ) {
|
|
|
|
std::cout << '\033' << escapeCodeForColour( colour );
|
2012-10-29 21:46:45 +01:00
|
|
|
}
|
2013-03-11 19:38:29 +01:00
|
|
|
};
|
2012-10-31 19:04:22 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
inline bool shouldUseColourForPlatform() {
|
|
|
|
return isatty( fileno(stdout) );
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef AnsiConsoleColourCodes PlatformConsoleColourCodes;
|
2013-04-05 08:47:36 +02:00
|
|
|
PosixColourImpl platformColourImpl;
|
2013-03-11 19:38:29 +01:00
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
} // end anon namespace
|
|
|
|
} // end namespace Catch
|
2012-10-31 19:04:22 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
#endif // not Windows
|
2012-10-29 21:46:45 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
namespace {
|
2013-04-05 08:47:36 +02:00
|
|
|
struct NoConsoleColourCodes : Catch::IConsoleColourCodes {
|
2013-03-11 19:38:29 +01:00
|
|
|
void set( Colours ) {}
|
|
|
|
};
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
namespace {
|
|
|
|
struct NoColourImpl : Detail::IColourImpl {
|
|
|
|
void use( Colour::Code ) {}
|
|
|
|
};
|
|
|
|
NoColourImpl noColourImpl;
|
|
|
|
static const bool shouldUseColour = shouldUseColourForPlatform() &&
|
|
|
|
!isDebuggerActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
Colour::Colour( Code _colourCode ){ use( _colourCode ); }
|
|
|
|
Colour::~Colour(){ use( None ); }
|
|
|
|
void Colour::use( Code _colourCode ) {
|
|
|
|
impl->use( _colourCode );
|
|
|
|
}
|
|
|
|
|
|
|
|
Detail::IColourImpl* Colour::impl = shouldUseColour
|
|
|
|
? static_cast<Detail::IColourImpl*>( &platformColourImpl )
|
|
|
|
: static_cast<Detail::IColourImpl*>( &noColourImpl );
|
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
TextColour::TextColour( Colours colour ) : m_impl( NULL ) {
|
|
|
|
static bool s_shouldUseColour = shouldUseColourForPlatform() &&
|
|
|
|
!isDebuggerActive();
|
|
|
|
if( s_shouldUseColour )
|
|
|
|
m_impl = new PlatformConsoleColourCodes();
|
|
|
|
else
|
|
|
|
m_impl = new NoConsoleColourCodes();
|
|
|
|
|
2012-02-25 21:36:22 +01:00
|
|
|
if( colour )
|
2013-03-11 19:38:29 +01:00
|
|
|
set( colour );
|
2012-02-25 21:36:22 +01:00
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
|
|
|
|
TextColour::~TextColour() {
|
2012-02-25 21:36:22 +01:00
|
|
|
delete m_impl;
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
|
|
|
|
void TextColour::set( Colours colour ) {
|
2012-02-25 21:36:22 +01:00
|
|
|
m_impl->set( colour );
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2012-02-25 21:36:22 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
|