Skip to content

Commit

Permalink
Refactored colour handling
Browse files Browse the repository at this point in the history
* POSIX colour impl is now compiled for all platforms.
* Deciding whether a colour impl should be picked is now stream
  dependent, and thus incompatible implementations can be removed
  immediately, rather than checking when the colour is being used.
  • Loading branch information
horenmar committed Mar 18, 2022
1 parent c1c72c7 commit 0e176c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
54 changes: 31 additions & 23 deletions src/catch2/internal/catch_console_colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ namespace {
originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
}

static bool useColourOnPlatform() { return true; }
static bool useColourOnPlatform(IStream const& stream) {
// Win32 text colour APIs can only be used on console streams
// We cannot check that the output hasn't been redirected,
// so we just check that the original stream is console stream.
return stream.isStdout();
}

private:
void use( Colour::Code _colourCode ) const override {
// Early exit if we are not writing to the console, because
// Win32 API can only change colour of the console.
if ( !m_stream->isStdout() ) {
return;
}

switch( _colourCode ) {
case Colour::None: return setTextAttribute( originalForegroundAttributes );
case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE );
Expand Down Expand Up @@ -138,9 +137,13 @@ namespace {
} // end anon namespace
} // end namespace Catch

#elif defined( CATCH_CONFIG_COLOUR_ANSI ) //////////////////////////////////////
#endif // Windows/ ANSI/ None

#include <unistd.h>

#if defined( CATCH_PLATFORM_LINUX ) || defined( CATCH_PLATFORM_MAC )
# define CATCH_INTERNAL_HAS_ISATTY
# include <unistd.h>
#endif

namespace Catch {
namespace {
Expand All @@ -153,18 +156,25 @@ namespace {
public:
PosixColourImpl( IStream const* stream ): ColourImpl( stream ) {}

static bool useColourOnPlatform() {
static bool useColourOnPlatform(IStream const& stream) {
// This is kinda messy due to trying to support a bunch of
// different platforms at once.
// The basic idea is that if we are asked to do autodetection (as
// opposed to being told to use posixy colours outright), then we
// only want to use the colours if we are writing to console.
// However, console might be redirected, so we make an attempt at
// checking for that on platforms where we know how to do that.
bool useColour = stream.isStdout();
#if defined( CATCH_INTERNAL_HAS_ISATTY ) && \
!( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) )
ErrnoGuard _; // for isatty
return
# if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE )
!isDebuggerActive() &&
useColour = useColour && isatty( STDOUT_FILENO );
# endif
# if !( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) )
isatty( STDOUT_FILENO )
# else
false
# if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE )
useColour = useColour && !isDebuggerActive();
# endif
;

return useColour;
}

private:
Expand Down Expand Up @@ -201,8 +211,6 @@ namespace {
} // end anon namespace
} // end namespace Catch

#endif // Windows/ ANSI/ None

namespace Catch {

Detail::unique_ptr<ColourImpl> makeColourImpl(IConfig const* config, IStream const* stream) {
Expand All @@ -220,11 +228,11 @@ namespace Catch {
if ( colourMode == UseColour::Auto ) {
createPlatformInstance =
#if defined( CATCH_CONFIG_COLOUR_ANSI )
PosixColourImpl::useColourOnPlatform()
PosixColourImpl::useColourOnPlatform( *stream )
#elif defined( CATCH_CONFIG_COLOUR_WINDOWS )
Win32ColourImpl::useColourOnPlatform()
Win32ColourImpl::useColourOnPlatform( *stream )
#else
NoColourImpl::useColourOnPlatform()
false
#endif
;
}
Expand Down
4 changes: 2 additions & 2 deletions src/catch2/reporters/catch_reporter_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,8 @@ void ConsoleReporter::printSummaryRow(StringRef label, std::vector<SummaryColumn
<< "- none -";
}
} else if (value != "0") {
m_stream << m_colour->startColour( Colour::LightGrey ) << " | "
<< m_colour->startColour( col.colour ) << value << ' '
m_stream << m_colour->startColour( Colour::LightGrey ) << " | ";
m_stream << m_colour->startColour( col.colour ) << value << ' '
<< col.label;
}
}
Expand Down

0 comments on commit 0e176c3

Please sign in to comment.