Skip to content

Commit e83fc64

Browse files
committed
Colors: Do not output colors to a file
1 parent 1f2b491 commit e83fc64

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

lib/color.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#endif
2424
#include <cstddef>
2525
#include <sstream> // IWYU pragma: keep
26+
#include <iostream>
2627

2728
bool gDisableColors = false;
2829

@@ -32,9 +33,16 @@ std::ostream& operator<<(std::ostream& os, const Color& /*c*/)
3233
#else
3334
std::ostream& operator<<(std::ostream & os, const Color& c)
3435
{
35-
// TODO: handle piping into file as well as other pipes like stderr
36-
static const bool s_is_tty = isatty(STDOUT_FILENO);
37-
if (!gDisableColors && s_is_tty)
36+
static const bool isatty_stdout = isatty(STDOUT_FILENO);
37+
static const bool isatty_stderr = isatty(STDERR_FILENO);
38+
bool stream_is_tty;
39+
if (&os == &std::cout)
40+
stream_is_tty = isatty_stdout;
41+
else if (&os == &std::cerr)
42+
stream_is_tty = isatty_stderr;
43+
else
44+
stream_is_tty = (isatty_stdout && isatty_stderr);
45+
if (!gDisableColors && stream_is_tty)
3846
return os << "\033[" << static_cast<std::size_t>(c) << "m";
3947
#endif
4048
return os;

0 commit comments

Comments
 (0)