- Promoted filtering code to the Command base class.
- Added filtering short-circuit.
This commit is contained in:
Paul Beckingham
2011-06-19 10:25:53 -04:00
parent db17536266
commit 7762ee2f9e
14 changed files with 79 additions and 173 deletions

View File

@@ -32,7 +32,6 @@
#include <Context.h> #include <Context.h>
#include <Date.h> #include <Date.h>
#include <Duration.h> #include <Duration.h>
#include <Expression.h>
#include <main.h> #include <main.h>
#include <CmdBurndown.h> #include <CmdBurndown.h>
@@ -991,15 +990,9 @@ int CmdBurndownMonthly::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Create a chart, scan the tasks, then render. // Create a chart, scan the tasks, then render.
Chart chart ('M'); Chart chart ('M');
@@ -1041,15 +1034,9 @@ int CmdBurndownWeekly::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Create a chart, scan the tasks, then render. // Create a chart, scan the tasks, then render.
Chart chart ('W'); Chart chart ('W');
@@ -1091,15 +1078,9 @@ int CmdBurndownDaily::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Create a chart, scan the tasks, then render. // Create a chart, scan the tasks, then render.
Chart chart ('D'); Chart chart ('D');

View File

@@ -29,7 +29,6 @@
#include <iomanip> #include <iomanip>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <ViewText.h> #include <ViewText.h>
#include <text.h> #include <text.h>
#include <util.h> #include <util.h>
@@ -71,15 +70,9 @@ int CmdCalendar::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
Date today; Date today;
bool getpendingdate = false; bool getpendingdate = false;
@@ -195,6 +188,7 @@ int CmdCalendar::execute (std::string& output)
{ {
// Find the oldest pending due date. // Find the oldest pending due date.
Date oldest (12,31,2037); Date oldest (12,31,2037);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
if (task->getStatus () == Task::pending) if (task->getStatus () == Task::pending)

View File

@@ -32,7 +32,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <ViewTask.h> #include <ViewTask.h>
#include <Expression.h>
#include <text.h> #include <text.h>
#include <main.h> #include <main.h>
#include <CmdCustom.h> #include <CmdCustom.h>
@@ -92,15 +91,9 @@ int CmdCustom::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Sort the tasks. // Sort the tasks.
std::vector <int> sequence; std::vector <int> sequence;

View File

@@ -27,7 +27,6 @@
#include <sstream> #include <sstream>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <ViewText.h> #include <ViewText.h>
#include <main.h> #include <main.h>
#include <text.h> #include <text.h>
@@ -63,16 +62,11 @@ int CmdHistoryMonthly::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
Date entry (task->get ("entry")); Date entry (task->get ("entry"));
@@ -228,16 +222,11 @@ int CmdHistoryAnnual::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
Date entry (task->get ("entry")); Date entry (task->get ("entry"));
@@ -390,16 +379,11 @@ int CmdGHistoryMonthly::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
Date entry (task->get ("entry")); Date entry (task->get ("entry"));
@@ -595,16 +579,11 @@ int CmdGHistoryAnnual::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
Date entry (task->get ("entry")); Date entry (task->get ("entry"));

View File

@@ -28,7 +28,6 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <main.h> #include <main.h>
#include <util.h> #include <util.h>
#include <CmdIDs.h> #include <CmdIDs.h>
@@ -56,18 +55,13 @@ int CmdIDs::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Find number of matching tasks. // Find number of matching tasks.
std::vector <int> ids; std::vector <int> ids;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
if (task->id) if (task->id)
ids.push_back (task->id); ids.push_back (task->id);
@@ -96,17 +90,12 @@ int CmdCompletionIds::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
std::vector <int> ids; std::vector <int> ids;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
if (task->getStatus () != Task::deleted && if (task->getStatus () != Task::deleted &&
task->getStatus () != Task::completed) task->getStatus () != Task::completed)
@@ -142,17 +131,12 @@ int CmdZshCompletionIds::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
std::stringstream out; std::stringstream out;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
if (task->getStatus () != Task::deleted && if (task->getStatus () != Task::deleted &&
task->getStatus () != Task::completed) task->getStatus () != Task::completed)

View File

@@ -28,7 +28,6 @@
#include <sstream> #include <sstream>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <Date.h> #include <Date.h>
#include <Duration.h> #include <Duration.h>
#include <ViewText.h> #include <ViewText.h>
@@ -60,15 +59,9 @@ int CmdInfo::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Read the undo file. // Read the undo file.
std::vector <std::string> undo; std::vector <std::string> undo;
@@ -81,6 +74,7 @@ int CmdInfo::execute (std::string& output)
// Find the task. // Find the task.
std::stringstream out; std::stringstream out;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
ViewText view; ViewText view;

View File

@@ -26,7 +26,6 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <main.h> #include <main.h>
#include <CmdQuery.h> #include <CmdQuery.h>
@@ -55,15 +54,9 @@ int CmdQuery::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
if (filtered.size () == 0) if (filtered.size () == 0)
{ {
@@ -81,6 +74,7 @@ int CmdQuery::execute (std::string& output)
if (json_array) if (json_array)
output += "[\n"; output += "[\n";
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
if (task != filtered.begin ()) if (task != filtered.begin ())

View File

@@ -31,7 +31,6 @@
#include <ViewText.h> #include <ViewText.h>
#include <Duration.h> #include <Duration.h>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <main.h> #include <main.h>
#include <text.h> #include <text.h>
#include <util.h> #include <util.h>
@@ -84,15 +83,9 @@ int CmdStatistics::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
Date now; Date now;
time_t earliest = time (NULL); time_t earliest = time (NULL);
@@ -110,6 +103,7 @@ int CmdStatistics::execute (std::string& output)
std::map <std::string, int> allTags; std::map <std::string, int> allTags;
std::map <std::string, int> allProjects; std::map <std::string, int> allProjects;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
++totalT; ++totalT;

View File

@@ -30,7 +30,6 @@
#include <Context.h> #include <Context.h>
#include <ViewText.h> #include <ViewText.h>
#include <Duration.h> #include <Duration.h>
#include <Expression.h>
#include <text.h> #include <text.h>
#include <main.h> #include <main.h>
#include <CmdSummary.h> #include <CmdSummary.h>
@@ -63,18 +62,13 @@ int CmdSummary::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Generate unique list of project names from all pending tasks. // Generate unique list of project names from all pending tasks.
std::map <std::string, bool> allProjects; std::map <std::string, bool> allProjects;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
if (task->getStatus () == Task::pending) if (task->getStatus () == Task::pending)
allProjects[task->get ("project")] = false; allProjects[task->get ("project")] = false;

View File

@@ -29,7 +29,6 @@
#include <vector> #include <vector>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <ViewText.h> #include <ViewText.h>
#include <CmdTags.h> #include <CmdTags.h>
#include <text.h> #include <text.h>
@@ -63,19 +62,14 @@ int CmdTags::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Scan all the tasks for their project name, building a map using project // Scan all the tasks for their project name, building a map using project
// names as keys. // names as keys.
std::map <std::string, int> unique; std::map <std::string, int> unique;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
std::vector <std::string> tags; std::vector <std::string> tags;
@@ -154,19 +148,14 @@ int CmdCompletionTags::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Scan all the tasks for their tags, building a map using tag // Scan all the tasks for their tags, building a map using tag
// names as keys. // names as keys.
std::map <std::string, int> unique; std::map <std::string, int> unique;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
std::vector <std::string> tags; std::vector <std::string> tags;

View File

@@ -28,7 +28,6 @@
#include <sstream> #include <sstream>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <ViewText.h> #include <ViewText.h>
#include <Date.h> #include <Date.h>
#include <main.h> #include <main.h>
@@ -59,15 +58,9 @@ int CmdTimesheet::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Just do this once. // Just do this once.
int width = context.getWidth (); int width = context.getWidth ();
@@ -118,6 +111,7 @@ int CmdTimesheet::execute (std::string& output)
completed.add (Column::factory ("string.right", "Due")); completed.add (Column::factory ("string.right", "Due"));
completed.add (Column::factory ("string", "Description")); completed.add (Column::factory ("string", "Description"));
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
// If task completed within range. // If task completed within range.

View File

@@ -28,7 +28,6 @@
#include <sstream> #include <sstream>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Expression.h>
#include <main.h> #include <main.h>
#include <CmdUrgency.h> #include <CmdUrgency.h>
@@ -55,17 +54,10 @@ int CmdUrgency::execute (std::string& output)
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
// Filter. // Apply filter.
Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
std::vector <Task> filtered; std::vector <Task> filtered;
std::vector <Task>::iterator task; filter (tasks, filtered);
for (task = tasks.begin (); task != tasks.end (); ++task)
if (e.eval (*task))
filtered.push_back (*task);
// Filter sequence.
if (filtered.size () == 0) if (filtered.size () == 0)
{ {
context.footnote ("No tasks specified."); context.footnote ("No tasks specified.");
@@ -74,6 +66,7 @@ int CmdUrgency::execute (std::string& output)
// Find the task(s). // Find the task(s).
std::stringstream out; std::stringstream out;
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
out << "task " out << "task "
<< task->id << task->id

View File

@@ -27,7 +27,9 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <Expression.h>
#include <Command.h> #include <Command.h>
#include <CmdAdd.h> #include <CmdAdd.h>
#include <CmdAnnotate.h> #include <CmdAnnotate.h>
#include <CmdAppend.h> #include <CmdAppend.h>
@@ -249,3 +251,20 @@ bool Command::displays_id () const
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Command::filter (std::vector <Task>& input, std::vector <Task>& output)
{
Arguments f = context.args.extract_read_only_filter ();
if (f.size ())
{
Expression e (f);
std::vector <Task>::iterator task;
for (task = input.begin (); task != input.end (); ++task)
if (e.eval (*task))
output.push_back (*task);
}
else
output = input;
}
////////////////////////////////////////////////////////////////////////////////

View File

@@ -29,7 +29,9 @@
#define L10N // Localization complete. #define L10N // Localization complete.
#include <map> #include <map>
#include <vector>
#include <string> #include <string>
#include <Task.h>
class Command class Command
{ {
@@ -49,6 +51,8 @@ public:
bool displays_id () const; bool displays_id () const;
virtual int execute (std::string&) = 0; virtual int execute (std::string&) = 0;
void filter (std::vector <Task>&, std::vector <Task>&);
protected: protected:
std::string _keyword; std::string _keyword;
std::string _usage; std::string _usage;