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-02-19 20:59:04 +01:00
|
|
|
#if defined( CATCH_CONFIG_USE_ANSI_COLOUR_CODES )
|
2012-10-31 19:04:22 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2012-02-25 21:36:22 +01:00
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace Catch {
|
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
|
|
|
|
|
|
|
|
TextColour::TextColour( Colours colour ) {
|
|
|
|
if( colour )
|
|
|
|
set( colour );
|
|
|
|
}
|
|
|
|
|
|
|
|
TextColour::~TextColour() {
|
|
|
|
set( TextColour::None );
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace { const char colourEscape = '\033'; }
|
|
|
|
|
2012-10-29 21:46:45 +01:00
|
|
|
void TextColour::set( Colours colour ) {
|
2013-02-18 14:02:49 +01:00
|
|
|
if( isatty( fileno(stdout) ) && !isDebuggerActive() ) {
|
2012-10-29 21:46:45 +01:00
|
|
|
switch( colour ) {
|
|
|
|
case TextColour::FileName:
|
2012-11-01 09:16:15 +01:00
|
|
|
std::cout << colourEscape << "[0m"; // white/ normal
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::ResultError:
|
2012-10-31 19:04:22 +01:00
|
|
|
std::cout << colourEscape << "[1;31m"; // bold red
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::ResultSuccess:
|
2012-10-31 19:04:22 +01:00
|
|
|
std::cout << colourEscape << "[1;32m"; // bold green
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::Error:
|
2012-10-31 19:04:22 +01:00
|
|
|
std::cout << colourEscape << "[0;31m"; // red
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::Success:
|
2012-10-31 19:04:22 +01:00
|
|
|
std::cout << colourEscape << "[0;32m"; // green
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::OriginalExpression:
|
2012-10-31 19:04:22 +01:00
|
|
|
std::cout << colourEscape << "[0;36m"; // cyan
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::ReconstructedExpression:
|
2012-10-31 19:04:22 +01:00
|
|
|
std::cout << colourEscape << "[0;33m"; // yellow
|
2012-10-29 21:46:45 +01:00
|
|
|
break;
|
|
|
|
case TextColour::None:
|
2012-11-01 09:16:15 +01:00
|
|
|
std::cout << colourEscape << "[0m"; // reset
|
2012-10-29 21:46:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-31 19:04:22 +01:00
|
|
|
|
|
|
|
} // namespace Catch
|
|
|
|
|
2012-10-29 21:46:45 +01:00
|
|
|
#elif defined ( CATCH_PLATFORM_WINDOWS )
|
|
|
|
|
|
|
|
#include <windows.h>
|
2012-05-16 09:02:20 +02:00
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
WORD mapConsoleColour( TextColour::Colours colour ) {
|
|
|
|
switch( colour ) {
|
2012-02-25 21:36:22 +01:00
|
|
|
case TextColour::FileName:
|
|
|
|
return FOREGROUND_INTENSITY; // greyed out
|
|
|
|
case TextColour::ResultError:
|
|
|
|
return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red
|
|
|
|
case TextColour::ResultSuccess:
|
|
|
|
return FOREGROUND_GREEN | FOREGROUND_INTENSITY; // bright green
|
|
|
|
case TextColour::Error:
|
|
|
|
return FOREGROUND_RED; // dark red
|
|
|
|
case TextColour::Success:
|
|
|
|
return FOREGROUND_GREEN; // dark green
|
|
|
|
case TextColour::OriginalExpression:
|
|
|
|
return FOREGROUND_BLUE | FOREGROUND_GREEN; // turquoise
|
|
|
|
case TextColour::ReconstructedExpression:
|
|
|
|
return FOREGROUND_RED | FOREGROUND_GREEN; // greeny-yellow
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
struct ConsoleColourImpl {
|
|
|
|
|
2012-02-25 21:36:22 +01:00
|
|
|
ConsoleColourImpl()
|
|
|
|
: hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ),
|
2012-02-27 20:03:54 +01:00
|
|
|
wOldColorAttrs( 0 )
|
2012-02-25 21:36:22 +01:00
|
|
|
{
|
|
|
|
GetConsoleScreenBufferInfo( hStdout, &csbiInfo );
|
|
|
|
wOldColorAttrs = csbiInfo.wAttributes;
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
|
|
|
|
~ConsoleColourImpl() {
|
2012-02-25 21:36:22 +01:00
|
|
|
SetConsoleTextAttribute( hStdout, wOldColorAttrs );
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
|
|
|
|
void set( TextColour::Colours colour ) {
|
2012-02-25 21:36:22 +01:00
|
|
|
WORD consoleColour = mapConsoleColour( colour );
|
|
|
|
if( consoleColour > 0 )
|
|
|
|
SetConsoleTextAttribute( hStdout, consoleColour );
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hStdout;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
WORD wOldColorAttrs;
|
|
|
|
};
|
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
TextColour::TextColour( Colours colour )
|
|
|
|
: m_impl( new ConsoleColourImpl() )
|
2012-02-25 21:36:22 +01:00
|
|
|
{
|
|
|
|
if( colour )
|
|
|
|
m_impl->set( colour );
|
|
|
|
}
|
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
|
|
|
#else
|
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
2012-02-25 21:36:22 +01:00
|
|
|
TextColour::TextColour( Colours ){}
|
|
|
|
TextColour::~TextColour(){}
|
|
|
|
void TextColour::set( Colours ){}
|
|
|
|
|
2012-10-29 21:46:45 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2012-10-31 19:04:22 +01:00
|
|
|
#endif
|
|
|
|
|
2012-02-25 21:36:22 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
|