mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-01 11:20:19 +02:00
Enhancement - Hooks
- Implemented pre-info-command, post-info-command hook.
This commit is contained in:
parent
d6daa336ca
commit
c8d208b9be
3 changed files with 256 additions and 246 deletions
|
@ -185,7 +185,8 @@ bool Hooks::eventType (const std::string& event, std::string& type)
|
|||
event == "pre-undo" || event == "post-undo" ||
|
||||
event == "pre-file-lock" || event == "post-file-lock" ||
|
||||
event == "pre-add-command" || event == "post-add-command" ||
|
||||
event == "pre-delete-command" || event == "post-delete-command")
|
||||
event == "pre-delete-command" || event == "post-delete-command" ||
|
||||
event == "pre-info-command" || event == "post-info-command")
|
||||
{
|
||||
type = "program";
|
||||
return true;
|
||||
|
|
101
src/command.cpp
101
src/command.cpp
|
@ -52,68 +52,71 @@ extern Context context;
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
int handleAdd (std::string &outs)
|
||||
{
|
||||
context.hooks.trigger ("pre-add-command");
|
||||
std::stringstream out;
|
||||
|
||||
context.task.set ("uuid", uuid ());
|
||||
context.task.setEntry ();
|
||||
|
||||
// Recurring tasks get a special status.
|
||||
if (context.task.has ("due") &&
|
||||
context.task.has ("recur"))
|
||||
if (context.hooks.trigger ("pre-add-command"))
|
||||
{
|
||||
context.task.setStatus (Task::recurring);
|
||||
context.task.set ("mask", "");
|
||||
}
|
||||
else if (context.task.has ("wait"))
|
||||
context.task.setStatus (Task::waiting);
|
||||
else
|
||||
context.task.setStatus (Task::pending);
|
||||
std::stringstream out;
|
||||
|
||||
// Override with default.project, if not specified.
|
||||
if (context.task.get ("project") == "")
|
||||
context.task.set ("project", context.config.get ("default.project"));
|
||||
context.task.set ("uuid", uuid ());
|
||||
context.task.setEntry ();
|
||||
|
||||
// Override with default.priority, if not specified.
|
||||
if (context.task.get ("priority") == "")
|
||||
{
|
||||
std::string defaultPriority = context.config.get ("default.priority");
|
||||
if (Att::validNameValue ("priority", "", defaultPriority))
|
||||
context.task.set ("priority", defaultPriority);
|
||||
}
|
||||
// Recurring tasks get a special status.
|
||||
if (context.task.has ("due") &&
|
||||
context.task.has ("recur"))
|
||||
{
|
||||
context.task.setStatus (Task::recurring);
|
||||
context.task.set ("mask", "");
|
||||
}
|
||||
else if (context.task.has ("wait"))
|
||||
context.task.setStatus (Task::waiting);
|
||||
else
|
||||
context.task.setStatus (Task::pending);
|
||||
|
||||
// Include tags.
|
||||
foreach (tag, context.tagAdditions)
|
||||
context.task.addTag (*tag);
|
||||
// Override with default.project, if not specified.
|
||||
if (context.task.get ("project") == "")
|
||||
context.task.set ("project", context.config.get ("default.project"));
|
||||
|
||||
// Perform some logical consistency checks.
|
||||
if (context.task.has ("recur") &&
|
||||
!context.task.has ("due"))
|
||||
throw std::string ("You cannot specify a recurring task without a due date.");
|
||||
// Override with default.priority, if not specified.
|
||||
if (context.task.get ("priority") == "")
|
||||
{
|
||||
std::string defaultPriority = context.config.get ("default.priority");
|
||||
if (Att::validNameValue ("priority", "", defaultPriority))
|
||||
context.task.set ("priority", defaultPriority);
|
||||
}
|
||||
|
||||
if (context.task.has ("until") &&
|
||||
!context.task.has ("recur"))
|
||||
throw std::string ("You cannot specify an until date for a non-recurring task.");
|
||||
// Include tags.
|
||||
foreach (tag, context.tagAdditions)
|
||||
context.task.addTag (*tag);
|
||||
|
||||
// Only valid tasks can be added.
|
||||
context.task.validate ();
|
||||
// Perform some logical consistency checks.
|
||||
if (context.task.has ("recur") &&
|
||||
!context.task.has ("due"))
|
||||
throw std::string ("You cannot specify a recurring task without a due date.");
|
||||
|
||||
context.tdb.lock (context.config.getBoolean ("locking"));
|
||||
context.tdb.add (context.task);
|
||||
if (context.task.has ("until") &&
|
||||
!context.task.has ("recur"))
|
||||
throw std::string ("You cannot specify an until date for a non-recurring task.");
|
||||
|
||||
// Only valid tasks can be added.
|
||||
context.task.validate ();
|
||||
|
||||
context.tdb.lock (context.config.getBoolean ("locking"));
|
||||
context.tdb.add (context.task);
|
||||
|
||||
#ifdef FEATURE_NEW_ID
|
||||
// All this, just for an id number.
|
||||
std::vector <Task> all;
|
||||
Filter none;
|
||||
context.tdb.loadPending (all, none);
|
||||
out << "Created task " << context.tdb.nextId () << std::endl;
|
||||
// All this, just for an id number.
|
||||
std::vector <Task> all;
|
||||
Filter none;
|
||||
context.tdb.loadPending (all, none);
|
||||
out << "Created task " << context.tdb.nextId () << std::endl;
|
||||
#endif
|
||||
|
||||
context.tdb.commit ();
|
||||
context.tdb.unlock ();
|
||||
context.tdb.commit ();
|
||||
context.tdb.unlock ();
|
||||
|
||||
outs = out.str ();
|
||||
context.hooks.trigger ("post-add-command");
|
||||
}
|
||||
|
||||
outs = out.str ();
|
||||
context.hooks.trigger ("post-add-command");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
398
src/report.cpp
398
src/report.cpp
|
@ -300,231 +300,237 @@ int longUsage (std::string &outs)
|
|||
int handleInfo (std::string &outs)
|
||||
{
|
||||
int rc = 0;
|
||||
// Get all the tasks.
|
||||
std::vector <Task> tasks;
|
||||
context.tdb.lock (context.config.getBoolean ("locking"));
|
||||
handleRecurrence ();
|
||||
context.tdb.loadPending (tasks, context.filter);
|
||||
context.tdb.commit ();
|
||||
context.tdb.unlock ();
|
||||
|
||||
// Filter sequence.
|
||||
context.filter.applySequence (tasks, context.sequence);
|
||||
|
||||
// Find the task.
|
||||
std::stringstream out;
|
||||
foreach (task, tasks)
|
||||
if (context.hooks.trigger ("pre-info-command"))
|
||||
{
|
||||
Table table;
|
||||
table.setTableWidth (context.getWidth ());
|
||||
table.setDateFormat (context.config.get ("dateformat"));
|
||||
// Get all the tasks.
|
||||
std::vector <Task> tasks;
|
||||
context.tdb.lock (context.config.getBoolean ("locking"));
|
||||
handleRecurrence ();
|
||||
context.tdb.loadPending (tasks, context.filter);
|
||||
context.tdb.commit ();
|
||||
context.tdb.unlock ();
|
||||
|
||||
table.addColumn ("Name");
|
||||
table.addColumn ("Value");
|
||||
// Filter sequence.
|
||||
context.filter.applySequence (tasks, context.sequence);
|
||||
|
||||
if ((context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor")) &&
|
||||
context.config.getBoolean ("fontunderline"))
|
||||
// Find the task.
|
||||
std::stringstream out;
|
||||
foreach (task, tasks)
|
||||
{
|
||||
table.setColumnUnderline (0);
|
||||
table.setColumnUnderline (1);
|
||||
}
|
||||
else
|
||||
table.setTableDashedUnderline ();
|
||||
Table table;
|
||||
table.setTableWidth (context.getWidth ());
|
||||
table.setDateFormat (context.config.get ("dateformat"));
|
||||
|
||||
table.setColumnWidth (0, Table::minimum);
|
||||
table.setColumnWidth (1, Table::flexible);
|
||||
table.addColumn ("Name");
|
||||
table.addColumn ("Value");
|
||||
|
||||
table.setColumnJustification (0, Table::left);
|
||||
table.setColumnJustification (1, Table::left);
|
||||
Date now;
|
||||
|
||||
int row = table.addRow ();
|
||||
table.addCell (row, 0, "ID");
|
||||
table.addCell (row, 1, task->id);
|
||||
|
||||
std::string status = ucFirst (Task::statusToText (task->getStatus ()));
|
||||
|
||||
if (task->has ("parent"))
|
||||
status += " (Recurring)";
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Status");
|
||||
table.addCell (row, 1, status);
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Description");
|
||||
table.addCell (row, 1, getFullDescription (*task));
|
||||
|
||||
if (task->has ("project"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Project");
|
||||
table.addCell (row, 1, task->get ("project"));
|
||||
}
|
||||
|
||||
if (task->has ("priority"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Priority");
|
||||
table.addCell (row, 1, task->get ("priority"));
|
||||
}
|
||||
|
||||
if (task->getStatus () == Task::recurring ||
|
||||
task->has ("parent"))
|
||||
{
|
||||
if (task->has ("recur"))
|
||||
if ((context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor")) &&
|
||||
context.config.getBoolean ("fontunderline"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Recurrence");
|
||||
table.addCell (row, 1, task->get ("recur"));
|
||||
table.setColumnUnderline (0);
|
||||
table.setColumnUnderline (1);
|
||||
}
|
||||
else
|
||||
table.setTableDashedUnderline ();
|
||||
|
||||
if (task->has ("until"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Recur until");
|
||||
table.addCell (row, 1, task->get ("until"));
|
||||
}
|
||||
table.setColumnWidth (0, Table::minimum);
|
||||
table.setColumnWidth (1, Table::flexible);
|
||||
|
||||
if (task->has ("mask"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Mask");
|
||||
table.addCell (row, 1, task->get ("mask"));
|
||||
}
|
||||
table.setColumnJustification (0, Table::left);
|
||||
table.setColumnJustification (1, Table::left);
|
||||
Date now;
|
||||
|
||||
int row = table.addRow ();
|
||||
table.addCell (row, 0, "ID");
|
||||
table.addCell (row, 1, task->id);
|
||||
|
||||
std::string status = ucFirst (Task::statusToText (task->getStatus ()));
|
||||
|
||||
if (task->has ("parent"))
|
||||
status += " (Recurring)";
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Status");
|
||||
table.addCell (row, 1, status);
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Description");
|
||||
table.addCell (row, 1, getFullDescription (*task));
|
||||
|
||||
if (task->has ("project"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Parent task");
|
||||
table.addCell (row, 1, task->get ("parent"));
|
||||
table.addCell (row, 0, "Project");
|
||||
table.addCell (row, 1, task->get ("project"));
|
||||
}
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Mask Index");
|
||||
table.addCell (row, 1, task->get ("imask"));
|
||||
}
|
||||
|
||||
// due (colored)
|
||||
bool imminent = false;
|
||||
bool overdue = false;
|
||||
if (task->has ("due"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Due");
|
||||
|
||||
Date dt (atoi (task->get ("due").c_str ()));
|
||||
std::string format = context.config.get ("reportdateformat");
|
||||
if (format == "")
|
||||
format = context.config.get ("dateformat");
|
||||
|
||||
std::string due = getDueDate (*task, format);
|
||||
table.addCell (row, 1, due);
|
||||
|
||||
overdue = (dt < now) ? true : false;
|
||||
int imminentperiod = context.config.getInteger ("due");
|
||||
Date imminentDay = now + imminentperiod * 86400;
|
||||
imminent = dt < imminentDay ? true : false;
|
||||
|
||||
if (context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor"))
|
||||
if (task->has ("priority"))
|
||||
{
|
||||
if (overdue)
|
||||
table.setCellColor (row, 1, Color (context.config.get ("color.overdue")));
|
||||
else if (imminent)
|
||||
table.setCellColor (row, 1, Color (context.config.get ("color.due")));
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Priority");
|
||||
table.addCell (row, 1, task->get ("priority"));
|
||||
}
|
||||
}
|
||||
|
||||
// wait
|
||||
if (task->has ("wait"))
|
||||
{
|
||||
if (task->getStatus () == Task::recurring ||
|
||||
task->has ("parent"))
|
||||
{
|
||||
if (task->has ("recur"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Recurrence");
|
||||
table.addCell (row, 1, task->get ("recur"));
|
||||
}
|
||||
|
||||
if (task->has ("until"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Recur until");
|
||||
table.addCell (row, 1, task->get ("until"));
|
||||
}
|
||||
|
||||
if (task->has ("mask"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Mask");
|
||||
table.addCell (row, 1, task->get ("mask"));
|
||||
}
|
||||
|
||||
if (task->has ("parent"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Parent task");
|
||||
table.addCell (row, 1, task->get ("parent"));
|
||||
}
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Mask Index");
|
||||
table.addCell (row, 1, task->get ("imask"));
|
||||
}
|
||||
|
||||
// due (colored)
|
||||
bool imminent = false;
|
||||
bool overdue = false;
|
||||
if (task->has ("due"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Due");
|
||||
|
||||
Date dt (atoi (task->get ("due").c_str ()));
|
||||
std::string format = context.config.get ("reportdateformat");
|
||||
if (format == "")
|
||||
format = context.config.get ("dateformat");
|
||||
|
||||
std::string due = getDueDate (*task, format);
|
||||
table.addCell (row, 1, due);
|
||||
|
||||
overdue = (dt < now) ? true : false;
|
||||
int imminentperiod = context.config.getInteger ("due");
|
||||
Date imminentDay = now + imminentperiod * 86400;
|
||||
imminent = dt < imminentDay ? true : false;
|
||||
|
||||
if (context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor"))
|
||||
{
|
||||
if (overdue)
|
||||
table.setCellColor (row, 1, Color (context.config.get ("color.overdue")));
|
||||
else if (imminent)
|
||||
table.setCellColor (row, 1, Color (context.config.get ("color.due")));
|
||||
}
|
||||
}
|
||||
|
||||
// wait
|
||||
if (task->has ("wait"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Waiting until");
|
||||
Date dt (atoi (task->get ("wait").c_str ()));
|
||||
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
|
||||
}
|
||||
|
||||
// start
|
||||
if (task->has ("start"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Start");
|
||||
Date dt (atoi (task->get ("start").c_str ()));
|
||||
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
|
||||
}
|
||||
|
||||
// end
|
||||
if (task->has ("end"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "End");
|
||||
Date dt (atoi (task->get ("end").c_str ()));
|
||||
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
|
||||
}
|
||||
|
||||
// tags ...
|
||||
std::vector <std::string> tags;
|
||||
task->getTags (tags);
|
||||
if (tags.size ())
|
||||
{
|
||||
std::string allTags;
|
||||
join (allTags, " ", tags);
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Tags");
|
||||
table.addCell (row, 1, allTags);
|
||||
}
|
||||
|
||||
// uuid
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Waiting until");
|
||||
Date dt (atoi (task->get ("wait").c_str ()));
|
||||
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
|
||||
}
|
||||
table.addCell (row, 0, "UUID");
|
||||
table.addCell (row, 1, task->get ("uuid"));
|
||||
|
||||
// start
|
||||
if (task->has ("start"))
|
||||
{
|
||||
// entry
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Start");
|
||||
Date dt (atoi (task->get ("start").c_str ()));
|
||||
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
|
||||
table.addCell (row, 0, "Entered");
|
||||
Date dt (atoi (task->get ("entry").c_str ()));
|
||||
std::string entry = dt.toString (context.config.get ("dateformat"));
|
||||
|
||||
std::string age;
|
||||
std::string created = task->get ("entry");
|
||||
if (created.length ())
|
||||
{
|
||||
Date dt (atoi (created.c_str ()));
|
||||
age = formatSeconds ((time_t) (now - dt));
|
||||
}
|
||||
|
||||
table.addCell (row, 1, entry + " (" + age + ")");
|
||||
|
||||
// fg
|
||||
std::string color = task->get ("fg");
|
||||
if (color != "")
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Foreground color");
|
||||
table.addCell (row, 1, color);
|
||||
}
|
||||
|
||||
// bg
|
||||
color = task->get ("bg");
|
||||
if (color != "")
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Background color");
|
||||
table.addCell (row, 1, color);
|
||||
}
|
||||
|
||||
out << optionalBlankLine ()
|
||||
<< table.render ()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// end
|
||||
if (task->has ("end"))
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "End");
|
||||
Date dt (atoi (task->get ("end").c_str ()));
|
||||
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
|
||||
if (! tasks.size ()) {
|
||||
out << "No matches." << std::endl;
|
||||
rc = 1;
|
||||
}
|
||||
|
||||
// tags ...
|
||||
std::vector <std::string> tags;
|
||||
task->getTags (tags);
|
||||
if (tags.size ())
|
||||
{
|
||||
std::string allTags;
|
||||
join (allTags, " ", tags);
|
||||
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Tags");
|
||||
table.addCell (row, 1, allTags);
|
||||
}
|
||||
|
||||
// uuid
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "UUID");
|
||||
table.addCell (row, 1, task->get ("uuid"));
|
||||
|
||||
// entry
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Entered");
|
||||
Date dt (atoi (task->get ("entry").c_str ()));
|
||||
std::string entry = dt.toString (context.config.get ("dateformat"));
|
||||
|
||||
std::string age;
|
||||
std::string created = task->get ("entry");
|
||||
if (created.length ())
|
||||
{
|
||||
Date dt (atoi (created.c_str ()));
|
||||
age = formatSeconds ((time_t) (now - dt));
|
||||
}
|
||||
|
||||
table.addCell (row, 1, entry + " (" + age + ")");
|
||||
|
||||
// fg
|
||||
std::string color = task->get ("fg");
|
||||
if (color != "")
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Foreground color");
|
||||
table.addCell (row, 1, color);
|
||||
}
|
||||
|
||||
// bg
|
||||
color = task->get ("bg");
|
||||
if (color != "")
|
||||
{
|
||||
row = table.addRow ();
|
||||
table.addCell (row, 0, "Background color");
|
||||
table.addCell (row, 1, color);
|
||||
}
|
||||
|
||||
out << optionalBlankLine ()
|
||||
<< table.render ()
|
||||
<< std::endl;
|
||||
outs = out.str ();
|
||||
context.hooks.trigger ("post-info-command");
|
||||
}
|
||||
|
||||
if (! tasks.size ()) {
|
||||
out << "No matches." << std::endl;
|
||||
rc = 1;
|
||||
}
|
||||
|
||||
outs = out.str ();
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue