From 486a3cedaa200a3e30e0cc7ad2a20a314e3bfa37 Mon Sep 17 00:00:00 2001 From: Adam Strzelecki Date: Mon, 22 Oct 2012 22:08:55 +0200 Subject: [PATCH] ANSI colour support and terminal detection Provides POSIX/UNIX counterpart for Windows colours implementation using ANSI escape codes. It also detects if stdout is a tty and disables colours when using pipes or file output. --- .../internal/catch_console_colour_impl.hpp | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/include/internal/catch_console_colour_impl.hpp b/include/internal/catch_console_colour_impl.hpp index 1830e182..db9d9305 100644 --- a/include/internal/catch_console_colour_impl.hpp +++ b/include/internal/catch_console_colour_impl.hpp @@ -83,11 +83,48 @@ namespace Catch { #else +#include + namespace Catch { - TextColour::TextColour( Colours ){} - TextColour::~TextColour(){} - void TextColour::set( Colours ){} - + + TextColour::TextColour( Colours colour ) { + if( colour ) + set( colour ); + } + + TextColour::~TextColour() { + set( TextColour::None ); + } + + void TextColour::set( Colours colour ) { + if( isatty( fileno(stdout) ) ) + switch( colour ) { + case TextColour::FileName: + std::cout << "\e[1m"; // bold + break; + case TextColour::ResultError: + std::cout << "\e[1;31m"; // bright red + break; + case TextColour::ResultSuccess: + std::cout << "\e[1;32m"; // bright green + break; + case TextColour::Error: + std::cout << "\e[0;31m"; // dark red + break; + case TextColour::Success: + std::cout << "\e[0;32m"; // dark green + break; + case TextColour::OriginalExpression: + std::cout << "\e[0;36m"; // cyan + break; + case TextColour::ReconstructedExpression: + std::cout << "\e[0;33m"; // yellow + break; + default: + std::cout << "\e[0m"; // reset + } + } + } // end namespace Catch #endif