Commands - modify

- Re-enabled the modify command.
- Re-implemented Arguments::inject_defaults to accomodate the idea
  of the default command, the modify command without the modify
  keyword, and the automatic info command.
This commit is contained in:
Paul Beckingham 2011-07-10 23:41:30 -04:00
parent 7f84fa0c80
commit ef61981041
2 changed files with 71 additions and 67 deletions

View file

@ -617,6 +617,7 @@ void Arguments::resolve_aliases ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Arguments::inject_defaults () void Arguments::inject_defaults ()
{ {
// Scan the arguments and detect what is present.
bool found_command = false; bool found_command = false;
bool found_sequence = false; bool found_sequence = false;
bool found_other = false; bool found_other = false;
@ -627,7 +628,8 @@ void Arguments::inject_defaults ()
if (arg->_third == "command") if (arg->_third == "command")
found_command = true; found_command = true;
else if (arg->_third == "id") else if (arg->_third == "id" ||
arg->_third == "uuid")
found_sequence = true; found_sequence = true;
else if (arg->_third != "program" && else if (arg->_third != "program" &&
@ -636,11 +638,11 @@ void Arguments::inject_defaults ()
found_other = true; found_other = true;
} }
// If no command was specified, and there were no command line arguments // If no command was specified, then a command will be inserted.
// then invoke the default command.
if (!found_command) if (!found_command)
{ {
if (found_other || !found_sequence) // Default command.
if (!found_sequence)
{ {
// Apply overrides, if any. // Apply overrides, if any.
std::string defaultCommand = context.config.get ("default.command"); std::string defaultCommand = context.config.get ("default.command");
@ -653,12 +655,17 @@ void Arguments::inject_defaults ()
throw std::string (STRING_TRIVIAL_INPUT); throw std::string (STRING_TRIVIAL_INPUT);
} }
// If the command "task 123" is entered, but with no modifier arguments, // Modify command.
// then the actual command is assumed to be "info". if (found_other)
else if (found_sequence) {
capture_first ("modify");
}
// Information command.
else
{ {
context.header (STRING_ASSUME_INFO); context.header (STRING_ASSUME_INFO);
push_back (Triple ("information", "", "command")); capture_first ("information");
} }
} }
} }

View file

@ -30,6 +30,8 @@
#include <Context.h> #include <Context.h>
#include <Permission.h> #include <Permission.h>
#include <main.h> #include <main.h>
#include <text.h>
#include <i18n.h>
#include <CmdModify.h> #include <CmdModify.h>
extern Context context; extern Context context;
@ -37,10 +39,9 @@ extern Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CmdModify::CmdModify () CmdModify::CmdModify ()
{ {
// TODO Mention substitutions.
_keyword = "modify"; _keyword = "modify";
_usage = "task modify ID [tags] [attrs] [desc...]\n" _usage = "task <filter> modify <modifications>\n"
"task ID [tags] [attrs] [desc...]"; "task <sequence> <modifications>";
_description = "Modifies the existing task with provided arguments.\n" _description = "Modifies the existing task with provided arguments.\n"
"The 'modify' keyword is optional."; "The 'modify' keyword is optional.";
_read_only = false; _read_only = false;
@ -50,89 +51,91 @@ CmdModify::CmdModify ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CmdModify::execute (std::string& output) int CmdModify::execute (std::string& output)
{ {
/*
int count = 0; int count = 0;
std::stringstream out; std::stringstream out;
std::vector <Task> tasks; std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking")); context.tdb.lock (context.config.getBoolean ("locking"));
Filter filter; context.tdb.loadPending (tasks);
context.tdb.loadPending (tasks, filter);
// Filter sequence. // Apply filter.
std::vector <Task> all = tasks; std::vector <Task> filtered;
context.filter.applySequence (tasks, context.sequence); filter (tasks, filtered);
if (tasks.size () == 0)
if (filtered.size () == 0)
{ {
context.footnote ("No tasks specified."); context.footnote (STRING_FEEDBACK_NO_TASKS_SP);
return 1; return 1;
} }
// Apply the command line modifications to the new task.
Arguments modifications = context.args.extract_modifications ();
Permission permission; Permission permission;
if (context.sequence.size () > (size_t) context.config.getInteger ("bulk")) if (filtered.size () > (size_t) context.config.getInteger ("bulk"))
permission.bigSequence (); permission.bigSequence ();
std::vector <Task>::iterator task; std::vector <Task>::iterator task;
for (task = tasks.begin (); task != tasks.end (); ++task) for (task = filtered.begin (); task != filtered.end (); ++task)
{ {
Task before (*task);
modify_task_annotate (*task, modifications);
apply_defaults (*task);
// Perform some logical consistency checks. // Perform some logical consistency checks.
if (context.task.has ("recur") && if (task->has ("recur") &&
!context.task.has ("due") && !task->has ("due") &&
!task->has ("due")) !before.has ("due"))
throw std::string ("You cannot specify a recurring task without a due date."); throw std::string ("You cannot specify a recurring task without a due date.");
if (context.task.has ("until") && if (task->has ("until") &&
!context.task.has ("recur") && !task->has ("recur") &&
!task->has ("recur")) !before.has ("recur"))
throw std::string ("You cannot specify an until date for a non-recurring task."); throw std::string ("You cannot specify an until date for a non-recurring task.");
if (task->has ("recur") && if (before.has ("recur") &&
task->has ("due") && before.has ("due") &&
context.task.has ("due") && task->has ("due") &&
context.task.get ("due") == "") task->get ("due") == "")
throw std::string ("You cannot remove the due date from a recurring task."); throw std::string ("You cannot remove the due date from a recurring task.");
if (task->has ("recur") && if (before.has ("recur") &&
context.task.has ("recur") && task->has ("recur") &&
context.task.get ("recur") == "") task->get ("recur") == "")
throw std::string ("You cannot remove the recurrence from a recurring task."); throw std::string ("You cannot remove the recurrence from a recurring task.");
// Make all changes. // Make all changes.
bool warned = false; bool warned = false;
std::vector <Task>::iterator other; std::vector <Task>::iterator other;
for (other = all.begin (); other != all.end (); ++other) for (other = tasks.begin (); other != tasks.end (); ++other)
{ {
// Skip wait: modification to a parent task, and other child tasks. Too // Skip wait: modification to a parent task, and other child tasks. Too
// difficult to achieve properly without losing 'waiting' as a status. // difficult to achieve properly without losing 'waiting' as a status.
// Soon... // Soon...
if (other->id == task->id || // Self if (other->id == task->id || // Self
(! context.task.has ("wait") && // skip waits (! task->has ("wait") && // skip waits
task->has ("parent") && // is recurring before.has ("parent") && // is recurring
task->get ("parent") == other->get ("parent")) || // Sibling before.get ("parent") == other->get ("parent")) || // Sibling
other->get ("uuid") == task->get ("parent")) // Parent other->get ("uuid") == before.get ("parent")) // Parent
{ {
if (task->has ("parent") && !warned) if (before.has ("parent") && !warned)
{ {
warned = true; warned = true;
std::cout << "Task " std::cout << "Task "
<< task->id << before.id
<< " is a recurring task, and all other instances of this" << " is a recurring task, and all other instances of this"
<< " task will be modified.\n"; << " task will be modified.\n";
} }
Task before (*other); Task alternate (*other);
// A non-zero value forces a file write.
int changes = 0;
// If a task is being made recurring, there are other cascading // If a task is being made recurring, there are other cascading
// changes. // changes.
if (!task->has ("recur") && if (!before.has ("recur") &&
context.task.has ("recur")) task->has ("recur"))
{ {
other->setStatus (Task::recurring); other->setStatus (Task::recurring);
other->set ("mask", ""); other->set ("mask", "");
++changes;
std::cout << "Task " std::cout << "Task "
<< other->id << other->id
@ -140,49 +143,43 @@ int CmdModify::execute (std::string& output)
} }
// Apply other deltas. // Apply other deltas.
if (deltaDescription (*other)) modify_task_description_replace (*other, modifications);
{ apply_defaults (*other);
permission.bigChange ();
++changes;
}
changes += deltaTags (*other); if (taskDiff (alternate, *other))
changes += deltaAttributes (*other);
changes += deltaSubstitutions (*other);
if (taskDiff (before, *other))
{ {
// Only allow valid tasks. // Only allow valid tasks.
other->validate (); other->validate ();
if (changes && permission.confirmed (before, taskDifferences (before, *other) + "Proceed with change?")) if (permission.confirmed (alternate, taskDifferences (alternate, *other) + "Proceed with change?"))
{ {
// TODO Are dependencies being explicitly removed? // TODO Are dependencies being explicitly removed?
// Either we scan context.task for negative IDs "depends:-n" // Either we scan context.task for negative IDs "depends:-n"
// or we ask deltaAttributes (above) to record dependency // or we ask deltaAttributes (above) to record dependency
// removal. // removal.
dependencyChainOnModify (before, *other); dependencyChainOnModify (alternate, *other);
context.tdb.update (*other); context.tdb.update (*other);
if (before.get ("project") != other->get ("project"))
context.footnote (onProjectChange (before, *other));
++count; ++count;
if (alternate.get ("project") != other->get ("project"))
context.footnote (onProjectChange (alternate, *other));
} }
} }
} }
} }
} }
context.tdb.commit (); if (count)
context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
if (context.config.getBoolean ("echo.command")) if (context.config.getBoolean ("echo.command"))
out << "Modified " << count << " task" << (count == 1 ? ".\n" : "s.\n"); out << "Modified " << count << " task" << (count == 1 ? ".\n" : "s.\n");
output = out.str (); output = out.str ();
*/
return 0; return 0;
} }