Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/Table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("");
Expand Down
6 changes: 4 additions & 2 deletions src/Table.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 (); }

Expand Down Expand Up @@ -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};
Expand Down
21 changes: 20 additions & 1 deletion test/table.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
////////////////////////////////////////////////////////////////////////////////
int main (int, char**)
{
UnitTest t (1);
UnitTest t (3);

try
{
Expand Down Expand Up @@ -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;
Expand Down