diff --git a/src/View.cpp b/src/View.cpp index b3e9f4542..27f18da11 100644 --- a/src/View.cpp +++ b/src/View.cpp @@ -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 & data, std::vector & sequence) { // Determine minimal, ideal column widths. @@ -70,8 +73,9 @@ std::string View::render (std::vector & data, std::vector & sequence) std::vector ::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 ::iterator d; for (d = data.begin (); d != data.end (); ++d) @@ -88,13 +92,65 @@ std::string View::render (std::vector & data, std::vector & 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 ::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 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 & data, std::vector & sequence) std::vector ::iterator s; for (s = sequence.begin (); s != sequence.end (); ++s) { + // TODO render each row. } return output.str (); diff --git a/src/columns/ColID.cpp b/src/columns/ColID.cpp index c292e3799..22d13a3d0 100644 --- a/src/columns/ColID.cpp +++ b/src/columns/ColID.cpp @@ -25,6 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// +#include // TODO Remove #include #include #include @@ -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 & lines, Task* task, int width) +void ColumnID::render (std::vector & 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 << '-'; diff --git a/src/columns/ColID.h b/src/columns/ColID.h index 16080f2ce..401d39ae0 100644 --- a/src/columns/ColID.h +++ b/src/columns/ColID.h @@ -39,7 +39,7 @@ public: ~ColumnID (); void measure (Task&, int&, int&); - void render (std::vector &, Task*, int); + void render (std::vector &, Task&, int); private: }; diff --git a/src/columns/ColProject.cpp b/src/columns/ColProject.cpp index 8930666cf..7fbbf6409 100644 --- a/src/columns/ColProject.cpp +++ b/src/columns/ColProject.cpp @@ -25,6 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// +#include // TODO Remove #include #include #include @@ -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 & lines, Task* task, int width) +void ColumnProject::render (std::vector & lines, Task& task, int width) { } diff --git a/src/columns/ColProject.h b/src/columns/ColProject.h index 02209af60..06bc4ad6b 100644 --- a/src/columns/ColProject.h +++ b/src/columns/ColProject.h @@ -39,7 +39,7 @@ public: ~ColumnProject (); void measure (Task&, int&, int&); - void render (std::vector &, Task*, int); + void render (std::vector &, Task&, int); private: }; diff --git a/src/columns/Column.h b/src/columns/Column.h index 4cde052a8..3c41581f5 100644 --- a/src/columns/Column.h +++ b/src/columns/Column.h @@ -50,7 +50,7 @@ public: virtual void measure (Task&, int&, int&) = 0; virtual void renderHeader (std::vector &, int); - virtual void render (std::vector &, Task*, int) = 0; + virtual void render (std::vector &, Task&, int) = 0; protected: std::string _type; diff --git a/test/view.t.cpp b/test/view.t.cpp index 3b5613b1f..50c283bc6 100644 --- a/test/view.t.cpp +++ b/test/view.t.cpp @@ -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 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 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)