# ctest enabling colored output on windows?

**URL:** https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696
**Category:** Usage
**Tags:** os:windows
**Created:** [March 16, 2023, 9:22pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696 "2023-03-16T21:22:06Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![serge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@serge](https://discourse.cmake.org/u/serge)
#### Post date: [March 16, 2023, 9:22pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/1 "2023-03-16T21:22:06Z")

</div>

Hello guys,

using ctest on Linux and the test output appeared according to the colors being set by the test. This does not happen on Windows, getting the color codes as plain text.  
I can see here: [https://gitlab.kitware.com/cmake/cmake/-/merge\_requests/2577](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/2577)  
that Sylvain Joubert writing on this same issue:  
`I have been conservative and disabled color on Windows as I can't test on that platform. Are ANSI code supported on Windows or is there another way to output color to the console?`

Is this feature disabled on windows?

Thanks,  
Serge

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 17, 2023, 12:51am UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/2 "2023-03-17T00:51:52Z")

</div>

Looks like the current code still doesn’t support coloured output on Windows:

```cpp
bool cmCTest::ColoredOutputSupportedByConsole()
{
#if defined(_WIN32)
  // Not supported on Windows
  return false;
#else
  // On UNIX we need a non-dumb tty.
  // ...<implementation omitted>
  return ConsoleIsNotDumb();
#endif
}

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 17, 2023, 4:32pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/3 "2023-03-17T16:32:14Z")

</div>

Part of this is likely that (AFAIK), colors are in-band on VT-alikes and out-of-band on Windows terminals. So color can be managed directly by the data stream on VT but windows requires calls to the terminal handle. Of course, maybe the new terminal understands ANSI color sequences, but I’ve not encountered the need yet either.

---

<div class="post-metadata">

### Author: ![serge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@serge](https://discourse.cmake.org/u/serge)
#### Post date: [March 18, 2023, 1:06pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/4 "2023-03-18T13:06:19Z")

</div>

Thanks Ben for the clarifications,  
are there any plan to support Windows and can you think on any possible workaround to use for now?

Thanks,  
Serge

---

<div class="post-metadata">

### Author: ![serge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@serge](https://discourse.cmake.org/u/serge)
#### Post date: [March 19, 2023, 2:29pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/5 "2023-03-19T14:29:43Z")

</div>

Answering to myself 🙂  
Also thanking Craig as well for pointing to the code culprit.

Using mingw console on Windows and I can see the colors back, so this will be our workaround for now.  
Also it surprised me to see that even though Windows and despite the above disabling code it still can work and show colors.

Thanks,  
Serge

---

<div class="post-metadata">

### Author: ![fredemmott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fredemmott/32/4835_2.png) [@fredemmott](https://discourse.cmake.org/u/fredemmott)
#### Post date: [August 6, 2024, 11:47am UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/6 "2024-08-06T11:47:32Z")

</div>

It used to be true that color escape sequences weren’t supported by Windows, but this hasn’t been the case for a few years:

[Console Virtual Terminal Sequences - Windows Console | Microsoft Learn](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting) matches the common unix escape sequences, and this is now _Microsoft’s recommended approach_:

- the documentation for the old out-of-band method - `SetConsoleTextAttributesA()`- explicitly deprecates it, and recommends escape sequences instead. No link because this discourse gives me a 2-link limit.
- a [longer Microsoft article](https://learn.microsoft.com/en-us/windows/console/classic-vs-vt) notes the same with much more detail

It’s possible to check support at runtime by seeing if `GetConsoleMode()` flags include both `ENABLE_PROCESSED_OUTPUT` and `ENABLE_VIRTUAL_TERMINAL_PROCESSING `

For example, ctest works fine in Microsoft’s new-ish ‘Windows Terminal’ app if I set `$Env:CLICOLOR_FORCE=1`

---

<div class="post-metadata">

### Author: ![phetdam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phetdam/32/4846_2.png) [@phetdam](https://discourse.cmake.org/u/phetdam)
#### Post date: [August 13, 2024, 4:41am UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/7 "2024-08-13T04:41:12Z")

</div>

Hi,

I think in general there’s probably two things that need to be done here to enable color output for Windows shells (including the venerable CMD shell). As Fred mentioned it’s possible to use `GetConsoleMode()` to retrieve flags for the `stdout` handle, which can be retrieved using `GetStdHandle(STD_OUTPUT_HANDLE)`. Testing the flags can be done in `cmCTest::ColoredOutputSupportedByConsole()` to determine if virtual terminal sequences are supported so we can have our nice color text printed.

The problem is that for Windows CMD, virtual terminal sequence processing is disabled by default, so a call to `SetConsoleMode` with `ENABLE_VIRTUAL_TERMINAL_PROCESSING` as one of the flags is necessary (maybe when CMake starts up?) in order to enable processing.

For exposition, here’s one way to modify `cmCTest::ColoredOutputSupportedByConsole()`:

```cpp
// note: minimal error checking; call GetLastError() for error code.
// or we could just print warnings to stderr, that works too?
bool cmCTest::ColoredOutputSupportedByConsole()
{
  // ... env checking
#if defined(_WIN32)
  // stdout handle
  auto out_hnd = GetStdHandle(STD_OUTPUT_HANDLE);
  // if no stdout or error then not supported
  if (!out_hnd || out_hnd == INVALID_HANDLE_VALUE)
    return false;
  // get flags + check
  DWORD out_flags;
  if (!GetConsoleMode(out_hnd, &out_flags))
    return false;
  return (out_flags & ENABLE_PROCESSED_OUTPUT) &&
    (out_flags & ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#else
  // On UNIX we need a non-dumb tty.
  return ConsoleIsNotDumb();
#endif // !defined_WIN32)
}

```

For the `SetConsoleMode()` call to enable virtual terminal sequences it’s possible to try and use some RAII wrapper or just a pair of routines for this.

---

<div class="post-metadata">

### Author: ![phetdam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phetdam/32/4846_2.png) [@phetdam](https://discourse.cmake.org/u/phetdam)
#### Post date: [August 14, 2024, 1:29am UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/8 "2024-08-14T01:29:25Z")

</div>

Edit: Forgot about calling `Instance()` before `CLICOLOR_FORCE` is checked.

Well, I tried making some changes after checking out the CMake source at `8733dcd115`, creating a `cmStdOutHandle` class that attempts to enable virtual terminal sequences on construction (via a Meyer’s singleton-esque `Instance()` static function). The instance stores a `HANDLE` retrieved via `GetStdHandle(STD_OUTPUT_HANDLE)` and `DWORD` flags from `GetConsoleMode()`.

So the only real changes to `cmCTest::ColoredOutputSupportedByConsole()` are:

```cpp
bool cmCTest::ColoredOutputSupportedByConsole()
{
  // check if VTS is enabled on Windows (attempts to enable)
#ifdef _WIN32
  bool vts_enabled = cmStdOutHandle::Instance().VtsEnabled();
#endif // _WIN32
  // ... env checking
#if defined(_WIN32)
  return vts_enabled;
#else
  // On UNIX we need a non-dumb tty.
  return ConsoleIsNotDumb();
#endif // !defined(_WIN32)
}

```

There should be a way to allow this kind of functionality to be enabled for `cmake` itself, not just `ctest`, although I don’t know enough about the code base to know where to put it. Maybe in a `Source/cmStdOutHandle.h` header (with accompanying `.cxx` file)?

---

<div class="post-metadata">

### Author: ![phetdam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phetdam/32/4846_2.png) [@phetdam](https://discourse.cmake.org/u/phetdam)
#### Post date: [August 9, 2025, 11:27pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/9 "2025-08-09T23:27:48Z")

</div>

It looks like this thread can probably be closed given [recent changes](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10813) to CTest.

@brad.king made changes to the CMake `cm::StdIo::Stream` stream that enable virtual terminal sequence processing when possible and correctly update the `Stream` terminal kind.

I guess we can reasonably expect this functionality to be available in CMake 4.2.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [August 12, 2025, 1:44pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/10 "2025-08-12T13:44:25Z")

</div>

> [@phetdam](#):
>
> expect this functionality to be available in CMake 4.2.

It’s already in CMake 4.1!

---

<div class="post-metadata">

### Author: ![phetdam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phetdam/32/4846_2.png) [@phetdam](https://discourse.cmake.org/u/phetdam)
#### Post date: [August 12, 2025, 11:43pm UTC](https://discourse.cmake.org/t/ctest-enabling-colored-output-on-windows/7696/11 "2025-08-12T23:43:01Z")

</div>

Even better! 🎉
