CmdReportDay: Broke out sections into functions

This commit is contained in:
Paul Beckingham 2016-04-26 23:30:24 -04:00
parent 1026254b90
commit 21809e1724

View file

@ -35,6 +35,11 @@
#include <algorithm>
#include <iostream>
static void renderAxis (const std::string&, int, int, Color&);
static void renderExclusionBlocks (Composite&, Composite&, const Datetime&, int, int, const std::vector <Range>&, Color&);
static void renderInterval (Composite&, Composite&, const Interval&, int, Palette&, std::map <std::string, Color>&);
static void renderSummary (const std::string&);
////////////////////////////////////////////////////////////////////////////////
int CmdReportDay (
const CLI& cli,
@ -82,13 +87,9 @@ int CmdReportDay (
}
// Render the axis.
std::string indent = " ";
std::cout << '\n'
<< indent;
for (int hour = first_hour; hour <= last_hour; hour++)
std::cout << colorLabel.colorize (leftJustify (hour, 5));
std::cout << '\n';
std::string indent = " ";
renderAxis (indent, first_hour, last_hour, colorLabel);
// Each day is rendered separately.
for (Datetime day = filter.range.start; day < filter.range.end; day++)
@ -96,111 +97,155 @@ int CmdReportDay (
// Render the exclusion blocks.
Composite line1;
Composite line2;
for (int hour = first_hour; hour <= last_hour; hour++)
{
// Construct a range representing a single 'hour', of 'day'.
Range r (Datetime (day.year (), day.month (), day.day (), hour, 0, 0),
Datetime (day.year (), day.month (), day.day (), hour + 1, 0, 0));
for (auto& exc : excluded)
{
if (exc.overlap (r))
{
// Determine which of the four 15-min quarters are included.
auto sub_hour = exc.intersect (r);
auto start_block = quantizeTo15Minutes (sub_hour.start.minute ()) / 15;
auto end_block = quantizeTo15Minutes (sub_hour.end.minute () == 0 ? 60 : sub_hour.end.minute ()) / 15;
int offset = (hour - first_hour) * 5 + start_block;
std::string block (end_block - start_block, ' ');
line1.add (block, offset, colorExc);
line2.add (block, offset, colorExc);
}
}
}
renderExclusionBlocks (line1, line2, day, first_hour, last_hour, excluded, colorExc);
for (auto& track : tracked)
{
auto start_hour = track.range.start.hour ();
auto start_min = track.range.start.minute ();
auto end_hour = track.range.end.hour ();
auto end_min = track.range.end.minute ();
auto start_block = quantizeTo15Minutes (start_min) / 15;
auto end_block = quantizeTo15Minutes (end_min == 0 ? 60 : end_min) / 15;
int start_offset = (start_hour - first_hour) * 5 + start_block;
int end_offset = (end_hour - 1 - first_hour) * 5 + end_block;
// Determine color of interval.
Color colorTrack;
if (track.tags ().size ())
// TODO Instead of using the first tag, look at them all, and choose one
// that has a color defined over any other.
colorTrack = tag_colors [*(track.tags ().begin ())];
else
colorTrack = palette.next ();
// Properly format the tags within the space.
std::string label;
for (auto& tag : track.tags ())
{
if (label != "")
label += ' ';
label += tag;
}
auto width = end_offset - start_offset;
if (width)
{
std::vector <std::string> lines;
wrapText (lines, label, width, false);
std::string label1 (width, ' ');
std::string label2 = label1;
if (lines.size () > 0)
{
label1 = leftJustify (lines[0], width);
if (lines.size () > 1)
label2 = leftJustify (lines[1], width);
}
line1.add (label1, start_offset, colorTrack);
line2.add (label2, start_offset, colorTrack);
// An open interval gets a "..." in the bottom right corner, or
// whatever fits.
if (! track.range.ended ())
{
int space = 3;
if (width < 3)
space = width;
line2.add (std::string (space, '.'),
width - space,
colorTrack);
}
}
}
renderInterval (line1, line2, track, first_hour, palette, tag_colors);
std::cout << indent << line1.str () << '\n'
<< indent << line2.str () << '\n'
<< '\n';
line1.clear ();
line2.clear ();
}
renderSummary (indent);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
static void renderAxis (
const std::string& indent,
int first_hour,
int last_hour,
Color& colorLabel)
{
std::cout << indent;
for (int hour = first_hour; hour <= last_hour; hour++)
std::cout << colorLabel.colorize (leftJustify (hour, 5));
std::cout << '\n';
}
////////////////////////////////////////////////////////////////////////////////
static void renderExclusionBlocks (
Composite& line1,
Composite& line2,
const Datetime& day,
int first_hour,
int last_hour,
const std::vector <Range>& excluded,
Color& colorExc)
{
// Render the exclusion blocks.
for (int hour = first_hour; hour <= last_hour; hour++)
{
// Construct a range representing a single 'hour', of 'day'.
Range r (Datetime (day.year (), day.month (), day.day (), hour, 0, 0),
Datetime (day.year (), day.month (), day.day (), hour + 1, 0, 0));
for (auto& exc : excluded)
{
if (exc.overlap (r))
{
// Determine which of the four 15-min quarters are included.
auto sub_hour = exc.intersect (r);
auto start_block = quantizeTo15Minutes (sub_hour.start.minute ()) / 15;
auto end_block = quantizeTo15Minutes (sub_hour.end.minute () == 0 ? 60 : sub_hour.end.minute ()) / 15;
int offset = (hour - first_hour) * 5 + start_block;
std::string block (end_block - start_block, ' ');
line1.add (block, offset, colorExc);
line2.add (block, offset, colorExc);
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
static void renderInterval (
Composite& line1,
Composite& line2,
const Interval& track,
int first_hour,
Palette& palette,
std::map <std::string, Color>& tag_colors)
{
// TODO track may have started days ago.
auto start_hour = track.range.start.hour ();
auto start_min = track.range.start.minute ();
auto end_hour = track.range.end.hour ();
auto end_min = track.range.end.minute ();
auto start_block = quantizeTo15Minutes (start_min) / 15;
auto end_block = quantizeTo15Minutes (end_min == 0 ? 60 : end_min) / 15;
int start_offset = (start_hour - first_hour) * 5 + start_block;
int end_offset = (end_hour - 1 - first_hour) * 5 + end_block;
if (end_offset > start_offset)
{
// Determine color of interval.
Color colorTrack;
if (track.tags ().size ())
// TODO Instead of using the first tag, look at them all, and choose one
// that has a color defined over any other.
colorTrack = tag_colors [*(track.tags ().begin ())];
else
colorTrack = palette.next ();
// Properly format the tags within the space.
std::string label;
for (auto& tag : track.tags ())
{
if (label != "")
label += ' ';
label += tag;
}
auto width = end_offset - start_offset;
if (width)
{
std::vector <std::string> lines;
wrapText (lines, label, width, false);
std::string label1 (width, ' ');
std::string label2 = label1;
if (lines.size () > 0)
{
label1 = leftJustify (lines[0], width);
if (lines.size () > 1)
label2 = leftJustify (lines[1], width);
}
line1.add (label1, start_offset, colorTrack);
line2.add (label2, start_offset, colorTrack);
// An open interval gets a "..." in the bottom right corner, or
// whatever fits.
if (! track.range.ended ())
{
int space = 3;
if (width < 3)
space = width;
line2.add (std::string (space, '.'),
width - space,
colorTrack);
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
static void renderSummary (const std::string& indent)
{
// TODO Summary, missing.
std::cout << indent << "Tracked\n"
<< indent << "Untracked\n"
<< indent << "Total\n"
<< '\n';
return 0;
}
////////////////////////////////////////////////////////////////////////////////