mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
Switched TextColour out for Colour
- Removed TextColour
This commit is contained in:
@@ -36,7 +36,7 @@ namespace Catch {
|
||||
BrightWhite = Bright | White,
|
||||
|
||||
// By intention
|
||||
FileName = Grey,
|
||||
FileName = LightGrey,
|
||||
ResultError = BrightRed,
|
||||
ResultSuccess = BrightGreen,
|
||||
|
||||
@@ -46,48 +46,20 @@ namespace Catch {
|
||||
OriginalExpression = Cyan,
|
||||
ReconstructedExpression = Yellow,
|
||||
|
||||
SecondaryText = Grey,
|
||||
SecondaryText = LightGrey,
|
||||
Headers = White
|
||||
};
|
||||
|
||||
// Use constructed object for RAII guard
|
||||
Colour( Code _colourCode );
|
||||
~Colour();
|
||||
|
||||
// Use static method for one-shot changes
|
||||
static void use( Code _colourCode );
|
||||
|
||||
private:
|
||||
static Detail::IColourImpl* impl;
|
||||
};
|
||||
|
||||
struct IConsoleColourCodes : NonCopyable {
|
||||
enum Colours {
|
||||
None,
|
||||
|
||||
FileName,
|
||||
ResultError,
|
||||
ResultSuccess,
|
||||
|
||||
Error,
|
||||
Success,
|
||||
|
||||
OriginalExpression,
|
||||
ReconstructedExpression,
|
||||
|
||||
SecondaryText,
|
||||
Headers
|
||||
};
|
||||
|
||||
virtual void set( Colours colour ) = 0;
|
||||
};
|
||||
|
||||
class TextColour : public IConsoleColourCodes {
|
||||
public:
|
||||
TextColour( Colours colour = None );
|
||||
void set( Colours colour );
|
||||
~TextColour();
|
||||
|
||||
private:
|
||||
IConsoleColourCodes* m_impl;
|
||||
};
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
|
@@ -64,60 +64,10 @@ namespace {
|
||||
WORD originalAttributes;
|
||||
};
|
||||
|
||||
WORD mapConsoleColour( IConsoleColourCodes::Colours colour ) {
|
||||
enum Win32Colours {
|
||||
Grey = FOREGROUND_INTENSITY,
|
||||
BrightRed = FOREGROUND_RED | FOREGROUND_INTENSITY,
|
||||
BrightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
|
||||
BrightWhite = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
||||
DarkGreen = FOREGROUND_GREEN,
|
||||
Cyan = FOREGROUND_BLUE | FOREGROUND_GREEN,
|
||||
Yellow = FOREGROUND_RED | FOREGROUND_GREEN
|
||||
};
|
||||
switch( colour ) {
|
||||
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;
|
||||
case IConsoleColourCodes::OriginalExpression: return Cyan;
|
||||
case IConsoleColourCodes::ReconstructedExpression: return Yellow;
|
||||
case IConsoleColourCodes::SecondaryText: return Grey;
|
||||
case IConsoleColourCodes::Headers: return 0;
|
||||
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;
|
||||
Win32ColourImpl platformColourImpl;
|
||||
|
||||
} // end anon namespace
|
||||
@@ -130,6 +80,10 @@ namespace {
|
||||
namespace Catch {
|
||||
namespace {
|
||||
|
||||
// use POSIX/ ANSI console terminal codes
|
||||
// Thanks to Adam Strzelecki for original contribution
|
||||
// (http://github.com/nanoant)
|
||||
// https://github.com/philsquared/Catch/pull/131
|
||||
class PosixColourImpl : public Detail::IColourImpl {
|
||||
public:
|
||||
PosixColourImpl() {
|
||||
@@ -161,51 +115,10 @@ namespace {
|
||||
}
|
||||
};
|
||||
|
||||
// use POSIX/ ANSI console terminal codes
|
||||
// Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
|
||||
// https://github.com/philsquared/Catch/pull/131
|
||||
|
||||
const char* WhiteOrNormal = "[0m";
|
||||
const char* BrightRed = "[1;31m";
|
||||
const char* BrightGreen = "[1;32m";
|
||||
// const char* BrightWhite = "[1;37m";
|
||||
const char* Green = "[0;32m";
|
||||
const char* Cyan = "[0;36m";
|
||||
const char* Yellow = "[0;33m";
|
||||
const char* LightGrey = "[0;37m";
|
||||
// const char* DarkGrey = "[1;30m";
|
||||
|
||||
struct AnsiConsoleColourCodes : IConsoleColourCodes {
|
||||
|
||||
~AnsiConsoleColourCodes() {
|
||||
set( None );
|
||||
}
|
||||
|
||||
const char* escapeCodeForColour( Colours colour ) {
|
||||
switch( colour ) {
|
||||
case FileName: return WhiteOrNormal;
|
||||
case ResultError: return BrightRed;
|
||||
case ResultSuccess: return BrightGreen;
|
||||
case Error: return BrightRed;
|
||||
case Success: return Green;
|
||||
case OriginalExpression: return Cyan;
|
||||
case ReconstructedExpression: return Yellow;
|
||||
case SecondaryText: return LightGrey;
|
||||
case Headers: return WhiteOrNormal;
|
||||
case None: return WhiteOrNormal;
|
||||
}
|
||||
}
|
||||
|
||||
void set( Colours colour ) {
|
||||
std::cout << '\033' << escapeCodeForColour( colour );
|
||||
}
|
||||
};
|
||||
|
||||
inline bool shouldUseColourForPlatform() {
|
||||
return isatty( fileno(stdout) );
|
||||
}
|
||||
|
||||
typedef AnsiConsoleColourCodes PlatformConsoleColourCodes;
|
||||
PosixColourImpl platformColourImpl;
|
||||
|
||||
} // end anon namespace
|
||||
@@ -213,12 +126,6 @@ namespace {
|
||||
|
||||
#endif // not Windows
|
||||
|
||||
namespace {
|
||||
struct NoConsoleColourCodes : Catch::IConsoleColourCodes {
|
||||
void set( Colours ) {}
|
||||
};
|
||||
}
|
||||
|
||||
namespace Catch {
|
||||
|
||||
namespace {
|
||||
@@ -240,26 +147,6 @@ namespace Catch {
|
||||
? static_cast<Detail::IColourImpl*>( &platformColourImpl )
|
||||
: static_cast<Detail::IColourImpl*>( &noColourImpl );
|
||||
|
||||
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();
|
||||
|
||||
if( colour )
|
||||
set( colour );
|
||||
}
|
||||
|
||||
TextColour::~TextColour() {
|
||||
delete m_impl;
|
||||
}
|
||||
|
||||
void TextColour::set( Colours colour ) {
|
||||
m_impl->set( colour );
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
|
||||
|
@@ -64,25 +64,25 @@ namespace Catch {
|
||||
tagsWrapper.setRight( maxTagLen ).wrap( it->getTestCaseInfo().tagsAsString );
|
||||
|
||||
for( std::size_t i = 0; i < std::max( nameWrapper.size(), tagsWrapper.size() ); ++i ) {
|
||||
TextColour::Colours colour = TextColour::None;
|
||||
Colour::Code colour = Colour::None;
|
||||
if( it->getTestCaseInfo().isHidden )
|
||||
colour = TextColour::SecondaryText;
|
||||
colour = Colour::SecondaryText;
|
||||
std::string nameCol;
|
||||
if( i < nameWrapper.size() ) {
|
||||
nameCol = nameWrapper[i];
|
||||
}
|
||||
else {
|
||||
nameCol = " ...";
|
||||
colour = TextColour::SecondaryText;
|
||||
colour = Colour::SecondaryText;
|
||||
}
|
||||
|
||||
{
|
||||
TextColour colourGuard( colour );
|
||||
Colour colourGuard( colour );
|
||||
std::cout << nameCol;
|
||||
}
|
||||
if( i < tagsWrapper.size() && !tagsWrapper[i].empty() ) {
|
||||
if( i == 0 ) {
|
||||
TextColour colourGuard( TextColour::SecondaryText );
|
||||
Colour colourGuard( Colour::SecondaryText );
|
||||
std::cout << " " << std::string( maxNameLen - nameCol.size(), '.' ) << " ";
|
||||
}
|
||||
else {
|
||||
@@ -143,7 +143,7 @@ namespace Catch {
|
||||
if( maxTagLen > wrapper.last().size() )
|
||||
dots += maxTagLen - wrapper.last().size();
|
||||
{
|
||||
TextColour colourGuard( TextColour::SecondaryText );
|
||||
Colour colourGuard( Colour::SecondaryText );
|
||||
std::cout << std::string( dots, '.' );
|
||||
}
|
||||
std::cout << countIt->second
|
||||
|
Reference in New Issue
Block a user