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.
This commit is contained in:
Adam Strzelecki 2012-10-22 22:08:55 +02:00
parent 9bcbe8c361
commit 486a3cedaa

View File

@ -83,11 +83,48 @@ namespace Catch {
#else #else
#include <unistd.h>
namespace Catch { namespace Catch {
TextColour::TextColour( Colours ){}
TextColour::~TextColour(){} TextColour::TextColour( Colours colour ) {
void TextColour::set( Colours ){} 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 } // end namespace Catch
#endif #endif