Skip to content

Commit 693f33e

Browse files
committed
Remove tty check from table component
Check for tty should be done in the application and passed to the table. Replace tty check with explicit setting for colored output. Relates to GothenburgBitFactory/timewarrior#727 GothenburgBitFactory/timewarrior#730 Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
1 parent 4810e8e commit 693f33e

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

src/Table.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,7 @@ void Table::set (int row, int col, const Color color)
100100
std::string Table::render ()
101101
{
102102
// Piped output disables color, unless overridden.
103-
if (! _forceColor &&
104-
#ifdef _WIN32
105-
! _isatty(_fileno(stdout))
106-
#else
107-
! isatty (STDOUT_FILENO)
108-
#endif
109-
)
103+
if (! _with_color)
110104
{
111105
_header = Color ("");
112106
_odd = Color ("");

src/Table.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
////////////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright 2016 - 2017, 2019 - 2021, 2023, Gothenburg Bit Factory.
3+
// Copyright 2016 - 2017, 2019 - 2021, 2023, 2025 Gothenburg Bit Factory.
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining a copy
66
// of this software and associated documentation files (the "Software"), to deal
@@ -49,9 +49,9 @@ class Table
4949
void extraColorEven (const Color& c) { _extra_even = c; }
5050
void truncateLines (int n) { _truncate_lines = n; }
5151
void truncateRows (int n) { _truncate_rows = n; }
52-
void forceColor () { _forceColor = true; }
5352
void obfuscate () { _obfuscate = true; }
5453
void underlineHeaders () { _underline_headers = true; }
54+
void withColor (bool with_color) { _with_color = with_color; }
5555
int lines () { return _lines; }
5656
int rows () { return (int) _data.size (); }
5757

@@ -90,7 +90,7 @@ class Table
9090
Color _extra_even {0};
9191
int _truncate_lines {0};
9292
int _truncate_rows {0};
93-
bool _forceColor {false};
93+
bool _with_color {true};
9494
bool _obfuscate {false};
9595
bool _underline_headers {false};
9696
int _lines {0};

test/table.t.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
////////////////////////////////////////////////////////////////////////////////
3232
int main (int, char**)
3333
{
34-
UnitTest t (1);
34+
UnitTest t (3);
3535

3636
try
3737
{
@@ -75,6 +75,25 @@ int main (int, char**)
7575

7676
std::cout << t1.render ();
7777
t.ok (t1.lines () > 4, "Table::lines > 4");
78+
79+
// Test color behavior - with color enabled (default)
80+
Table t3;
81+
t3.width (80);
82+
t3.add ("Header", true);
83+
row = t3.addRow ();
84+
t3.set (row, 0, "cell with color", single_cell);
85+
std::string colored_output = t3.render ();
86+
t.ok (colored_output.find("\033[") != std::string::npos, "Colored output contains ANSI escape codes");
87+
88+
// Test color behavior - with color disabled
89+
Table t4;
90+
t4.width (80);
91+
t4.withColor (false); // Disable color
92+
t4.add ("Header", true);
93+
row = t4.addRow ();
94+
t4.set (row, 0, "cell without color", single_cell);
95+
std::string uncolored_output = t4.render ();
96+
t.ok (uncolored_output.find("\033[") == std::string::npos, "Uncolored output does not contain ANSI escape codes");
7897

7998
// Chessboard example
8099
Table t2;

0 commit comments

Comments
 (0)