diff --git a/src/Table.cpp b/src/Table.cpp index 9e3706d..a94c734 100644 --- a/src/Table.cpp +++ b/src/Table.cpp @@ -100,13 +100,7 @@ void Table::set (int row, int col, const Color color) std::string Table::render () { // Piped output disables color, unless overridden. - if (! _forceColor && -#ifdef _WIN32 - ! _isatty(_fileno(stdout)) -#else - ! isatty (STDOUT_FILENO) -#endif - ) + if (! _with_color) { _header = Color (""); _odd = Color (""); diff --git a/src/Table.h b/src/Table.h index 87adc7a..819e0ca 100644 --- a/src/Table.h +++ b/src/Table.h @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2016 - 2017, 2019 - 2021, 2023, Gothenburg Bit Factory. +// Copyright 2016 - 2017, 2019 - 2021, 2023, 2025 Gothenburg Bit Factory. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -52,6 +52,7 @@ class Table void forceColor () { _forceColor = true; } void obfuscate () { _obfuscate = true; } void underlineHeaders () { _underline_headers = true; } + void withColor (bool with_color) { _with_color = with_color; } int lines () { return _lines; } int rows () { return (int) _data.size (); } @@ -90,7 +91,8 @@ class Table Color _extra_even {0}; int _truncate_lines {0}; int _truncate_rows {0}; - bool _forceColor {false}; + bool _forceColor {false}; // TODO: remove once TW and TI have switched + bool _with_color {true}; bool _obfuscate {false}; bool _underline_headers {false}; int _lines {0}; diff --git a/test/table.t.cpp b/test/table.t.cpp index 2d43ec6..caece76 100644 --- a/test/table.t.cpp +++ b/test/table.t.cpp @@ -31,7 +31,7 @@ //////////////////////////////////////////////////////////////////////////////// int main (int, char**) { - UnitTest t (1); + UnitTest t (3); try { @@ -75,6 +75,25 @@ int main (int, char**) std::cout << t1.render (); t.ok (t1.lines () > 4, "Table::lines > 4"); + + // Test color behavior - with color enabled (default) + Table t3; + t3.width (80); + t3.add ("Header", true); + row = t3.addRow (); + t3.set (row, 0, "cell with color", single_cell); + std::string colored_output = t3.render (); + t.ok (colored_output.find("\033[") != std::string::npos, "Colored output contains ANSI escape codes"); + + // Test color behavior - with color disabled + Table t4; + t4.width (80); + t4.withColor (false); // Disable color + t4.add ("Header", true); + row = t4.addRow (); + t4.set (row, 0, "cell without color", single_cell); + std::string uncolored_output = t4.render (); + t.ok (uncolored_output.find("\033[") == std::string::npos, "Uncolored output does not contain ANSI escape codes"); // Chessboard example Table t2;