From c562f3fc81ad1ede7b268f564430c238a819bcde Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Mon, 28 Jan 2019 14:31:29 +0100 Subject: [PATCH] Make Chart::render return a string instead of writing to std::cout --- src/Chart.cpp | 50 +++++++++++++++++++++------------------ src/Chart.h | 2 +- src/commands/CmdChart.cpp | 2 +- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/Chart.cpp b/src/Chart.cpp index 45784fd7..a3c661ee 100644 --- a/src/Chart.cpp +++ b/src/Chart.cpp @@ -36,7 +36,7 @@ #include #include -void Chart::render ( +std::string Chart::render ( const Interval &filter, const std::vector &tracked, const std::vector &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 (); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Chart.h b/src/Chart.h index 531a2a5d..3ffda3a0 100644 --- a/src/Chart.h +++ b/src/Chart.h @@ -36,7 +36,7 @@ class Chart public: Chart() = default; - static void render (const Interval&, const std::vector &, const std::vector &, const std::map &, const std::map &, 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 &, const std::vector &, const std::map &, const std::map &, 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); diff --git a/src/commands/CmdChart.cpp b/src/commands/CmdChart.cpp index e27801be..0e15d8ba 100644 --- a/src/commands/CmdChart.cpp +++ b/src/commands/CmdChart.cpp @@ -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; }