- Converted history reports to use ViewText.
- Fixed unit test.
This commit is contained in:
Paul Beckingham 2011-05-11 00:20:35 -04:00
parent fcb711d673
commit ec7d9ed765
5 changed files with 103 additions and 144 deletions

View file

@ -51,12 +51,6 @@ ViewText::ViewText ()
{ {
} }
////////////////////////////////////////////////////////////////////////////////
void ViewText::add (const std::string& label)
{
_columns.push_back (Column::factory ("string", label));
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int ViewText::addRow () int ViewText::addRow ()
{ {

View file

@ -40,7 +40,7 @@ public:
~ViewText () {} ~ViewText () {}
// View specifications. // View specifications.
void add (const std::string&); void add (Column* col) { _columns.push_back (col); }
void width (int width) { _width = width; } void width (int width) { _width = width; }
void leftMargin (int margin) { _left_margin = margin; } void leftMargin (int margin) { _left_margin = margin; }
void colorHeader (Color& c) { _header = c; } void colorHeader (Color& c) { _header = c; }
@ -55,7 +55,7 @@ public:
void truncateLines (int n) { _truncate_lines = n; } void truncateLines (int n) { _truncate_lines = n; }
void truncateRows (int n) { _truncate_rows = n; } void truncateRows (int n) { _truncate_rows = n; }
int lines () { return _lines; } int lines () { return _lines; }
int rows () { return _rows; } int rows () { return (int) _data.size (); }
// Data provision. // Data provision.
int addRow (); int addRow ();

View file

@ -28,7 +28,7 @@
#include <sstream> #include <sstream>
#include <Context.h> #include <Context.h>
#include <Table.h> #include <ViewText.h>
#include <text.h> #include <text.h>
#include <util.h> #include <util.h>
#include <main.h> #include <main.h>
@ -83,32 +83,15 @@ int handleReportHistoryMonthly (std::string& outs)
} }
} }
// Now build the table. // Now build the view.
Table table; ViewText view;
table.setDateFormat (context.config.get ("dateformat")); view.width (context.getWidth ());
table.addColumn ("Year"); view.add (Column::factory ("string", "Year"));
table.addColumn ("Month"); view.add (Column::factory ("string", "Month"));
table.addColumn ("Added"); view.add (Column::factory ("string.right", "Added"));
table.addColumn ("Completed"); view.add (Column::factory ("string.right", "Completed"));
table.addColumn ("Deleted"); view.add (Column::factory ("string.right", "Deleted"));
table.addColumn ("Net"); view.add (Column::factory ("string.right", "Net"));
if (context.color () && context.config.getBoolean ("fontunderline"))
{
table.setColumnUnderline (0);
table.setColumnUnderline (1);
table.setColumnUnderline (2);
table.setColumnUnderline (3);
table.setColumnUnderline (4);
table.setColumnUnderline (5);
}
else
table.setTableDashedUnderline ();
table.setColumnJustification (2, Table::right);
table.setColumnJustification (3, Table::right);
table.setColumnJustification (4, Table::right);
table.setColumnJustification (5, Table::right);
int totalAdded = 0; int totalAdded = 0;
int totalCompleted = 0; int totalCompleted = 0;
@ -118,7 +101,7 @@ int handleReportHistoryMonthly (std::string& outs)
int row = 0; int row = 0;
foreach (i, groups) foreach (i, groups)
{ {
row = table.addRow (); row = view.addRow ();
totalAdded += addedGroup [i->first]; totalAdded += addedGroup [i->first];
totalCompleted += completedGroup [i->first]; totalCompleted += completedGroup [i->first];
@ -130,55 +113,60 @@ int handleReportHistoryMonthly (std::string& outs)
if (y != priorYear) if (y != priorYear)
{ {
table.addCell (row, 0, y); view.set (row, 0, y);
priorYear = y; priorYear = y;
} }
table.addCell (row, 1, Date::monthName(m)); view.set (row, 1, Date::monthName(m));
int net = 0; int net = 0;
if (addedGroup.find (i->first) != addedGroup.end ()) if (addedGroup.find (i->first) != addedGroup.end ())
{ {
table.addCell (row, 2, addedGroup[i->first]); view.set (row, 2, addedGroup[i->first]);
net +=addedGroup[i->first]; net +=addedGroup[i->first];
} }
if (completedGroup.find (i->first) != completedGroup.end ()) if (completedGroup.find (i->first) != completedGroup.end ())
{ {
table.addCell (row, 3, completedGroup[i->first]); view.set (row, 3, completedGroup[i->first]);
net -= completedGroup[i->first]; net -= completedGroup[i->first];
} }
if (deletedGroup.find (i->first) != deletedGroup.end ()) if (deletedGroup.find (i->first) != deletedGroup.end ())
{ {
table.addCell (row, 4, deletedGroup[i->first]); view.set (row, 4, deletedGroup[i->first]);
net -= deletedGroup[i->first]; net -= deletedGroup[i->first];
} }
table.addCell (row, 5, net); Color net_color;
if (context.color () && net) if (context.color () && net)
table.setCellColor (row, 5, net > 0 ? Color (Color::red) : net_color = net > 0
Color (Color::green)); ? Color (Color::red)
: Color (Color::green);
view.set (row, 5, net, net_color);
} }
if (table.rowCount ()) if (view.rows ())
{ {
table.addRow (); view.addRow ();
row = table.addRow (); row = view.addRow ();
table.addCell (row, 1, "Average"); Color row_color;
if (context.color ()) if (context.color ())
table.setRowColor (row, Color (Color::nocolor, Color::nocolor, false, true, false)); row_color = Color (Color::nocolor, Color::nocolor, false, true, false);
table.addCell (row, 2, totalAdded / (table.rowCount () - 2));
table.addCell (row, 3, totalCompleted / (table.rowCount () - 2)); view.set (row, 1, "Average", row_color);
table.addCell (row, 4, totalDeleted / (table.rowCount () - 2)); view.set (row, 2, totalAdded / (view.rows () - 2), row_color);
table.addCell (row, 5, (totalAdded - totalCompleted - totalDeleted) / (table.rowCount () - 2)); view.set (row, 3, totalCompleted / (view.rows () - 2), row_color);
view.set (row, 4, totalDeleted / (view.rows () - 2), row_color);
view.set (row, 5, (totalAdded - totalCompleted - totalDeleted) / (view.rows () - 2), row_color);
} }
std::stringstream out; std::stringstream out;
if (table.rowCount ()) if (view.rows ())
out << optionalBlankLine () out << optionalBlankLine ()
<< table.render () << view.render ()
<< "\n"; << "\n";
else else
{ {
@ -238,30 +226,14 @@ int handleReportHistoryAnnual (std::string& outs)
} }
} }
// Now build the table. // Now build the view.
Table table; ViewText view;
table.setDateFormat (context.config.get ("dateformat")); view.width (context.getWidth ());
table.addColumn ("Year"); view.add (Column::factory ("string", "Year"));
table.addColumn ("Added"); view.add (Column::factory ("string.right", "Added"));
table.addColumn ("Completed"); view.add (Column::factory ("string.right", "Completed"));
table.addColumn ("Deleted"); view.add (Column::factory ("string.right", "Deleted"));
table.addColumn ("Net"); view.add (Column::factory ("string.right", "Net"));
if (context.color () && context.config.getBoolean ("fontunderline"))
{
table.setColumnUnderline (0);
table.setColumnUnderline (1);
table.setColumnUnderline (2);
table.setColumnUnderline (3);
table.setColumnUnderline (4);
}
else
table.setTableDashedUnderline ();
table.setColumnJustification (1, Table::right);
table.setColumnJustification (2, Table::right);
table.setColumnJustification (3, Table::right);
table.setColumnJustification (4, Table::right);
int totalAdded = 0; int totalAdded = 0;
int totalCompleted = 0; int totalCompleted = 0;
@ -271,7 +243,7 @@ int handleReportHistoryAnnual (std::string& outs)
int row = 0; int row = 0;
foreach (i, groups) foreach (i, groups)
{ {
row = table.addRow (); row = view.addRow ();
totalAdded += addedGroup [i->first]; totalAdded += addedGroup [i->first];
totalCompleted += completedGroup [i->first]; totalCompleted += completedGroup [i->first];
@ -283,7 +255,7 @@ int handleReportHistoryAnnual (std::string& outs)
if (y != priorYear) if (y != priorYear)
{ {
table.addCell (row, 0, y); view.set (row, 0, y);
priorYear = y; priorYear = y;
} }
@ -291,46 +263,51 @@ int handleReportHistoryAnnual (std::string& outs)
if (addedGroup.find (i->first) != addedGroup.end ()) if (addedGroup.find (i->first) != addedGroup.end ())
{ {
table.addCell (row, 1, addedGroup[i->first]); view.set (row, 1, addedGroup[i->first]);
net +=addedGroup[i->first]; net +=addedGroup[i->first];
} }
if (completedGroup.find (i->first) != completedGroup.end ()) if (completedGroup.find (i->first) != completedGroup.end ())
{ {
table.addCell (row, 2, completedGroup[i->first]); view.set (row, 2, completedGroup[i->first]);
net -= completedGroup[i->first]; net -= completedGroup[i->first];
} }
if (deletedGroup.find (i->first) != deletedGroup.end ()) if (deletedGroup.find (i->first) != deletedGroup.end ())
{ {
table.addCell (row, 3, deletedGroup[i->first]); view.set (row, 3, deletedGroup[i->first]);
net -= deletedGroup[i->first]; net -= deletedGroup[i->first];
} }
table.addCell (row, 4, net); Color net_color;
if (context.color () && net) if (context.color () && net)
table.setCellColor (row, 4, net > 0 ? Color (Color::red) : net_color = net > 0
Color (Color::green)); ? Color (Color::red)
: Color (Color::green);
view.set (row, 4, net, net_color);
} }
if (table.rowCount ()) if (view.rows ())
{ {
table.addRow (); view.addRow ();
row = table.addRow (); row = view.addRow ();
table.addCell (row, 0, "Average"); Color row_color;
if (context.color ()) if (context.color ())
table.setRowColor (row, Color (Color::nocolor, Color::nocolor, false, true, false)); row_color = Color (Color::nocolor, Color::nocolor, false, true, false);
table.addCell (row, 1, totalAdded / (table.rowCount () - 2));
table.addCell (row, 2, totalCompleted / (table.rowCount () - 2)); view.set (row, 0, "Average", row_color);
table.addCell (row, 3, totalDeleted / (table.rowCount () - 2)); view.set (row, 1, totalAdded / (view.rows () - 2), row_color);
table.addCell (row, 4, (totalAdded - totalCompleted - totalDeleted) / (table.rowCount () - 2)); view.set (row, 2, totalCompleted / (view.rows () - 2), row_color);
view.set (row, 3, totalDeleted / (view.rows () - 2), row_color);
view.set (row, 4, (totalAdded - totalCompleted - totalDeleted) / (view.rows () - 2), row_color);
} }
std::stringstream out; std::stringstream out;
if (table.rowCount ()) if (view.rows ())
out << optionalBlankLine () out << optionalBlankLine ()
<< table.render () << view.render ()
<< "\n"; << "\n";
else else
{ {
@ -392,20 +369,12 @@ int handleReportGHistoryMonthly (std::string& outs)
int widthOfBar = context.getWidth () - 15; // 15 == strlen ("2008 September ") int widthOfBar = context.getWidth () - 15; // 15 == strlen ("2008 September ")
// Now build the table. // Now build the view.
Table table; ViewText view;
table.setDateFormat (context.config.get ("dateformat")); view.width (context.getWidth ());
table.addColumn ("Year"); view.add (Column::factory ("string", "Year"));
table.addColumn ("Month"); view.add (Column::factory ("string", "Month"));
table.addColumn ("Number Added/Completed/Deleted"); view.add (Column::factory ("string", "Number Added/Completed/Deleted"));
if (context.color () && context.config.getBoolean ("fontunderline"))
{
table.setColumnUnderline (0);
table.setColumnUnderline (1);
}
else
table.setTableDashedUnderline ();
Color color_add (context.config.get ("color.history.add")); Color color_add (context.config.get ("color.history.add"));
Color color_done (context.config.get ("color.history.done")); Color color_done (context.config.get ("color.history.done"));
@ -436,7 +405,7 @@ int handleReportGHistoryMonthly (std::string& outs)
int row = 0; int row = 0;
foreach (i, groups) foreach (i, groups)
{ {
row = table.addRow (); row = view.addRow ();
totalAdded += addedGroup[i->first]; totalAdded += addedGroup[i->first];
totalCompleted += completedGroup[i->first]; totalCompleted += completedGroup[i->first];
@ -448,10 +417,10 @@ int handleReportGHistoryMonthly (std::string& outs)
if (y != priorYear) if (y != priorYear)
{ {
table.addCell (row, 0, y); view.set (row, 0, y);
priorYear = y; priorYear = y;
} }
table.addCell (row, 1, Date::monthName(m)); view.set (row, 1, Date::monthName(m));
unsigned int addedBar = (widthOfBar * addedGroup[i->first]) / maxLine; unsigned int addedBar = (widthOfBar * addedGroup[i->first]) / maxLine;
unsigned int completedBar = (widthOfBar * completedGroup[i->first]) / maxLine; unsigned int completedBar = (widthOfBar * completedGroup[i->first]) / maxLine;
@ -500,15 +469,15 @@ int handleReportGHistoryMonthly (std::string& outs)
bar += aBar + cBar + dBar; bar += aBar + cBar + dBar;
} }
table.addCell (row, 2, bar); view.set (row, 2, bar);
} }
} }
std::stringstream out; std::stringstream out;
if (table.rowCount ()) if (view.rows ())
{ {
out << optionalBlankLine () out << optionalBlankLine ()
<< table.render () << view.render ()
<< "\n"; << "\n";
if (context.color ()) if (context.color ())
@ -583,18 +552,11 @@ int handleReportGHistoryAnnual (std::string& outs)
int widthOfBar = context.getWidth () - 5; // 5 == strlen ("2008 ") int widthOfBar = context.getWidth () - 5; // 5 == strlen ("2008 ")
// Now build the table. // Now build the view.
Table table; ViewText view;
table.setDateFormat (context.config.get ("dateformat")); view.width (context.getWidth ());
table.addColumn ("Year"); view.add (Column::factory ("string", "Year"));
table.addColumn ("Number Added/Completed/Deleted"); view.add (Column::factory ("string", "Number Added/Completed/Deleted"));
if (context.color () && context.config.getBoolean ("fontunderline"))
{
table.setColumnUnderline (0);
}
else
table.setTableDashedUnderline ();
Color color_add (context.config.get ("color.history.add")); Color color_add (context.config.get ("color.history.add"));
Color color_done (context.config.get ("color.history.done")); Color color_done (context.config.get ("color.history.done"));
@ -625,7 +587,7 @@ int handleReportGHistoryAnnual (std::string& outs)
int row = 0; int row = 0;
foreach (i, groups) foreach (i, groups)
{ {
row = table.addRow (); row = view.addRow ();
totalAdded += addedGroup[i->first]; totalAdded += addedGroup[i->first];
totalCompleted += completedGroup[i->first]; totalCompleted += completedGroup[i->first];
@ -637,7 +599,7 @@ int handleReportGHistoryAnnual (std::string& outs)
if (y != priorYear) if (y != priorYear)
{ {
table.addCell (row, 0, y); view.set (row, 0, y);
priorYear = y; priorYear = y;
} }
@ -687,15 +649,15 @@ int handleReportGHistoryAnnual (std::string& outs)
bar += aBar + cBar + dBar; bar += aBar + cBar + dBar;
} }
table.addCell (row, 1, bar); view.set (row, 1, bar);
} }
} }
std::stringstream out; std::stringstream out;
if (table.rowCount ()) if (view.rows ())
{ {
out << optionalBlankLine () out << optionalBlankLine ()
<< table.render () << view.render ()
<< "\n"; << "\n";
if (context.color ()) if (context.color ())

View file

@ -57,9 +57,9 @@ int shortUsage (std::string& outs)
{ {
ViewText view; ViewText view;
view.width (context.getWidth ()); view.width (context.getWidth ());
view.add (""); view.add (Column::factory ("string", ""));
view.add (""); view.add (Column::factory ("string", ""));
view.add (""); view.add (Column::factory ("string", ""));
int row = view.addRow (); int row = view.addRow ();
view.set (row, 0, "Usage:"); view.set (row, 0, "Usage:");
@ -1762,8 +1762,8 @@ int handleReportStats (std::string& outs)
ViewText view; ViewText view;
view.width (context.getWidth ()); view.width (context.getWidth ());
view.intraPadding (2); view.intraPadding (2);
view.add ("Category"); view.add (Column::factory ("string", "Category"));
view.add ("Data"); view.add (Column::factory ("string", "Data"));
int row = view.addRow (); int row = view.addRow ();
view.set (row, 0, "Pending"); view.set (row, 0, "Pending");

View file

@ -164,9 +164,9 @@ int main (int argc, char** argv)
string_view.intraColorOdd (odd_color); string_view.intraColorOdd (odd_color);
string_view.intraColorEven (even_color); string_view.intraColorEven (even_color);
string_view.add ("One"); string_view.add (Column::factory ("string", "One"));
string_view.add ("Two"); string_view.add (Column::factory ("string", "Two"));
string_view.add ("Three"); string_view.add (Column::factory ("string", "Three"));
int row = string_view.addRow (); int row = string_view.addRow ();
string_view.set (row, 0, "top left"); string_view.set (row, 0, "top left");
@ -177,7 +177,10 @@ int main (int argc, char** argv)
string_view.set (row, 0, "bottom left", single_cell); string_view.set (row, 0, "bottom left", single_cell);
string_view.set (row, 1, "bottom center, containing sufficient text that " string_view.set (row, 1, "bottom center, containing sufficient text that "
"wrapping will occur because it exceeds all " "wrapping will occur because it exceeds all "
"reasonable values for default width."); "reasonable values for default width. Even in a "
"very wide terminal window. Just look at the "
"lengths we must go to, to get passing unit tests "
"and not flaky tests.");
string_view.set (row, 2, "bottom right"); string_view.set (row, 2, "bottom right");
std::cout << string_view.render (); std::cout << string_view.render ();