Enhancement - Hooks

- Implemented pre-info-command, post-info-command hook.
This commit is contained in:
Paul Beckingham 2010-01-23 13:57:38 -05:00
parent d6daa336ca
commit c8d208b9be
3 changed files with 256 additions and 246 deletions

View file

@ -185,7 +185,8 @@ bool Hooks::eventType (const std::string& event, std::string& type)
event == "pre-undo" || event == "post-undo" || event == "pre-undo" || event == "post-undo" ||
event == "pre-file-lock" || event == "post-file-lock" || event == "pre-file-lock" || event == "post-file-lock" ||
event == "pre-add-command" || event == "post-add-command" || 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"; type = "program";
return true; return true;

View file

@ -52,68 +52,71 @@ extern Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int handleAdd (std::string &outs) int handleAdd (std::string &outs)
{ {
context.hooks.trigger ("pre-add-command"); if (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"))
{ {
context.task.setStatus (Task::recurring); std::stringstream out;
context.task.set ("mask", "");
}
else if (context.task.has ("wait"))
context.task.setStatus (Task::waiting);
else
context.task.setStatus (Task::pending);
// Override with default.project, if not specified. context.task.set ("uuid", uuid ());
if (context.task.get ("project") == "") context.task.setEntry ();
context.task.set ("project", context.config.get ("default.project"));
// Override with default.priority, if not specified. // Recurring tasks get a special status.
if (context.task.get ("priority") == "") if (context.task.has ("due") &&
{ context.task.has ("recur"))
std::string defaultPriority = context.config.get ("default.priority"); {
if (Att::validNameValue ("priority", "", defaultPriority)) context.task.setStatus (Task::recurring);
context.task.set ("priority", defaultPriority); context.task.set ("mask", "");
} }
else if (context.task.has ("wait"))
context.task.setStatus (Task::waiting);
else
context.task.setStatus (Task::pending);
// Include tags. // Override with default.project, if not specified.
foreach (tag, context.tagAdditions) if (context.task.get ("project") == "")
context.task.addTag (*tag); context.task.set ("project", context.config.get ("default.project"));
// Perform some logical consistency checks. // Override with default.priority, if not specified.
if (context.task.has ("recur") && if (context.task.get ("priority") == "")
!context.task.has ("due")) {
throw std::string ("You cannot specify a recurring task without a due date."); std::string defaultPriority = context.config.get ("default.priority");
if (Att::validNameValue ("priority", "", defaultPriority))
context.task.set ("priority", defaultPriority);
}
if (context.task.has ("until") && // Include tags.
!context.task.has ("recur")) foreach (tag, context.tagAdditions)
throw std::string ("You cannot specify an until date for a non-recurring task."); context.task.addTag (*tag);
// Only valid tasks can be added. // Perform some logical consistency checks.
context.task.validate (); 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")); if (context.task.has ("until") &&
context.tdb.add (context.task); !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 #ifdef FEATURE_NEW_ID
// All this, just for an id number. // All this, just for an id number.
std::vector <Task> all; std::vector <Task> all;
Filter none; Filter none;
context.tdb.loadPending (all, none); context.tdb.loadPending (all, none);
out << "Created task " << context.tdb.nextId () << std::endl; out << "Created task " << context.tdb.nextId () << std::endl;
#endif #endif
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
outs = out.str ();
context.hooks.trigger ("post-add-command");
}
outs = out.str ();
context.hooks.trigger ("post-add-command");
return 0; return 0;
} }

View file

@ -300,231 +300,237 @@ int longUsage (std::string &outs)
int handleInfo (std::string &outs) int handleInfo (std::string &outs)
{ {
int rc = 0; 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. if (context.hooks.trigger ("pre-info-command"))
context.filter.applySequence (tasks, context.sequence);
// Find the task.
std::stringstream out;
foreach (task, tasks)
{ {
Table table; // Get all the tasks.
table.setTableWidth (context.getWidth ()); std::vector <Task> tasks;
table.setDateFormat (context.config.get ("dateformat")); context.tdb.lock (context.config.getBoolean ("locking"));
handleRecurrence ();
context.tdb.loadPending (tasks, context.filter);
context.tdb.commit ();
context.tdb.unlock ();
table.addColumn ("Name"); // Filter sequence.
table.addColumn ("Value"); context.filter.applySequence (tasks, context.sequence);
if ((context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor")) && // Find the task.
context.config.getBoolean ("fontunderline")) std::stringstream out;
foreach (task, tasks)
{ {
table.setColumnUnderline (0); Table table;
table.setColumnUnderline (1); table.setTableWidth (context.getWidth ());
} table.setDateFormat (context.config.get ("dateformat"));
else
table.setTableDashedUnderline ();
table.setColumnWidth (0, Table::minimum); table.addColumn ("Name");
table.setColumnWidth (1, Table::flexible); table.addColumn ("Value");
table.setColumnJustification (0, Table::left); if ((context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor")) &&
table.setColumnJustification (1, Table::left); context.config.getBoolean ("fontunderline"))
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"))
{ {
row = table.addRow (); table.setColumnUnderline (0);
table.addCell (row, 0, "Recurrence"); table.setColumnUnderline (1);
table.addCell (row, 1, task->get ("recur"));
} }
else
table.setTableDashedUnderline ();
if (task->has ("until")) table.setColumnWidth (0, Table::minimum);
{ table.setColumnWidth (1, Table::flexible);
row = table.addRow ();
table.addCell (row, 0, "Recur until");
table.addCell (row, 1, task->get ("until"));
}
if (task->has ("mask")) table.setColumnJustification (0, Table::left);
{ table.setColumnJustification (1, Table::left);
row = table.addRow (); Date now;
table.addCell (row, 0, "Mask");
table.addCell (row, 1, task->get ("mask")); 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")) 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 (); row = table.addRow ();
table.addCell (row, 0, "Parent task"); table.addCell (row, 0, "Project");
table.addCell (row, 1, task->get ("parent")); table.addCell (row, 1, task->get ("project"));
} }
row = table.addRow (); if (task->has ("priority"))
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) row = table.addRow ();
table.setCellColor (row, 1, Color (context.config.get ("color.overdue"))); table.addCell (row, 0, "Priority");
else if (imminent) table.addCell (row, 1, task->get ("priority"));
table.setCellColor (row, 1, Color (context.config.get ("color.due")));
} }
}
// wait if (task->getStatus () == Task::recurring ||
if (task->has ("wait")) 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 (); row = table.addRow ();
table.addCell (row, 0, "Waiting until"); table.addCell (row, 0, "UUID");
Date dt (atoi (task->get ("wait").c_str ())); table.addCell (row, 1, task->get ("uuid"));
table.addCell (row, 1, dt.toString (context.config.get ("dateformat")));
}
// start // entry
if (task->has ("start"))
{
row = table.addRow (); row = table.addRow ();
table.addCell (row, 0, "Start"); table.addCell (row, 0, "Entered");
Date dt (atoi (task->get ("start").c_str ())); Date dt (atoi (task->get ("entry").c_str ()));
table.addCell (row, 1, dt.toString (context.config.get ("dateformat"))); 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 (! tasks.size ()) {
if (task->has ("end")) out << "No matches." << std::endl;
{ rc = 1;
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 ... outs = out.str ();
std::vector <std::string> tags; context.hooks.trigger ("post-info-command");
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;
} }
if (! tasks.size ()) {
out << "No matches." << std::endl;
rc = 1;
}
outs = out.str ();
return rc; return rc;
} }