Skip to content

Commit

Permalink
allow CLICOLOR_FORCE env var to force CLI color output
Browse files Browse the repository at this point in the history
  • Loading branch information
cconverse711 committed Apr 11, 2024
1 parent 83d4e31 commit 0f4ba54
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef _WIN32
#include <unistd.h>
#include <cstddef>
#include <cstdlib>
#include <sstream>
#include <iostream>
#endif
Expand All @@ -38,12 +39,19 @@ static bool isStreamATty(const std::ostream & os)
return stderr_tty;
return (stdout_tty && stderr_tty);
}

static bool isCliColorForced()
{
// See https://bixense.com/clicolors/
static const bool force_color = (nullptr != getenv("CLICOLOR_FORCE"));
return force_color;
}
#endif

std::ostream& operator<<(std::ostream & os, Color c)
{
#ifndef _WIN32
if (!gDisableColors && isStreamATty(os))
if (!gDisableColors && (isCliColorForced() || isStreamATty(os)))
return os << "\033[" << static_cast<std::size_t>(c) << "m";
#else
(void)c;
Expand Down

0 comments on commit 0f4ba54

Please sign in to comment.