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

@@ -129,7 +129,6 @@ void Cmd::load ()
{ {
if (commands.size () == 0) if (commands.size () == 0)
{ {
commands.push_back ("_projects");
commands.push_back ("_config"); commands.push_back ("_config");
commands.push_back ("_query"); commands.push_back ("_query");
commands.push_back ("export.csv"); commands.push_back ("export.csv");
@@ -155,7 +154,6 @@ void Cmd::load ()
commands.push_back ("import"); commands.push_back ("import");
commands.push_back ("log"); commands.push_back ("log");
commands.push_back ("prepend"); commands.push_back ("prepend");
commands.push_back ("projects");
commands.push_back ("start"); commands.push_back ("start");
commands.push_back ("stop"); commands.push_back ("stop");
commands.push_back ("summary"); commands.push_back ("summary");
@@ -220,8 +218,7 @@ void Cmd::allCommands (std::vector <std::string>& all) const
// Commands that do not directly modify the data files. // Commands that do not directly modify the data files.
bool Cmd::isReadOnlyCommand () bool Cmd::isReadOnlyCommand ()
{ {
if (command == "_projects" || if (command == "_config" ||
command == "_config" ||
command == "_query" || command == "_query" ||
command == "export.csv" || command == "export.csv" ||
command == "export.ical" || command == "export.ical" ||
@@ -236,7 +233,6 @@ bool Cmd::isReadOnlyCommand ()
command == "calendar" || command == "calendar" ||
command == "colors" || command == "colors" ||
command == "config" || command == "config" ||
command == "projects" ||
command == "push" || command == "push" ||
command == "summary" || command == "summary" ||
command == "timesheet" || command == "timesheet" ||

View File

@@ -247,8 +247,7 @@ int Context::dispatch (std::string &out)
Timer t ("Context::dispatch"); Timer t ("Context::dispatch");
// TODO Chain-of-command pattern dispatch. // TODO Chain-of-command pattern dispatch.
if (cmd.command == "projects") { rc = handleProjects (out); } if (cmd.command == "colors") { rc = handleColor (out); }
else if (cmd.command == "colors") { rc = handleColor (out); }
else if (cmd.command == "config") { rc = handleConfig (out); } else if (cmd.command == "config") { rc = handleConfig (out); }
else if (cmd.command == "history.monthly") { rc = handleReportHistoryMonthly (out); } else if (cmd.command == "history.monthly") { rc = handleReportHistoryMonthly (out); }
else if (cmd.command == "history.annual") { rc = handleReportHistoryAnnual (out); } else if (cmd.command == "history.annual") { rc = handleReportHistoryAnnual (out); }
@@ -280,7 +279,6 @@ int Context::dispatch (std::string &out)
handleMerge (out); } handleMerge (out); }
else if (cmd.command == "push") { handlePush (out); } else if (cmd.command == "push") { handlePush (out); }
else if (cmd.command == "pull") { handlePull (out); } else if (cmd.command == "pull") { handlePull (out); }
else if (cmd.command == "_projects") { rc = handleCompletionProjects (out); }
else if (cmd.command == "_config") { rc = handleCompletionConfig (out); } else if (cmd.command == "_config") { rc = handleCompletionConfig (out); }
else if (cmd.command == "_query") { rc = handleQuery (out); } else if (cmd.command == "_query") { rc = handleQuery (out); }
else if (cmd.command == "" && else if (cmd.command == "" &&

View File

@@ -215,122 +215,6 @@ int handleLog (std::string& outs)
return rc; return rc;
} }
////////////////////////////////////////////////////////////////////////////////
int handleProjects (std::string& outs)
{
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;
foreach (t, tasks)
{
project = t->get ("project");
priority = t->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"));
foreach (i, unique)
{
int row = view.addRow ();
view.set (row, 0, (i->first == "" ? "(none)" : i->first));
view.set (row, 1, i->second);
view.set (row, 2, none[i->first]);
view.set (row, 3, low[i->first]);
view.set (row, 4, medium[i->first]);
view.set (row, 5, high[i->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;
}
outs = out.str ();
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handleCompletionProjects (std::string& outs)
{
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;
foreach (t, tasks)
unique[t->get ("project")] = 0;
std::stringstream out;
foreach (project, unique)
if (project->first.length ())
out << project->first << "\n";
outs = out.str ();
return 0;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int handleCompletionConfig (std::string& outs) int handleCompletionConfig (std::string& outs)
{ {

View File

@@ -17,6 +17,7 @@ set (commands_SRCS Command.cpp Command.h
CmdInfo.cpp CmdInfo.h CmdInfo.cpp CmdInfo.h
CmdInstall.cpp CmdInstall.h CmdInstall.cpp CmdInstall.h
CmdLogo.cpp CmdLogo.h CmdLogo.cpp CmdLogo.h
CmdProjects.cpp CmdProjects.h
CmdShell.cpp CmdShell.h CmdShell.cpp CmdShell.h
CmdShow.cpp CmdShow.h CmdShow.cpp CmdShow.h
CmdStatistics.cpp CmdStatistics.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, 1, "task done ID [tags] [attrs] [desc...]");
view.set (row, 2, "Marks the specified task as completed."); 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 (); row = view.addRow ();
view.set (row, 1, "task summary"); view.set (row, 1, "task summary");
view.set (row, 2, "Shows a report of task status by project."); 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 <CmdInfo.h>
#include <CmdInstall.h> #include <CmdInstall.h>
#include <CmdLogo.h> #include <CmdLogo.h>
#include <CmdProjects.h>
#include <CmdShell.h> #include <CmdShell.h>
#include <CmdShow.h> #include <CmdShow.h>
#include <CmdStatistics.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 CmdCompletionCommands (); all[c->keyword ()] = c;
c = new CmdCompletionIds (); 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 CmdCompletionTags (); all[c->keyword ()] = c;
c = new CmdCompletionVersion (); all[c->keyword ()] = c; c = new CmdCompletionVersion (); all[c->keyword ()] = c;
c = new CmdCount (); 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 CmdInfo (); all[c->keyword ()] = c;
c = new CmdInstall (); all[c->keyword ()] = c; c = new CmdInstall (); all[c->keyword ()] = c;
c = new CmdLogo (); 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 CmdShell (); all[c->keyword ()] = c;
c = new CmdShow (); all[c->keyword ()] = c; c = new CmdShow (); all[c->keyword ()] = c;
c = new CmdStatistics (); all[c->keyword ()] = c; c = new CmdStatistics (); all[c->keyword ()] = c;

View File

@@ -56,8 +56,6 @@ int handleAppend (std::string&);
int handlePrepend (std::string&); int handlePrepend (std::string&);
int handleDone (std::string&); int handleDone (std::string&);
int handleModify (std::string&); int handleModify (std::string&);
int handleProjects (std::string&);
int handleCompletionProjects (std::string&);
int handleCompletionConfig (std::string&); int handleCompletionConfig (std::string&);
int handleQuery (std::string&); int handleQuery (std::string&);
int handleConfig (std::string&); int handleConfig (std::string&);