Skip to content

Optin to VT100 style terminal codes on Windows #3004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 10 additions & 30 deletions src/catch2/internal/catch_console_colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ namespace Catch {

} // namespace Catch


#if defined ( CATCH_CONFIG_COLOUR_WIN32 ) /////////////////////////////////////////

namespace Catch {
Expand All @@ -115,10 +114,13 @@ namespace {
}

static bool useImplementationForStream(IStream const& stream) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might just kick this one out of the auto-selection. Even the version check here is wonky, because it's easily thrown off guard by manifests or compatibility mode.

// 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.isConsole();
OSVERSIONINFOA versionInfo;
// Use as fallback for Windows versions <10.0 only.
// Newer Windows versions have full support for ANSI color codes.
if ( GetVersionExA( &versionInfo ) && versionInfo.dwMajorVersion < 10 ) {
return stream.isConsole();
}
return false;
}

private:
Expand Down Expand Up @@ -160,38 +162,16 @@ namespace {

#endif // Windows/ ANSI/ None


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

namespace Catch {
namespace {

class ANSIColourImpl final : public ColourImpl {
public:
ANSIColourImpl( IStream* stream ): ColourImpl( stream ) {}
ANSIColourImpl( IStream* stream ): ColourImpl( stream ) {
}

static bool useImplementationForStream(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.isConsole();
#if defined( CATCH_INTERNAL_HAS_ISATTY ) && \
!( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) )
ErrnoGuard _; // for isatty
useColour = useColour && isatty( STDOUT_FILENO );
# endif
# if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE )
useColour = useColour && !isDebuggerActive();
# endif

return useColour;
return stream.isConsole();
}

private:
Expand Down
75 changes: 67 additions & 8 deletions src/catch2/internal/catch_istream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@

// SPDX-License-Identifier: BSL-1.0

#include <catch2/internal/catch_istream.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_debug_console.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_errno_guard.hpp>
#include <catch2/internal/catch_istream.hpp>
#include <catch2/internal/catch_stdstreams.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>
#include <catch2/internal/catch_windows_h_proxy.hpp>

#include <cstdio>
#include <fstream>

#if defined( CATCH_PLATFORM_LINUX ) || defined( CATCH_PLATFORM_MAC ) || \
defined( __GLIBC__ )
# define CATCH_INTERNAL_HAS_ISATTY
# include <unistd.h>
#elif defined( CATCH_PLATFORM_WINDOWS )
# include <io.h>
#endif

namespace Catch {

Catch::IStream::~IStream() = default;
Expand Down Expand Up @@ -86,29 +96,78 @@ namespace Detail {

///////////////////////////////////////////////////////////////////////////

#if defined( CATCH_PLATFORM_WINDOWS )
bool enableVirtualTerminalSupport( DWORD stdHandle ) {
HANDLE outputHandle = GetStdHandle( stdHandle );
DWORD mode = 0;
const DWORD requiredMode = ENABLE_PROCESSED_OUTPUT |
ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if ( GetConsoleMode( outputHandle, &mode ) &&
( mode & requiredMode ) == requiredMode ) {
// VT100 style sequence processing has to be enabled by
// explicit opt-in.
const DWORD newMode = mode | requiredMode;
if( SetConsoleMode( outputHandle, newMode ) )
{
return true;
}
// Restore fail-safe state.
SetConsoleMode( outputHandle, mode );
}
return false;
}
#endif

class CoutStream final : public IStream {
std::ostream m_os;
bool m_isatty;
public:
// Store the streambuf from cout up-front because
// cout may get redirected when running tests
CoutStream() : m_os( Catch::cout().rdbuf() ) {}
CoutStream() : m_os( Catch::cout().rdbuf() ) {
m_isatty = true;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much Copy&Paste for my taste - and since Catch::cout() can be reimplemented, this is quite possibly probing the wrong file handles...

#if defined( CATCH_INTERNAL_HAS_ISATTY ) && \
!( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) )
ErrnoGuard _; // for isatty
m_isatty = m_isatty && isatty( STDOUT_FILENO );
#elif defined( CATCH_PLATFORM_WINDOWS )
m_isatty = m_isatty && _isatty( _fileno( stdout ) );
m_isatty = m_isatty && enableVirtualTerminalSupport( STD_OUTPUT_HANDLE );
#endif
#if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE )
m_isatty = m_isatty && !isDebuggerActive();
#endif
}

public: // IStream
std::ostream& stream() override { return m_os; }
bool isConsole() const override { return true; }
bool isConsole() const override { return m_isatty; }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not pass the tests. Now isConsole reports false in all of the unit tests when run in the CI - but its doing so rightfully.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two options - injecting a proper terminal emulator for testing or changing the expected results?

};

class CerrStream : public IStream {
std::ostream m_os;

bool m_isatty;
public:
// Store the streambuf from cerr up-front because
// cout may get redirected when running tests
CerrStream(): m_os( Catch::cerr().rdbuf() ) {}
CerrStream(): m_os( Catch::cerr().rdbuf() ) {
m_isatty = true;
#if defined( CATCH_INTERNAL_HAS_ISATTY ) && \
!( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) )
ErrnoGuard _; // for isatty
m_isatty = m_isatty && isatty( STDERR_FILENO );
#elif defined( CATCH_PLATFORM_WINDOWS )
m_isatty = m_isatty && _isatty( _fileno( stderr ) );
m_isatty = m_isatty && enableVirtualTerminalSupport( STD_ERROR_HANDLE );
#endif
#if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE )
m_isatty = m_isatty && !isDebuggerActive();
Copy link
Author

@Ext3h Ext3h Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy&Paste from original ANSIColourImpl code - unfortunately no comments, so what is this supposed to do? Is the debugger specifically not compatible with VT100 style sequences? Or does it not behave like a console in some other sense?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it even apply to stderr or is stderr not usable with debuggers on Mac/Iphone?

#endif
}

public: // IStream
std::ostream& stream() override { return m_os; }
bool isConsole() const override { return true; }
bool isConsole() const override { return m_isatty; }
};

///////////////////////////////////////////////////////////////////////////
Expand Down