diff --git a/src/columns/CMakeLists.txt b/src/columns/CMakeLists.txt index 21c141fbe..42b467c0f 100644 --- a/src/columns/CMakeLists.txt +++ b/src/columns/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories (${CMAKE_SOURCE_DIR}/src set (columns_SRCS Column.cpp Column.h ColID.cpp ColID.h + ColPriority.cpp ColPriority.h ColProject.cpp ColProject.h) add_library (columns STATIC ${columns_SRCS}) diff --git a/src/columns/ColID.cpp b/src/columns/ColID.cpp index 9dd737937..9c2284f14 100644 --- a/src/columns/ColID.cpp +++ b/src/columns/ColID.cpp @@ -58,6 +58,9 @@ void ColumnID::measure (Task& task, int& minimum, int& maximum) else length = (int) log10 ((double) task.id); // Slow minimum = maximum = length; + + if (_style != "default") + throw std::string ("Unrecognized column format '") + _type + "." + _style + "'"; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/ColID.h b/src/columns/ColID.h index da25805ac..159cb8c28 100644 --- a/src/columns/ColID.h +++ b/src/columns/ColID.h @@ -24,8 +24,8 @@ // USA // //////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDED_ID -#define INCLUDED_ID +#ifndef INCLUDED_COLID +#define INCLUDED_COLID #include #include diff --git a/src/columns/ColPriority.cpp b/src/columns/ColPriority.cpp new file mode 100644 index 000000000..c8dd8d9dc --- /dev/null +++ b/src/columns/ColPriority.cpp @@ -0,0 +1,93 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +extern Context context; + +//////////////////////////////////////////////////////////////////////////////// +ColumnPriority::ColumnPriority () +{ + _type = "string"; + _style = "default"; + _label = "Pri"; +} + +//////////////////////////////////////////////////////////////////////////////// +ColumnPriority::~ColumnPriority () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +// Overriden so that style <----> label are linked. +// Note that you can not determine which gets called first. +void ColumnPriority::setStyle (const std::string& value) +{ + _style = value; + + if (_style == "long" && _label == "Pri") + _label = "Priority"; +} + +//////////////////////////////////////////////////////////////////////////////// +// Set the minimum and maximum widths for the value. +void ColumnPriority::measure (Task& task, int& minimum, int& maximum) +{ + std::string priority = task.get ("priority"); + + minimum = maximum = 1; + if (_style == "long") + { + if (priority == "H") minimum = maximum = 4; + else if (priority == "M") minimum = maximum = 6; + else if (priority == "L") minimum = maximum = 3; + } + else if (_style != "default") + throw std::string ("Unrecognized column format '") + _type + "." + _style + "'"; +} + +//////////////////////////////////////////////////////////////////////////////// +void ColumnPriority::render ( + std::vector & lines, + Task& task, + int width, + Color& color) +{ + std::string priority = task.get ("priority"); + if (_style == "long") + { + if (priority == "H") priority = "High"; + else if (priority == "M") priority = "Medium"; + else if (priority == "L") priority = "Low"; + } + + lines.push_back (color.colorize (leftJustify (priority, width))); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/ColPriority.h b/src/columns/ColPriority.h new file mode 100644 index 000000000..c827eb7a5 --- /dev/null +++ b/src/columns/ColPriority.h @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_COLPRIORITY +#define INCLUDED_COLPRIORITY + +#include +#include +#include +#include +#include + +class ColumnPriority : public Column +{ +public: + ColumnPriority (); + ~ColumnPriority (); + + void setStyle (const std::string&); + void measure (Task&, int&, int&); + void render (std::vector &, Task&, int, Color&); + +private: +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/ColProject.cpp b/src/columns/ColProject.cpp index 7fd208c6c..72b7a0769 100644 --- a/src/columns/ColProject.cpp +++ b/src/columns/ColProject.cpp @@ -50,6 +50,16 @@ ColumnProject::~ColumnProject () void ColumnProject::measure (Task& task, int& minimum, int& maximum) { std::string project = task.get ("project"); + + if (_style == "parent") + { + std::string::size_type period = project.find ('.'); + if (period != std::string::npos) + project = project.substr (0, period); + } + else if (_style != "default") + throw std::string ("Unrecognized column format '") + _type + "." + _style + "'"; + minimum = 0; maximum = project.length (); @@ -70,14 +80,20 @@ void ColumnProject::render ( int width, Color& color) { - // TODO Can't use Nibbler here. Need to use a UTF8-safe version of extractLines. - Nibbler nibbler (task.get ("project")); - std::string word; - while (nibbler.getUntilWS (word)) + std::string project = task.get ("project"); + if (_style == "parent") { - nibbler.skipWS (); - lines.push_back (color.colorize (leftJustify (word, width))); + std::string::size_type period = project.find ('.'); + if (period != std::string::npos) + project = project.substr (0, period); } + + std::vector raw; + wrapText (raw, project, width); + + std::vector ::iterator i; + for (i = raw.begin (); i != raw.end (); ++i) + lines.push_back (color.colorize (leftJustify (*i, width))); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/ColProject.h b/src/columns/ColProject.h index 69c73bb9f..c6b34c226 100644 --- a/src/columns/ColProject.h +++ b/src/columns/ColProject.h @@ -24,8 +24,8 @@ // USA // //////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDED_PROJECT -#define INCLUDED_PROJECT +#ifndef INCLUDED_COLPROJECT +#define INCLUDED_COLPROJECT #include #include diff --git a/src/columns/Column.cpp b/src/columns/Column.cpp index fb8dcf2ec..d59663ef2 100644 --- a/src/columns/Column.cpp +++ b/src/columns/Column.cpp @@ -28,19 +28,34 @@ #include #include #include +#include #include #include extern Context context; //////////////////////////////////////////////////////////////////////////////// +// TODO Supports the new complete column definition: +// +// [.][.[.][.]] +// Column* Column::factory (const std::string& name) { - if (name == "id") return new ColumnID (); - if (name == "project") return new ColumnProject (); + // TODO Decompose name into type, format, key, direction and break. - throw std::string ("Unrecognized column type '") + name + "'"; - return NULL; + Column* column; + if (name == "id") column = new ColumnID (); + else if (name == "priority") column = new ColumnPriority (); + else if (name == "project") column = new ColumnProject (); + else + throw std::string ("Unrecognized column type '") + name + "'"; + + // TODO Set format. + // TODO Set key. + // TODO Set direction. + // TODO Set break. + + return column; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/Column.h b/src/columns/Column.h index 3fa0e21d4..3e06866d5 100644 --- a/src/columns/Column.h +++ b/src/columns/Column.h @@ -45,10 +45,11 @@ public: std::string getStyle () { return _style; } std::string getLabel () { return _label; } - void setStyle (const std::string& value) { _style = value; } - void setLabel (const std::string& value) { _label = value; } std::string type () const { return _type; } + virtual void setStyle (const std::string& value) { _style = value; } + virtual void setLabel (const std::string& value) { _label = value; } + virtual void measure (Task&, int&, int&) = 0; virtual void renderHeader (std::vector &, int, Color&); virtual void render (std::vector &, Task&, int, Color&) = 0;