Merge branch '2.0.0' of tasktools.org:task into 2.0.0

This commit is contained in:
Cory Donnelly 2011-03-16 08:40:02 -04:00
commit 994404d6e1
22 changed files with 173 additions and 50 deletions

View file

@ -149,6 +149,7 @@ void Cmd::load ()
commands.push_back ("burndown.weekly");
commands.push_back ("burndown.monthly");
commands.push_back ("count");
commands.push_back ("ids");
// Commands whose names are localized.
commands.push_back (context.stringtable.get (CMD_ADD, "add"));
@ -258,6 +259,7 @@ bool Cmd::isReadOnlyCommand ()
command == "burndown.weekly" ||
command == "burndown.monthly" ||
command == "count" ||
command == "ids" ||
command == context.stringtable.get (CMD_CALENDAR, "calendar") ||
command == context.stringtable.get (CMD_COLORS, "colors") ||
command == context.stringtable.get (CMD_DIAGNOSTICS, "diagnostics") ||

View file

@ -31,6 +31,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include "Context.h"
#include "Directory.h"
#include "File.h"
@ -84,6 +85,26 @@ void Context::initialize (int argc, char** argv)
args.push_back (argv[i]);
}
// Capture any stdin args.
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO (&fds);
FD_SET (STDIN_FILENO, &fds);
select (STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
if (FD_ISSET (0, &fds))
{
std::string arg;
while (std::cin >> arg)
{
if (arg == "--")
break;
args.push_back (arg);
}
}
initialize ();
// Hook system init, plus post-start event occurring at the first possible
@ -263,6 +284,7 @@ int Context::dispatch (std::string &out)
else if (cmd.command == "pull") { handlePull (out); }
else if (cmd.command == "diagnostics") { handleDiagnostics (out); }
else if (cmd.command == "count") { rc = handleCount (out); }
else if (cmd.command == "ids") { rc = handleIds (out); }
else if (cmd.command == "_projects") { rc = handleCompletionProjects (out); }
else if (cmd.command == "_tags") { rc = handleCompletionTags (out); }
else if (cmd.command == "_commands") { rc = handleCompletionCommands (out); }

View file

@ -2398,6 +2398,36 @@ int handleCount (std::string& outs)
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handleIds (std::string& outs)
{
int rc = 0;
if (context.hooks.trigger ("pre-ids-command"))
{
// Scan the pending tasks, applying any filter.
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
handleRecurrence ();
context.tdb.load (tasks, context.filter);
context.tdb.commit ();
context.tdb.unlock ();
// Find number of matching tasks.
std::vector <int> ids;
foreach (task, tasks)
if (task->id)
ids.push_back (task->id);
std::sort (ids.begin (), ids.end ());
outs = compressIds (ids) + "\n";
context.hooks.trigger ("post-ids-command");
}
return rc;
}
////////////////////////////////////////////////////////////////////////////////
#ifdef FEATURE_SHELL
void handleShell ()

View file

@ -64,7 +64,7 @@ int main (int argc, char** argv)
{
context.initialize (argc, argv);
/* From 2.0.0
/* From old 2.0.0
std::string::size_type task = context.program.find ("/task");
std::string::size_type t = context.program.find ("/t");
std::string::size_type cal = context.program.find ("/cal");

View file

@ -84,6 +84,7 @@ int handleAnnotate (std::string&);
int handleDenotate (std::string&);
int handleDuplicate (std::string&);
int handleCount (std::string&);
int handleIds (std::string&);
void handleUndo ();
void handleMerge (std::string&);
void handlePush (std::string&);

View file

@ -244,6 +244,10 @@ int shortUsage (std::string& outs)
table.addCell (row, 1, "task count [filter]");
table.addCell (row, 2, "Shows only the number of matching tasks.");
row = table.addRow ();
table.addCell (row, 1, "task ids [filter]");
table.addCell (row, 2, "Shows only the IDs of matching tasks, in the form of a range.");
row = table.addRow ();
table.addCell (row, 1, "task version");
table.addCell (row, 2, "Shows the task version number.");

View file

@ -577,3 +577,58 @@ std::string renderAttribute (const std::string& name, const std::string& value)
}
////////////////////////////////////////////////////////////////////////////////
// The vector must be sorted first. This is a modified version of the run-
// length encoding algorithm.
//
// This function converts the vector:
//
// [1, 3, 4, 6, 7, 8, 9, 11]
//
// to ths string:
//
// 1,3-4,6-9,11
//
std::string compressIds (const std::vector <int>& ids)
{
std::stringstream result;
int range_start = 0;
int range_end = 0;
for (int i = 0; i < ids.size (); ++i)
{
if (i + 1 == ids.size ())
{
if (result.str ().length ())
result << ",";
if (range_start < range_end)
result << ids[range_start] << "-" << ids[range_end];
else
result << ids[range_start];
}
else
{
if (ids[range_end] + 1 == ids[i + 1])
{
++range_end;
}
else
{
if (result.str ().length ())
result << ",";
if (range_start < range_end)
result << ids[range_start] << "-" << ids[range_end];
else
result << ids[range_start];
range_start = range_end = i + 1;
}
}
}
return result.str ();
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -72,6 +72,7 @@ bool taskDiff (const Task&, const Task&);
std::string taskDifferences (const Task&, const Task&);
std::string taskInfoDifferences (const Task&, const Task&);
std::string renderAttribute (const std::string&, const std::string&);
std::string compressIds (const std::vector <int>&);
#endif
////////////////////////////////////////////////////////////////////////////////