Helper Command

- Added _query helper command for script writers, which accepts a filter like
  any other report, but returns raw JSON.
This commit is contained in:
Paul Beckingham 2010-12-26 10:00:41 -05:00
parent 126a3d88b5
commit 7f32435ce9
8 changed files with 72 additions and 0 deletions

View file

@ -24,6 +24,8 @@
+ Added feature #574, default due dates (thanks to Erlan Sergaziev).
+ Added feature #575, including Danish holidays (thanks to Irfan Siddiqui).
+ Eliminated dependency on ncurses.
+ Added _query helper command for script writers, which accepts a filter like
any other report, but returns only full JSON.
+ Fixed bug #515, which displayed an incorrect message after duplicating a
non-existent task (thanks to Peter De Poorter).
+ Fixed bug #529, where the 'depends' attribute was not mentioned in the

2
NEWS
View file

@ -11,6 +11,8 @@ New Features in taskwarrior 1.9.4
- The first month in the calendar report can now be changed with an offset
value.
- No more dependency on ncurses.
- New '_query helper command for script writers, which accepts a filter like
any other report, but returns unformatted JSON.
- Assorted bug fixes.
Please refer to the ChangeLog file for full details. There are too many to

View file

@ -135,6 +135,7 @@ void Cmd::load ()
commands.push_back ("_config");
commands.push_back ("_version");
commands.push_back ("_urgency");
commands.push_back ("_query");
commands.push_back ("export.csv");
commands.push_back ("export.ical");
commands.push_back ("export.yaml");
@ -241,6 +242,7 @@ bool Cmd::isReadOnlyCommand ()
command == "_config" ||
command == "_version" ||
command == "_urgency" ||
command == "_query" ||
command == "export.csv" ||
command == "export.ical" ||
command == "export.yaml" ||

View file

@ -259,6 +259,7 @@ int Context::dispatch (std::string &out)
else if (cmd.command == "_config") { rc = handleCompletionConfig (out); }
else if (cmd.command == "_version") { rc = handleCompletionVersion (out); }
else if (cmd.command == "_urgency") { rc = handleUrgency (out); }
else if (cmd.command == "_query") { rc = handleQuery (out); }
else if (cmd.command == "" &&
sequence.size ()) { rc = handleModify (out); }

View file

@ -423,6 +423,29 @@ std::string Task::composeYAML () const
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string Task::composeJSON () const
{
std::stringstream out;
out << "{";
std::map <std::string, Att>::const_iterator i;
for (i = this->begin (); i != this->end (); ++i)
{
if (i != this->begin ())
out << ",";
out << "\""
<< i->second.name ()
<< "\":\""
<< i->second.value ()
<< "\"";
}
out << "}";
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
void Task::getAnnotations (std::vector <Att>& annotations) const
{

View file

@ -45,6 +45,7 @@ public:
void parse (const std::string&);
std::string composeCSV () const;
std::string composeYAML () const;
std::string composeJSON () const;
// Status values.
enum status {pending, completed, deleted, recurring, waiting};

View file

@ -552,6 +552,46 @@ int handleUrgency (std::string& outs)
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int handleQuery (std::string& outs)
{
int rc = 0;
if (context.hooks.trigger ("pre-query-command"))
{
// Get all the tasks.
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
handleRecurrence ();
context.tdb.load (tasks, context.filter);
context.tdb.commit ();
context.tdb.unlock ();
// Filter sequence.
if (context.sequence.size ())
context.filter.applySequence (tasks, context.sequence);
// Note: "limit:" feature not supported.
// Compose output.
outs = "{";
std::vector <Task>::iterator t;
for (t = tasks.begin (); t != tasks.end (); ++t)
{
if (t != tasks.begin ())
outs += ",\n";
outs += t->composeJSON ();
}
outs += "}\n";
context.hooks.trigger ("post-query-command");
}
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handleCompletionIDs (std::string& outs)
{

View file

@ -68,6 +68,7 @@ int handleCompletionIDs (std::string&);
int handleCompletionConfig (std::string&);
int handleCompletionVersion (std::string&);
int handleUrgency (std::string&);
int handleQuery (std::string&);
int handleVersion (std::string&);
int handleConfig (std::string&);
int handleShow (std::string&);