Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Added terminal colour codes for POSIX

With thanks to Adam Strzelecki
Commit
108f1937d8cbe04038ab8d2aaf9f6e958fe8168c
Author
Phil Nash ·
Parents
8f4cc541d55c

Files · Download .tar.gz

Changes

 include/internal/catch_console_colour_impl.hpp | 58 ++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 8 deletions(-)

diff --git a/include/internal/catch_console_colour_impl.hpp b/include/internal/catch_console_colour_impl.hpp
index 1830e182..101b3220 100644
--- a/include/internal/catch_console_colour_impl.hpp
+++ b/include/internal/catch_console_colour_impl.hpp
@@ -10,11 +10,56 @@
 
 #include "catch_console_colour.hpp"
 
-#ifdef CATCH_PLATFORM_WINDOWS
+namespace Catch {
 
-#include <windows.h>
+#if defined( CATCH_CONFIG_USE_POSIX_COLOUR_CODES )
 
-namespace Catch {
+    // use POSIX console terminal codes
+    // Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
+    // https://github.com/philsquared/Catch/pull/131
+    
+    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;
+                case TextColour::None:
+                    std::cout << "\e[0m"; // reset
+            }
+        }
+    }
+    
+#elif defined ( CATCH_PLATFORM_WINDOWS )
+
+#include <windows.h>
 
     namespace {
     
@@ -78,18 +123,15 @@ namespace Catch {
     void TextColour::set( Colours colour ) {
         m_impl->set( colour );
     }
-    
-} // end namespace Catch
 
 #else
 
-namespace Catch {
     TextColour::TextColour( Colours ){}
     TextColour::~TextColour(){}
     void TextColour::set( Colours ){}
-    
-} // end namespace Catch
 
 #endif
 
+} // end namespace Catch
+
 #endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED