Commands - projects, _projects

- Migraged handleProjects and handleCompletionProjects to CmdProjects.
This commit is contained in:
Paul Beckingham 2011-05-28 16:18:53 -04:00
parent a7bc09d487
commit 920e1c6c86
9 changed files with 229 additions and 130 deletions

View file

@ -17,6 +17,7 @@ set (commands_SRCS Command.cpp Command.h
CmdInfo.cpp CmdInfo.h
CmdInstall.cpp CmdInstall.h
CmdLogo.cpp CmdLogo.h
CmdProjects.cpp CmdProjects.h
CmdShell.cpp CmdShell.h
CmdShow.cpp CmdShow.h
CmdStatistics.cpp CmdStatistics.h

View file

@ -151,10 +151,6 @@ int CmdHelp::execute (const std::string& command_line, std::string& output)
view.set (row, 1, "task done ID [tags] [attrs] [desc...]");
view.set (row, 2, "Marks the specified task as completed.");
row = view.addRow ();
view.set (row, 1, "task projects");
view.set (row, 2, "Shows a list of all project names used, and how many tasks are in each.");
row = view.addRow ();
view.set (row, 1, "task summary");
view.set (row, 2, "Shows a report of task status by project.");

View file

@ -0,0 +1,174 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <sstream>
#include <Context.h>
#include <ViewText.h>
#include <text.h>
#include <CmdProjects.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
CmdProjects::CmdProjects ()
{
_keyword = "projects";
_usage = "task projects [<filter>]";
_description = "Shows a list of all project names used, and how many tasks are in each";
_read_only = true;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdProjects::execute (const std::string& command_line, std::string& output)
{
int rc = 0;
std::stringstream out;
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
int quantity;
if (context.config.getBoolean ("list.all.projects"))
quantity = context.tdb.load (tasks, context.filter);
else
quantity = context.tdb.loadPending (tasks, context.filter);
context.tdb.commit ();
context.tdb.unlock ();
// Scan all the tasks for their project name, building a map using project
// names as keys.
std::map <std::string, int> unique;
std::map <std::string, int> high;
std::map <std::string, int> medium;
std::map <std::string, int> low;
std::map <std::string, int> none;
bool no_project = false;
std::string project;
std::string priority;
std::vector <Task>::iterator task;
for (task = tasks.begin (); task != tasks.end (); ++task)
{
project = task->get ("project");
priority = task->get ("priority");
unique[project] += 1;
if (project == "")
no_project = true;
if (priority == "H") high[project] += 1;
else if (priority == "M") medium[project] += 1;
else if (priority == "L") low[project] += 1;
else none[project] += 1;
}
if (unique.size ())
{
// Render a list of project names from the map.
ViewText view;
view.width (context.getWidth ());
view.add (Column::factory ("string", "Project"));
view.add (Column::factory ("string.right", "Tasks"));
view.add (Column::factory ("string.right", "Pri:None"));
view.add (Column::factory ("string.right", "Pri:L"));
view.add (Column::factory ("string.right", "Pri:M"));
view.add (Column::factory ("string.right", "Pri:H"));
std::map <std::string, int>::iterator project;
for (project = unique.begin (); project != unique.end (); ++project)
{
int row = view.addRow ();
view.set (row, 0, (project->first == "" ? "(none)" : project->first));
view.set (row, 1, project->second);
view.set (row, 2, none[project->first]);
view.set (row, 3, low[project->first]);
view.set (row, 4, medium[project->first]);
view.set (row, 5, high[project->first]);
}
int number_projects = unique.size ();
if (no_project)
--number_projects;
out << optionalBlankLine ()
<< view.render ()
<< optionalBlankLine ()
<< number_projects
<< (number_projects == 1 ? " project" : " projects")
<< " (" << quantity << (quantity == 1 ? " task" : " tasks") << ")\n";
}
else
{
out << "No projects.\n";
rc = 1;
}
output = out.str ();
return rc;
}
////////////////////////////////////////////////////////////////////////////////
CmdCompletionProjects::CmdCompletionProjects ()
{
_keyword = "_projects";
_usage = "task _projects [<filter>]";
_description = "Shows only a list of all project names used";
_read_only = true;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdCompletionProjects::execute (const std::string& command_line, std::string& output)
{
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
Filter filter;
if (context.config.getBoolean ("complete.all.projects"))
context.tdb.load (tasks, filter);
else
context.tdb.loadPending (tasks, filter);
context.tdb.commit ();
context.tdb.unlock ();
// Scan all the tasks for their project name, building a map using project
// names as keys.
std::map <std::string, int> unique;
std::vector <Task>::iterator task;
for (task = tasks.begin (); task != tasks.end (); ++task)
unique[task->get ("project")] = 0;
std::map <std::string, int>::iterator project;
for (project = unique.begin (); project != unique.end (); ++project)
if (project->first.length ())
output += project->first + "\n";
return 0;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,49 @@
////////////////////////////////////////////////////////////////////////////////
// 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_CMDPROJECTS
#define INCLUDED_CMDPROJECTS
#define L10N // Localization complete.
#include <string>
#include <Command.h>
class CmdProjects : public Command
{
public:
CmdProjects ();
int execute (const std::string&, std::string&);
};
class CmdCompletionProjects : public Command
{
public:
CmdCompletionProjects ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -39,6 +39,7 @@
#include <CmdInfo.h>
#include <CmdInstall.h>
#include <CmdLogo.h>
#include <CmdProjects.h>
#include <CmdShell.h>
#include <CmdShow.h>
#include <CmdStatistics.h>
@ -57,6 +58,7 @@ void Command::factory (std::map <std::string, Command*>& all)
c = new CmdCompletionCommands (); all[c->keyword ()] = c;
c = new CmdCompletionIds (); all[c->keyword ()] = c;
c = new CmdCompletionProjects (); all[c->keyword ()] = c;
c = new CmdCompletionTags (); all[c->keyword ()] = c;
c = new CmdCompletionVersion (); all[c->keyword ()] = c;
c = new CmdCount (); all[c->keyword ()] = c;
@ -68,6 +70,7 @@ void Command::factory (std::map <std::string, Command*>& all)
c = new CmdInfo (); all[c->keyword ()] = c;
c = new CmdInstall (); all[c->keyword ()] = c;
c = new CmdLogo (); all[c->keyword ()] = c;
c = new CmdProjects (); all[c->keyword ()] = c;
c = new CmdShell (); all[c->keyword ()] = c;
c = new CmdShow (); all[c->keyword ()] = c;
c = new CmdStatistics (); all[c->keyword ()] = c;