Skip to content

Conversation

@4LT
Copy link
Contributor

@4LT 4LT commented Jan 9, 2026

Prevents ANSI color codes from being printed in output in e.g. TrenchBroom without the need for -nocolor switch. Also prevents color codes from appearing when redirecting output, e.g. qbsp my.map | cat

Tested w/ TrenchBroom under Linux: No ANSI codes are printed
Tested w/ terminal under Linux (qbsp some.map): Output displayed w/ color
Tested w/ terminal under Linux/Wine (wine qbsp.exe some.map): ANSI color codes are printed
Tested w/ terminal under Linux/Wine w/ redirected output (wine qbsp.exe some.map | cat): No ANSI color codes are printed

@4LT
Copy link
Contributor Author

4LT commented Jan 9, 2026

oops, left some extra, unnecessary indentation in. Fixing...

@4LT 4LT force-pushed the check-tty-for-ansi-colorcodes branch from 1778bb9 to b4d1ff8 Compare January 9, 2026 15:16
@Paril
Copy link
Collaborator

Paril commented Jan 9, 2026

Oh cool, thanks

common/log.cc Outdated
#elif defined(__unix__)
return isatty(STDOUT_FILENO) != 0;
#else
return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

would false be a better default? Or should we even care about non-Windows platforms w/o POSIX support?

Copy link
Owner

Choose a reason for hiding this comment

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

I don't know if we support any platforms in this category (non-Windows, non-POSIX) so probably doesn't matter either way.

Copy link
Contributor Author

@4LT 4LT Jan 10, 2026

Choose a reason for hiding this comment

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

In that case it could be simplified to #ifdef _WIN32 / #else with _WIN32 not being defined implying __unix__ is defined. It would be less misleading.

Copy link
Owner

Choose a reason for hiding this comment

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

Sure that works.

I checked the codebase and it's a bit inconsistent, but we do already assume lack of _WIN32 implies POSIX (in cmdlib.cc:q_aligned_malloc).

There's also a LINUX preprocessor macro we define ourselves in CMakeLists.txt if CMake reports a unix-like target; LINUX is used in some include guards but nothing else.

@4LT 4LT force-pushed the check-tty-for-ansi-colorcodes branch from fb3c77c to f50ae35 Compare January 9, 2026 22:49
@4LT 4LT changed the title Default ANSI color codes off when stdout is a TTY Default ANSI color codes off when stdout is NOT a TTY Jan 9, 2026
@ericwa
Copy link
Owner

ericwa commented Jan 10, 2026

Nice, thanks.

  • Tested on Win 11: running qbsp.exe asdf | code - -> no color codes in VS Code
  • Tested on Win 11: running qbsp.exe asdf in Windows Terminal -> colors appear
  • Tested on Win 11: running "qbsp.exe" asdf in git bash -> colors appear

so, all good.

Is it OK if I revert the reformatting of #ifdef's? Our clang-format config is currently set to have them un-indented so it'll revert that if I reformat log.cc.

@4LT
Copy link
Contributor Author

4LT commented Jan 10, 2026

Yeah that's cool, it's your repo after all :)

Ah I see you made the change already

@4LT
Copy link
Contributor Author

4LT commented Jan 10, 2026

Alright, that's enough updates from me unless you need me to squash the commits

@ericwa
Copy link
Owner

ericwa commented Jan 10, 2026

Thanks again. I'll squash it in GitHub.

@ericwa ericwa merged commit 5733c25 into ericwa:main Jan 10, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants