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-03-11 19:38:29 +01:00
|
|
|
#if defined ( CATCH_PLATFORM_WINDOWS ) /////////////////////////////////////////
|
2013-03-04 12:19:15 +01:00
|
|
|
|
2013-06-29 21:31:17 +02:00
|
|
|
#ifndef NOMINMAX
|
2013-06-14 20:18:13 +02:00
|
|
|
#define NOMINMAX
|
2013-06-29 21:31:17 +02:00
|
|
|
#endif
|
2013-07-01 19:44:40 +02:00
|
|
|
|
|
|
|
#ifdef __AFXDLL
|
|
|
|
#include <AfxWin.h>
|
|
|
|
#else
|
2013-03-11 19:38:29 +01:00
|
|
|
#include <windows.h>
|
2013-07-01 19:44:40 +02:00
|
|
|
#endif
|
2013-03-11 19:38:29 +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
|
|
|
|
2013-12-09 15:18:35 +01:00
|
|
|
class Win32ColourImpl {
|
2013-04-05 08:47:36 +02:00
|
|
|
public:
|
|
|
|
Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) )
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo );
|
|
|
|
originalAttributes = csbiInfo.wAttributes;
|
|
|
|
}
|
2013-04-05 21:55:57 +02:00
|
|
|
|
2013-12-09 15:18:35 +01:00
|
|
|
void use( Colour::Code _colourCode ) {
|
2013-04-05 08:47:36 +02:00
|
|
|
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 );
|
2013-06-14 20:18:13 +02:00
|
|
|
case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE );
|
2013-04-05 08:47:36 +02:00
|
|
|
|
|
|
|
case Colour::Bright: throw std::logic_error( "not a colour" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:14:59 +02:00
|
|
|
private:
|
2013-04-05 08:47:36 +02:00
|
|
|
void setTextAttribute( WORD _textAttribute ) {
|
|
|
|
SetConsoleTextAttribute( stdoutHandle, _textAttribute );
|
|
|
|
}
|
|
|
|
HANDLE stdoutHandle;
|
|
|
|
WORD originalAttributes;
|
|
|
|
};
|
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
inline bool shouldUseColourForPlatform() {
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-12-09 15:18:35 +01:00
|
|
|
typedef 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
|
|
|
|
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
|
2013-12-09 15:18:35 +01:00
|
|
|
class PosixColourImpl {
|
2013-04-05 08:47:36 +02:00
|
|
|
public:
|
2013-12-09 15:18:35 +01:00
|
|
|
void use( Colour::Code _colourCode ) {
|
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" );
|
|
|
|
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" );
|
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" );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-05 08:47:36 +02:00
|
|
|
case Colour::Bright: throw std::logic_error( "not a colour" );
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
private:
|
2013-04-05 08:47:36 +02:00
|
|
|
void setColour( const char* _escapeCode ) {
|
|
|
|
std::cout << '\033' << _escapeCode;
|
|
|
|
}
|
|
|
|
};
|
2012-02-25 21:36:22 +01:00
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
inline bool shouldUseColourForPlatform() {
|
2013-11-26 19:37:37 +01:00
|
|
|
return isatty(STDOUT_FILENO);
|
2013-03-11 19:38:29 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-12-09 15:18:35 +01:00
|
|
|
typedef PosixColourImpl PlatformColourImpl;
|
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
|
|
|
|
2013-03-11 19:38:29 +01:00
|
|
|
#endif // not Windows
|
2012-10-29 21:46:45 +01:00
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
2013-12-09 15:18:35 +01:00
|
|
|
template <typename Impl>
|
|
|
|
struct ColourChange
|
|
|
|
{
|
|
|
|
static Impl impl;
|
|
|
|
static const bool shouldUseColour;
|
|
|
|
};
|
|
|
|
template <typename Impl>
|
|
|
|
Impl ColourChange<Impl>::impl;
|
|
|
|
template <typename Impl>
|
|
|
|
const bool ColourChange<Impl>::shouldUseColour = shouldUseColourForPlatform() &&
|
|
|
|
!isDebuggerActive();;
|
|
|
|
|
|
|
|
INTERNAL_CATCH_INLINE Colour::Colour( Code _colourCode ) {
|
|
|
|
if( ColourChange<PlatformColourImpl>::shouldUseColour ) ColourChange<PlatformColourImpl>::impl.use( _colourCode );
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
2013-12-09 15:18:35 +01:00
|
|
|
INTERNAL_CATCH_INLINE Colour::~Colour() {
|
|
|
|
if( ColourChange<PlatformColourImpl>::shouldUseColour ) ColourChange<PlatformColourImpl>::impl.use( Colour::None );
|
2013-04-05 08:47:36 +02:00
|
|
|
}
|
|
|
|
|
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
|