diff options
author | Nico Rieck <nico.rieck@gmail.com> | 2013-09-11 00:36:48 +0000 |
---|---|---|
committer | Nico Rieck <nico.rieck@gmail.com> | 2013-09-11 00:36:48 +0000 |
commit | 44a61bde15d456527156ee2080f0964344b939fe (patch) | |
tree | c62e30e826059e609c06769baf2982ca7ce8b757 /lib/Support/Windows | |
parent | f495a2679248bca1a426052b3a297326bb46e9db (diff) | |
download | llvm-44a61bde15d456527156ee2080f0964344b939fe.tar.gz llvm-44a61bde15d456527156ee2080f0964344b939fe.tar.bz2 llvm-44a61bde15d456527156ee2080f0964344b939fe.tar.xz |
Support ANSI escape code on Windows
In some cases (e.g. when a build system pipes stderr) the Windows console
API cannot be used to color output. For these, provide a way to switch to
ANSI escape codes. This is required for Clang's -fansi-escape-codes option.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190460 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows')
-rw-r--r-- | lib/Support/Windows/Process.inc | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/Support/Windows/Process.inc b/lib/Support/Windows/Process.inc index 0191751a82..5d776504fb 100644 --- a/lib/Support/Windows/Process.inc +++ b/lib/Support/Windows/Process.inc @@ -216,6 +216,11 @@ bool Process::StandardErrHasColors() { return FileDescriptorHasColors(2); } +static bool UseANSI = false; +void Process::UseANSIEscapeCodes(bool enable) { + UseANSI = enable; +} + namespace { class DefaultColors { @@ -237,10 +242,12 @@ DefaultColors defaultColors; } bool Process::ColorNeedsFlush() { - return true; + return !UseANSI; } const char *Process::OutputBold(bool bg) { + if (UseANSI) return "\033[1m"; + WORD colors = DefaultColors::GetCurrentColor(); if (bg) colors |= BACKGROUND_INTENSITY; @@ -251,6 +258,8 @@ const char *Process::OutputBold(bool bg) { } const char *Process::OutputColor(char code, bool bold, bool bg) { + if (UseANSI) return colorcodes[bg?1:0][bold?1:0][code&7]; + WORD colors; if (bg) { colors = ((code&1) ? BACKGROUND_RED : 0) | @@ -276,6 +285,8 @@ static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) { } const char *Process::OutputReverse() { + if (UseANSI) return "\033[7m"; + const WORD attributes = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)); @@ -302,6 +313,7 @@ const char *Process::OutputReverse() { } const char *Process::ResetColor() { + if (UseANSI) return "\033[0m"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors()); return 0; } |