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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- fix double line after header in some report tables #115


## [v21.01.1] - 2021-01-06
Expand Down
40 changes: 36 additions & 4 deletions main/report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@

/**** ReportEntry Class ****/
// Constructor
ReportEntry::ReportEntry(const char *t, int c, int a, int m) :
text(t ? t : ""),
color(c),
align(a),
edge(a),
mode(m),
draw_a_line(t==nullptr)
{
FnTrace("ReportEntry::ReportEntry()");
}
ReportEntry::ReportEntry(const std::string &t, int c, int a, int m) :
text(t),
color(c),
align(a),
edge(a),
mode(m)
mode(m),
draw_a_line(false)
{
FnTrace("ReportEntry::ReportEntry()");
}
Expand Down Expand Up @@ -284,6 +295,7 @@ int Report::Render(Terminal *term, LayoutZone *lz, Flt header_size,
if ((last_line > lines_shown || print) && footer < 2)
footer = 2;

lines_shown = (int) ((lz->size_y - (header + footer)) / spacing + .5);
if (debug_mode && lines_shown < 1)
{ //FIX BAK-->Why does lines_shown sometimes hit 0?
fprintf(stderr, "Report::Render lines_shown = %d, fixing\n", lines_shown);
Expand Down Expand Up @@ -335,7 +347,7 @@ int Report::Render(Terminal *term, LayoutZone *lz, Flt header_size,
xx = re.pos; break;
}

if (re.text.empty())
if (re.draw_a_line)
{
switch (re.align)
{
Expand Down Expand Up @@ -645,7 +657,7 @@ int Report::PrintEntry(const ReportEntry &report_entry, int start, int width, in
int i;
int c;

if (report_entry.text.empty())
if (report_entry.draw_a_line)
len = report_entry.max_len;
else
{
Expand Down Expand Up @@ -686,7 +698,7 @@ int Report::PrintEntry(const ReportEntry &report_entry, int start, int width, in
xx = 0;
}

if (!report_entry.text.empty())
if (!report_entry.draw_a_line)
{
xx += start;
strncpy(&text[xx], report_entry.text.c_str(), len);
Expand Down Expand Up @@ -777,6 +789,26 @@ int Report::Mode(int new_mode)
return 0;
}

int Report::Text(const char *text, int c, int align, float indent)
{
FnTrace("Report::Text()");
int retval = 0;
ReportEntry re(text, c, align, current_mode);
if (indent > 0.01)
{
re.pos = indent;
re.edge = ALIGN_LEFT;
}
else if (indent < -0.01)
{
re.pos = -indent;
re.edge = ALIGN_RIGHT;
}
else
re.edge = align;
retval = Add(re);
return retval;
}
int Report::Text(const std::string &text, int c, int align, float indent)
{
FnTrace("Report::Text()");
Expand Down
3 changes: 3 additions & 0 deletions main/report.hh
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public:
Uchar align;
Uchar edge;
Uchar mode;
bool draw_a_line=false; // draw a line if text char ptr is a nullptr

// Constructor
ReportEntry(const char *t, int c, int a, int m);
ReportEntry(const std::string &t, int c, int a, int m);
// Destructor
~ReportEntry() {}
Expand Down Expand Up @@ -146,6 +148,7 @@ public:
int Clear(); // erases report
int Load(const std::string &textfile, int color = COLOR_DEFAULT); // make report out of text file
int Mode(int flags); // printing mode to use for next entries
int Text(const char *t, int c, int a, float indent); // Adds text entry
int Text(const std::string &t, int c, int a, float indent); // Adds text entry
int Text2Col(const std::string &text, int color, int align, float indent);
int Number(int n, int c, int a, float indent); // Adds number entry
Expand Down
3 changes: 2 additions & 1 deletion tests/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ endif()
# create test
add_executable(test_main
test_main.cc
test_labor.cc)
test_labor.cc
test_report.cc)
add_test(test_main test_main)
target_link_libraries(test_main PRIVATE
vtcore tz)
Expand Down
96 changes: 96 additions & 0 deletions tests/main/test_report.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "catch2/catch.hpp"
#include "report.hh"

#include <string>
#include <vector>
#include <fstream>
#include <limits> // std::numeric_limits
#include <cmath> // std::isnan, std::isinf
#include <locale>

TEST_CASE("report: ReportEntry: pass nullptr for text sets draw_a_line")
{
int color = 0;
int align = 0;
int mode = 0;
ReportEntry re(nullptr,color, align, mode);
CHECK(re.text.empty());
CHECK(re.draw_a_line == true);
CHECK(re.color == color);
CHECK(re.align == align);
CHECK(re.mode == mode);
}
TEST_CASE("report: ReportEntry: pass char nullptr as text sets draw_line")
{
const char *text = nullptr;
int color = 0;
int align = 0;
int mode = 0;
REQUIRE_NOTHROW(ReportEntry(text, color, align, mode));
ReportEntry re(text,color, align, mode);
CHECK(re.text.empty());
CHECK(re.draw_a_line == true);
CHECK(re.color == color);
CHECK(re.align == align);
CHECK(re.mode == mode);
}

TEST_CASE("report: ReportEntry: empty char string does not set draw_a_line")
{
const char *text = "";
int color = 0;
int align = 0;
int mode = 0;
REQUIRE_NOTHROW(ReportEntry(text, color, align, mode));
ReportEntry re(text,color, align, mode);
CHECK(re.text.empty());
CHECK(re.draw_a_line == false);
}
TEST_CASE("report: ReportEntry: empty std::string does not set draw_a_line")
{
const std::string text = "";
int color = 0;
int align = 0;
int mode = 0;
REQUIRE_NOTHROW(ReportEntry(text, color, align, mode));
ReportEntry re(text,color, align, mode);
CHECK(re.text.empty());
CHECK(re.draw_a_line == false);
}

TEST_CASE("report: ReportEntry: char entries creating no line")
{
auto text = GENERATE(as<const char *>{},
"a",
"12",
"11144",
"loads of text with spaces",
"nothing to see here!");
int color = 0;
int align = 0;
int mode = 0;
ReportEntry re(text,color, align, mode);
CHECK(re.text.empty() == false);
CHECK(re.draw_a_line == false);
CHECK(re.color == color);
CHECK(re.align == align);
CHECK(re.mode == mode);
}
TEST_CASE("report: ReportEntry: std::string entries creating no line")
{
auto text = GENERATE(as<std::string>{},
"a",
"12",
"11144",
"loads of text with spaces",
"nothing to see here!");
int color = 0;
int align = 0;
int mode = 0;
ReportEntry re(text,color, align, mode);
CHECK(re.text.empty() == false);
CHECK(re.draw_a_line == false);
CHECK(re.color == color);
CHECK(re.align == align);
CHECK(re.mode == mode);
}