- Implemented column sizing algorithm.
This commit is contained in:
Paul Beckingham 2011-04-27 01:50:32 -04:00
parent 29649bdf07
commit 9f672d0b06
7 changed files with 94 additions and 16 deletions

View file

@ -36,7 +36,7 @@ View::View ()
, _left_margin (0)
, _odd (0)
, _even (0)
, _intra_padding (0)
, _intra_padding (1)
, _intra_odd (0)
, _intra_even (0)
, _extra_padding (0)
@ -61,6 +61,9 @@ View::~View ()
// |ma|ex|cell |in|cell |in|cell |ex|
// +--+--+-------+--+-------+--+-------+--+
//
// margin
// extrapadding
// intrapadding
std::string View::render (std::vector <Task>& data, std::vector <int>& sequence)
{
// Determine minimal, ideal column widths.
@ -70,8 +73,9 @@ std::string View::render (std::vector <Task>& data, std::vector <int>& sequence)
std::vector <Column*>::iterator i;
for (i = _columns.begin (); i != _columns.end (); ++i)
{
// Headers factor in to width calculations.
int global_min = characters ((*i)->getLabel ());
int global_ideal = 0;
int global_ideal = global_min;
std::vector <Task>::iterator d;
for (d = data.begin (); d != data.end (); ++d)
@ -88,13 +92,65 @@ std::string View::render (std::vector <Task>& data, std::vector <int>& sequence)
ideal.push_back (global_ideal);
}
// TODO Remove
std::string combined;
join (combined, ",", minimal);
std::cout << "# minimal " << combined << "\n";
join (combined, ",", ideal);
std::cout << "# ideal " << combined << "\n";
// TODO Calculate final column widths.
// Sum the minimal widths.
int sum_minimal = 0;
std::vector <int>::iterator c;
for (c = minimal.begin (); c != minimal.end (); ++c)
sum_minimal += *c;
std::cout << "# sum_minimal " << sum_minimal << "\n";
// Sum the ideal widths.
int sum_ideal = 0;
for (c = ideal.begin (); c != ideal.end (); ++c)
sum_ideal += *c;
std::cout << "# sum_ideal " << sum_ideal << "\n";
// Calculate final column widths.
int overage = _width
- _left_margin
- (2 * _extra_padding)
- ((_columns.size () - 1) * _intra_padding);
std::cout << "# width " << _width << "\n";
std::vector <int> widths;
if (sum_ideal <= overage)
{
std::cout << "# ideal case: " << sum_ideal << " <= " << overage << "\n";
widths = ideal;
}
else if (sum_minimal > overage)
{
throw std::string ("There is not enough horizontal width to display the results.");
}
else
{
widths = minimal;
overage -= sum_minimal;
std::cout << "# overage " << overage << "\n";
// Spread 'overage' among columns where width[i] < ideal[i]
while (overage)
{
for (int i = 0; i < _columns.size () && overage; ++i)
{
if (widths[i] < ideal[i])
{
++widths[i];
--overage;
}
}
}
join (combined, ",", widths);
std::cout << "# final widths " << combined << "\n";
}
// TODO Compose column headers.
@ -105,6 +161,7 @@ std::string View::render (std::vector <Task>& data, std::vector <int>& sequence)
std::vector <int>::iterator s;
for (s = sequence.begin (); s != sequence.end (); ++s)
{
// TODO render each row.
}
return output.str ();

View file

@ -25,6 +25,7 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream> // TODO Remove
#include <iomanip>
#include <sstream>
#include <math.h>
@ -60,16 +61,18 @@ void ColumnID::measure (Task& task, int& minimum, int& maximum)
else length = (int) log10 ((double) task.id); // Slow
minimum = maximum = length;
std::cout << "# ColID::measure id=" << task.id << " min=" << minimum << " max=" << maximum << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void ColumnID::render (std::vector <std::string>& lines, Task* task, int width)
void ColumnID::render (std::vector <std::string>& lines, Task& task, int width)
{
std::stringstream line;
line << std::setw (width) << std::setfill (' ') << task->id;
line << std::setw (width) << std::setfill (' ') << task.id;
if (task->id)
line << task->id;
if (task.id)
line << task.id;
else
line << '-';

View file

@ -39,7 +39,7 @@ public:
~ColumnID ();
void measure (Task&, int&, int&);
void render (std::vector <std::string>&, Task*, int);
void render (std::vector <std::string>&, Task&, int);
private:
};

View file

@ -25,6 +25,7 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream> // TODO Remove
#include <math.h>
#include <Context.h>
#include <ColProject.h>
@ -51,12 +52,19 @@ void ColumnProject::measure (Task& task, int& minimum, int& maximum)
std::string project = task.get ("project");
minimum = maximum = project.length ();
std::string::size_type space = project.find (' ');
if (space == std::string::npos)
{
std::cout << "# ColProject::measure project=" << project << " min=" << minimum << " max=" << maximum << "\n";
return;
}
minimum = 0;
int longest = 0;
std::string::size_type last = -1;
std::string::size_type space = project.find (' ');
while (space != std::string::npos)
{
if (space - last - 1 > minimum)
if (space - last - 1 > longest)
longest = space - last - 1;
last = space;
@ -65,10 +73,12 @@ void ColumnProject::measure (Task& task, int& minimum, int& maximum)
if (longest)
minimum = longest;
std::cout << "# ColProject::measure project=" << project << " min=" << minimum << " max=" << maximum << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void ColumnProject::render (std::vector <std::string>& lines, Task* task, int width)
void ColumnProject::render (std::vector <std::string>& lines, Task& task, int width)
{
}

View file

@ -39,7 +39,7 @@ public:
~ColumnProject ();
void measure (Task&, int&, int&);
void render (std::vector <std::string>&, Task*, int);
void render (std::vector <std::string>&, Task&, int);
private:
};

View file

@ -50,7 +50,7 @@ public:
virtual void measure (Task&, int&, int&) = 0;
virtual void renderHeader (std::vector <std::string>&, int);
virtual void render (std::vector <std::string>&, Task*, int) = 0;
virtual void render (std::vector <std::string>&, Task&, int) = 0;
protected:
std::string _type;

View file

@ -41,9 +41,14 @@ int main (int argc, char** argv)
try
{
// Two sample tasks.
Task t1 ("[project:\"Home\"]");
t1.id = 1;
Task t2 ("[project:\"Garden Care\"]");
t2.id = 11;
std::vector <Task> data;
data.push_back (Task ("[description:\"Migrate import out of core\" entry:\"1303155011\" project:\"task-2.1\" status:\"pending\" uuid:\"3bb54f40-c38f-4936-aae6-f67de6227e00\"]"));
data.push_back (Task ("[annotation_1303444800:\"Uli Martens\" description:\"New command: task show defaults, that dumps the Config.cpp defaults string, as an example of a complete .taskrc file\" entry:\"1303472714\" project:\"task-2.0\" status:\"pending\" uuid:\"f30cb9c3-3fc0-483f-bfb2-3bf134f00694\"]"));
data.push_back (t1);
data.push_back (t2);
// Sequence of tasks.
std::vector <int> sequence;
@ -54,7 +59,10 @@ int main (int argc, char** argv)
View view;
view.add (Column::factory ("id"));
view.add (Column::factory ("project"));
view.width (40);
view.width (12);
view.leftMargin (0);
view.extraPadding (0);
view.intraPadding (1);
// Render the view.
std::cout << view.render (data, sequence)