From 1687bd32357154c2508701f8b5d9a26b4a3858cb Mon Sep 17 00:00:00 2001 From: David Neto Date: Mon, 28 Aug 2017 10:30:58 -0400 Subject: [PATCH] Disassembler: Print colour codes only when writing to a terminal --- tools/dis/dis.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tools/dis/dis.cpp b/tools/dis/dis.cpp index 226f733ab..9c14b07a5 100644 --- a/tools/dis/dis.cpp +++ b/tools/dis/dis.cpp @@ -12,6 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +#include // Need fileno +#include +#endif + #include #include #include @@ -39,7 +44,8 @@ Options: not specified, or if the filename is "-". --no-color Don't print in color. - The default when output goes to a file. + The default when output goes to something other than a + terminal (e.g. a file, a pipe, or a shell redirection). --no-indent Don't indent instructions. @@ -142,7 +148,11 @@ int main(int argc, char** argv) { if (!outFile || (0 == strcmp("-", outFile))) { // Print to standard output. options |= SPV_BINARY_TO_TEXT_OPTION_PRINT; - if (allow_color) { + bool output_is_tty = true; +#if defined(_POSIX_VERSION) + output_is_tty = isatty(fileno(stdout)); +#endif + if (allow_color && output_is_tty) { options |= SPV_BINARY_TO_TEXT_OPTION_COLOR; } }