From 8a87db6163c2cbb887a03c4a8539bde8ee3e0ce6 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 30 Apr 2011 00:06:07 -0400 Subject: [PATCH] View - Implemented tags.default, tags.indicator, tags.count. --- src/columns/CMakeLists.txt | 1 + src/columns/ColTags.cpp | 125 +++++++++++++++++++++++++++++++++++++ src/columns/ColTags.h | 50 +++++++++++++++ src/columns/Column.cpp | 2 + test/view.t.cpp | 6 +- 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 src/columns/ColTags.cpp create mode 100644 src/columns/ColTags.h diff --git a/src/columns/CMakeLists.txt b/src/columns/CMakeLists.txt index 9de62d8a2..5d8e07b79 100644 --- a/src/columns/CMakeLists.txt +++ b/src/columns/CMakeLists.txt @@ -8,6 +8,7 @@ set (columns_SRCS Column.cpp Column.h ColID.cpp ColID.h ColPriority.cpp ColPriority.h ColProject.cpp ColProject.h + ColTags.cpp ColTags.h ColUUID.cpp ColUUID.h) add_library (columns STATIC ${columns_SRCS}) diff --git a/src/columns/ColTags.cpp b/src/columns/ColTags.cpp new file mode 100644 index 000000000..30a48edc7 --- /dev/null +++ b/src/columns/ColTags.cpp @@ -0,0 +1,125 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 +#include +#include + +extern Context context; + +//////////////////////////////////////////////////////////////////////////////// +ColumnTags::ColumnTags () +{ + _type = "string"; + _style = "default"; + _label = "Tags"; +} + +//////////////////////////////////////////////////////////////////////////////// +ColumnTags::~ColumnTags () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +// Overriden so that style <----> label are linked. +// Note that you can not determine which gets called first. +void ColumnTags::setStyle (const std::string& value) +{ + _style = value; + + if (_style == "indicator" && _label == "Tags") _label = _label.substr (0, context.config.get ("tag.indicator").length ()); + else if (_style == "count" && _label == "Tags") _label = "Tag"; +} + +//////////////////////////////////////////////////////////////////////////////// +// Set the minimum and maximum widths for the value. +void ColumnTags::measure (Task& task, int& minimum, int& maximum) +{ + + if (_style == "indicator") minimum = maximum = context.config.get ("tag.indicator").length (); + else if (_style == "count") minimum = maximum = 3; + else if (_style == "default") + { + std::string tags = task.get ("tags"); + minimum = 0; + maximum = tags.length (); + + if (maximum) + { + std::vector all; + split (all, tags, ','); + std::vector ::iterator i; + for (i = all.begin (); i != all.end (); ++i) + if (i->length () > minimum) + minimum = i->length () + 1; + } + } + else + throw std::string ("Unrecognized column format '") + _type + "." + _style + "'"; + +} + +//////////////////////////////////////////////////////////////////////////////// +void ColumnTags::render ( + std::vector & lines, + Task& task, + int width, + Color& color) +{ + std::string tags = task.get ("tags"); + if (tags != "") + { + if (_style == "indicator") + { + lines.push_back ( + color.colorize ( + rightJustify (context.config.get ("tag.indicator"), width))); + } + else if (_style == "count") + { + std::vector all; + split (all, tags, ','); + lines.push_back ( + color.colorize ( + rightJustify ("[" + format ((int)all.size ()) + "]", width))); + } + else if (_style == "default") + { + std::replace (tags.begin (), tags.end (), ',', ' '); + std::vector all; + wrapText (all, tags, width); + + std::vector ::iterator i; + for (i = all.begin (); i != all.end (); ++i) + lines.push_back (color.colorize (rightJustify (*i, width))); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/ColTags.h b/src/columns/ColTags.h new file mode 100644 index 000000000..206852cae --- /dev/null +++ b/src/columns/ColTags.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_COLTAGS +#define INCLUDED_COLTAGS + +#include +#include +#include +#include +#include + +class ColumnTags : public Column +{ +public: + ColumnTags (); + ~ColumnTags (); + + void setStyle (const std::string&); + void measure (Task&, int&, int&); + void render (std::vector &, Task&, int, Color&); + +private: +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/columns/Column.cpp b/src/columns/Column.cpp index fcff01070..b5d9dee9f 100644 --- a/src/columns/Column.cpp +++ b/src/columns/Column.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -61,6 +62,7 @@ Column* Column::factory (const std::string& name) if (column_name == "id") column = new ColumnID (); else if (column_name == "priority") column = new ColumnPriority (); else if (column_name == "project") column = new ColumnProject (); + else if (column_name == "tags") column = new ColumnTags (); else if (column_name == "uuid") column = new ColumnUUID (); else throw std::string ("Unrecognized column type '") + column_name + "'"; diff --git a/test/view.t.cpp b/test/view.t.cpp index 611e32412..1ba6c44a0 100644 --- a/test/view.t.cpp +++ b/test/view.t.cpp @@ -42,9 +42,10 @@ int main (int argc, char** argv) { // Set up configuration. context.config.set ("fontunderline", true); + context.config.set ("tag.indicator", "+"); // Two sample tasks. - Task t1 ("[uuid:\"2a64f6e0-bf8e-430d-bf71-9ec3f0d9b661\" project:\"Home\" priority:\"H\"]"); + Task t1 ("[uuid:\"2a64f6e0-bf8e-430d-bf71-9ec3f0d9b661\" project:\"Home\" priority:\"H\" tags:\"one,two\"]"); t1.id = 1; Task t2 ("[uuid:\"f30cb9c3-3fc0-483f-bfb2-3bf134f00694\" project:\"Garden Care\"]"); t2.id = 11; @@ -67,6 +68,9 @@ int main (int argc, char** argv) view.add (Column::factory ("uuid.short")); view.add (Column::factory ("project")); view.add (Column::factory ("priority.long")); + view.add (Column::factory ("tags")); + view.add (Column::factory ("tags.indicator")); + view.add (Column::factory ("tags.count")); view.width (80); view.leftMargin (4); view.extraPadding (0);