Skip to content

Commit a061bdf

Browse files
authored
Pass color setting to table component (#4005)d
Pass color setting to table component (#4005) Removed TTY check from libshared `Table` component in 3da15345b1bdd2bc69de7231e578bd5d530fc92c. Instead, the color settings from the main application are passed to it. Make stats.test.py check for ANSI color codes Make commands.test.py check for ANSI color codes
1 parent fbea4b3 commit a061bdf

10 files changed

Lines changed: 70 additions & 11 deletions

File tree

src/commands/CmdCalendar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ int CmdCalendar::execute(std::string& output) {
345345
holTable.width(Context::getContext().getWidth());
346346
holTable.add("Date");
347347
holTable.add("Holiday");
348+
holTable.withColor(Context::getContext().color());
348349
setHeaderUnderline(holTable);
349350

350351
auto dateFormat = config.get("dateformat.holiday");
@@ -408,6 +409,7 @@ std::string CmdCalendar::renderMonths(int firstMonth, int firstYear, const Datet
408409
Table view;
409410
setHeaderUnderline(view);
410411
view.width(Context::getContext().getWidth());
412+
view.withColor(Context::getContext().color());
411413
for (int i = 0; i < (monthsPerLine * 8); i += 8) {
412414
if (weekStart == 1) {
413415
view.add("", false);

src/commands/CmdColor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int CmdColor::execute(std::string& output) {
7171

7272
Table view;
7373
view.width(Context::getContext().getWidth());
74-
if (Context::getContext().config.getBoolean("color")) view.forceColor();
74+
view.withColor(Context::getContext().config.getBoolean("color"));
7575
view.add("Color");
7676
view.add("Definition");
7777

src/commands/CmdInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int CmdInfo::execute(std::string& output) {
9696
Table view;
9797
view.width(Context::getContext().getWidth());
9898
if (Context::getContext().config.getBoolean("obfuscate")) view.obfuscate();
99-
if (Context::getContext().color()) view.forceColor();
99+
view.withColor(Context::getContext().color());
100100
view.add("Name");
101101
view.add("Value");
102102
setHeaderUnderline(view);
@@ -411,7 +411,7 @@ int CmdInfo::execute(std::string& output) {
411411
}
412412

413413
if (Context::getContext().config.getBoolean("obfuscate")) urgencyDetails.obfuscate();
414-
if (Context::getContext().config.getBoolean("color")) view.forceColor();
414+
urgencyDetails.withColor(Context::getContext().config.getBoolean("color"));
415415

416416
urgencyDetails.width(Context::getContext().getWidth());
417417
urgencyDetails.add(""); // Attribute
@@ -500,7 +500,7 @@ int CmdInfo::execute(std::string& output) {
500500
setHeaderUnderline(journal);
501501

502502
if (Context::getContext().config.getBoolean("obfuscate")) journal.obfuscate();
503-
if (Context::getContext().config.getBoolean("color")) journal.forceColor();
503+
journal.withColor(Context::getContext().config.getBoolean("color"));
504504

505505
journal.width(Context::getContext().getWidth());
506506
journal.add("Date");

src/commands/CmdProjects.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ int CmdProjects::execute(std::string& output) {
101101
view.width(Context::getContext().getWidth());
102102
view.add("Project");
103103
view.add("Tasks", false);
104+
view.withColor(Context::getContext().color());
104105
setHeaderUnderline(view);
105106

106107
// create sorted list of table entries

src/commands/CmdStats.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ int CmdStats::execute(std::string& output) {
144144
// Create a table for output.
145145
Table view;
146146
view.width(Context::getContext().getWidth());
147+
view.withColor(Context::getContext().color());
147148
view.intraPadding(2);
148149
view.add("Category");
149150
view.add("Data");

src/commands/CmdSummary.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ int CmdSummary::execute(std::string& output) {
130130

131131
Color bar_color;
132132
Color bg_color;
133+
view.withColor(Context::getContext().color());
134+
133135
if (Context::getContext().color()) {
134136
bar_color = Color(Context::getContext().config.get("color.summary.bar"));
135137
bg_color = Color(Context::getContext().config.get("color.summary.background"));

test/calendar.test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_basic_command_holidays(self):
9595
self.assertIn("Date Holiday", out)
9696

9797
def test_basic_command_single_holiday(self):
98-
"""Verify 'calendar rc.holiday.test.name:donkeyday rc.holiday.test.date:[tomorrws date] rc.calendar.holidays:full' does not fail"""
98+
"""Verify 'calendar rc.holiday.test.name:donkeyday rc.holiday.test.date:[tomorrow's date] rc.calendar.holidays:full' does not fail"""
9999
code, out, err = self.t(
100100
"calendar rc.holiday.test.name:donkeyday rc.holliday.test.date:{0} rc.calendar.holidays:full".format(
101101
self.tomorrow

test/commands.test.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,61 @@ def test_command_dna(self):
5151
def test_command_dna_color(self):
5252
"""Verify 'add', 'modify', 'list' dna"""
5353
code, out, err = self.t("commands rc._forcecolor:on")
54-
self.assertRegex(out, r"add\s+operation\s+RW\s+Ctxt\s+Mods\s+Adds a new task")
55-
self.assertRegex(
56-
out, r"list\s+report\s+RO\s+ID\s+GC\s+Recur\s+Ctxt\s+Filt\s+Most details of"
54+
55+
columns = [
56+
"add",
57+
"operation",
58+
"RW",
59+
"",
60+
"",
61+
"",
62+
"Ctxt",
63+
"",
64+
"Mods",
65+
"",
66+
"Adds a new task",
67+
]
68+
col_regex = r"\s*\x1b\[0m\x1b\[48;5;234m \x1b\[0m\x1b\[48;5;234m\s*".join(
69+
columns
5770
)
58-
self.assertRegex(out, r"modify\s+operation\s+RW\s+Filt\s+Mods\s+Modifies the")
71+
expected_regex = r"\x1b\[48;5;234m" + col_regex + r"\s*\x1b\[0m"
72+
self.assertRegex(out, expected_regex)
73+
74+
columns = [
75+
"list",
76+
"report",
77+
"RO",
78+
"ID",
79+
"GC",
80+
"Recur",
81+
"Ctxt",
82+
"Filt",
83+
"Most details of",
84+
]
85+
col_regex = r"\s*(\x1b\[0m\x1b\[48;5;234m)? (\x1b\[0m\x1b\[48;5;234m)?\s*".join(
86+
columns
87+
)
88+
expected_regex = r"(\x1b\[48;5;234m)?" + col_regex + r"\s*(\x1b\[0m)?"
89+
self.assertRegex(out, expected_regex)
90+
91+
columns = [
92+
"modify",
93+
"operation",
94+
"RW",
95+
"",
96+
"",
97+
"",
98+
"",
99+
"Filt",
100+
"Mods",
101+
"",
102+
"Modifies the",
103+
]
104+
col_regex = r"\s*(\x1b\[0m\x1b\[48;5;234m)? (\x1b\[0m\x1b\[48;5;234m)?\s*".join(
105+
columns
106+
)
107+
expected_regex = r"(\x1b\[48;5;234m)?" + col_regex + r"\s*(\x1b\[0m)?"
108+
self.assertRegex(out, expected_regex)
59109

60110

61111
if __name__ == "__main__":

test/stats.test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def test_stats(self):
5454
self.assertRegex(out, "Total\\s+3\n")
5555

5656
code, out, err = self.t("stats rc._forcecolor:on")
57-
self.assertRegex(out, "Pending\\s+1\n")
57+
self.assertRegex(
58+
out,
59+
r"\x1b\[48;5;234mPending\s+\x1b\[0m\x1b\[48;5;234m \x1b\[0m\x1b\[48;5;234m1\s+\x1b\[0m",
60+
)
5861

5962

6063
if __name__ == "__main__":

0 commit comments

Comments
 (0)