Make Chart::render return a string instead of writing to std::cout

This commit is contained in:
Thomas Lauf 2019-01-28 14:31:29 +01:00
parent 365ae27e49
commit c562f3fc81
3 changed files with 29 additions and 25 deletions

View file

@ -36,7 +36,7 @@
#include <timew.h>
#include <Chart.h>
void Chart::render (
std::string Chart::render (
const Interval &filter,
const std::vector<Interval> &tracked,
const std::vector<Range> &exclusions,
@ -78,19 +78,21 @@ void Chart::render (
const auto indent = std::string (indent_size, ' ');
const auto padding_size = indent_size + ((last_hour - first_hour + 1) * (cell_size)) + 1;
std::cout << '\n';
std::stringstream out;
out << '\n';
// Render the axis.
if (!with_internal_axis)
{
std::cout << indent
<< Chart::renderAxis (
first_hour,
last_hour,
color_label,
color_today,
cell_size,
with_totals);
out << indent
<< Chart::renderAxis (
first_hour,
last_hour,
color_label,
color_today,
cell_size,
with_totals);
}
// For rendering labels on edge detection.
@ -135,32 +137,34 @@ void Chart::render (
auto labelWeekday = with_weekday ? Chart::renderWeekday (day, color_day) : "";
auto labelDay = with_day ? Chart::renderDay (day, color_day) : "";
std::cout << labelMonth
<< labelWeek
<< labelWeekday
<< labelDay
<< lines[0].str ();
out << labelMonth
<< labelWeek
<< labelWeekday
<< labelDay
<< lines[0].str ();
if (lines.size () > 1)
{
for (unsigned int i = 1; i < lines.size (); ++i)
{
std::cout << "\n"
<< indent
<< lines[i].str ();
out << "\n"
<< indent
<< lines[i].str ();
}
}
std::cout << (with_totals ? Chart::renderTotal (work) : "")
<< '\n';
out << (with_totals ? Chart::renderTotal (work) : "")
<< '\n';
previous = day;
total_work += work;
}
std::cout << (with_totals ? Chart::renderSubTotal (total_work, std::string (padding_size, ' ')) : "")
<< (with_holidays ? Chart::renderHolidays (holidays) : "")
<< (with_summary ? Chart::renderSummary (indent, filter, exclusions, tracked, show_intervals) : "");
out << (with_totals ? Chart::renderSubTotal (total_work, std::string (padding_size, ' ')) : "")
<< (with_holidays ? Chart::renderHolidays (holidays) : "")
<< (with_summary ? Chart::renderSummary (indent, filter, exclusions, tracked, show_intervals) : "");
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -36,7 +36,7 @@ class Chart
public:
Chart() = default;
static void render (const Interval&, const std::vector <Interval>&, const std::vector <Range>&, const std::map <Datetime, std::string>&, const std::map <std::string, Color>&, const Color&, const Color&, const Color&, const Color&, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, int, int, int);
static std::string render (const Interval&, const std::vector <Interval>&, const std::vector <Range>&, const std::map <Datetime, std::string>&, const std::map <std::string, Color>&, const Color&, const Color&, const Color&, const Color&, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, int, int, int);
static unsigned long getIndentSize (bool, bool, bool, bool);

View file

@ -145,7 +145,7 @@ int renderChart (
auto axis_type = rules.get ("reports." + type + ".axis");
const auto with_internal_axis = axis_type == "internal";
Chart::render (filter, tracked, exclusions, holidays, tag_colors, color_today, color_holiday, color_label, color_exclusion, show_intervals, determine_hour_range, with_ids, with_summary, with_holidays, with_totals, with_month, with_week, with_day, with_weekday, with_internal_axis, minutes_per_char, spacing, num_lines);
std::cout << Chart::render (filter, tracked, exclusions, holidays, tag_colors, color_today, color_holiday, color_label, color_exclusion, show_intervals, determine_hour_range, with_ids, with_summary, with_holidays, with_totals, with_month, with_week, with_day, with_weekday, with_internal_axis, minutes_per_char, spacing, num_lines);
return 0;
}