Skip to content

Commit 46b30b0

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 46b30b0

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

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: 4 additions & 2 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
@@ -52,6 +52,7 @@ class Table
5252
void forceColor () { _forceColor = true; }
5353
void obfuscate () { _obfuscate = true; }
5454
void underlineHeaders () { _underline_headers = true; }
55+
void withColor (bool with_color) { _with_color = with_color; }
5556
int lines () { return _lines; }
5657
int rows () { return (int) _data.size (); }
5758

@@ -90,7 +91,8 @@ class Table
9091
Color _extra_even {0};
9192
int _truncate_lines {0};
9293
int _truncate_rows {0};
93-
bool _forceColor {false};
94+
bool _forceColor {false}; // TODO: remove once TW and TI have switched
95+
bool _with_color {true};
9496
bool _obfuscate {false};
9597
bool _underline_headers {false};
9698
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)