Command - append

- Migrated handleAppend to CmdAppend.
This commit is contained in:
Paul Beckingham 2011-05-28 16:41:21 -04:00
parent ef65617258
commit b9246e04b2
9 changed files with 177 additions and 92 deletions

View file

@ -142,7 +142,6 @@ void Cmd::load ()
commands.push_back ("burndown.weekly");
commands.push_back ("burndown.monthly");
commands.push_back ("add");
commands.push_back ("append");
commands.push_back ("annotate");
commands.push_back ("denotate");
commands.push_back ("calendar");
@ -247,7 +246,6 @@ bool Cmd::isWriteCommand ()
{
if (command == "merge" ||
command == "add" ||
command == "append" ||
command == "annotate" ||
command == "denotate" ||
command == "delete" ||

View file

@ -261,7 +261,6 @@ int Context::dispatch (std::string &out)
else if (cmd.command == "timesheet") { rc = handleReportTimesheet (out); }
else if (cmd.command == "add") { rc = handleAdd (out); }
else if (cmd.command == "log") { rc = handleLog (out); }
else if (cmd.command == "append") { rc = handleAppend (out); }
else if (cmd.command == "annotate") { rc = handleAnnotate (out); }
else if (cmd.command == "denotate") { rc = handleDenotate (out); }
else if (cmd.command == "done") { rc = handleDone (out); }

View file

@ -1080,90 +1080,6 @@ int handleModify (std::string& outs)
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int handleAppend (std::string& outs)
{
if (!context.task.has ("description"))
throw std::string ("Additional text must be provided.");
int rc = 0;
int count = 0;
std::stringstream out;
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
Filter filter;
context.tdb.loadPending (tasks, filter);
// Filter sequence.
std::vector <Task> all = tasks;
context.filter.applySequence (tasks, context.sequence);
if (tasks.size () == 0)
{
std::cout << "No tasks specified.\n";
return 1;
}
Permission permission;
if (context.sequence.size () > (size_t) context.config.getInteger ("bulk"))
permission.bigSequence ();
foreach (task, tasks)
{
foreach (other, all)
{
if (other->id == task->id || // Self
(task->has ("parent") &&
task->get ("parent") == other->get ("parent")) || // Sibling
other->get ("uuid") == task->get ("parent")) // Parent
{
Task before (*other);
// A non-zero value forces a file write.
int changes = 0;
// Apply other deltas.
changes += deltaAppend (*other);
changes += deltaTags (*other);
changes += deltaAttributes (*other);
changes += deltaSubstitutions (*other);
if (taskDiff (before, *other))
{
// Only allow valid tasks.
other->validate ();
if (changes && permission.confirmed (before, taskDifferences (before, *other) + "Proceed with change?"))
{
context.tdb.update (*other);
if (context.config.getBoolean ("echo.command"))
out << "Appended '"
<< context.task.get ("description")
<< "' to task "
<< other->id
<< ".\n";
if (before.get ("project") != other->get ("project"))
context.footnote (onProjectChange (before, *other));
++count;
}
}
}
}
}
context.tdb.commit ();
context.tdb.unlock ();
if (context.config.getBoolean ("echo.command"))
out << "Appended " << count << " task" << (count == 1 ? ".\n" : "s.\n");
outs = out.str ();
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handleDuplicate (std::string& outs)
{

View file

@ -6,6 +6,7 @@ include_directories (${CMAKE_SOURCE_DIR}
${TASK_INCLUDE_DIRS})
set (commands_SRCS Command.cpp Command.h
CmdAppend.cpp CmdAppend.h
CmdCommands.cpp CmdCommands.h
CmdCount.cpp CmdCount.h
CmdCustom.cpp CmdCustom.h

132
src/commands/CmdAppend.cpp Normal file
View file

@ -0,0 +1,132 @@
////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the
//
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA
// 02110-1301
// USA
//
////////////////////////////////////////////////////////////////////////////////
#include <sstream>
#include <Context.h>
#include <Permission.h>
#include <main.h>
#include <CmdAppend.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
CmdAppend::CmdAppend ()
{
_keyword = "append";
_usage = "task append <IDs> [tags] [attrs] desc...";
_description = "Append more description to an existing task.";
_read_only = false;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdAppend::execute (const std::string& command_line, std::string& output)
{
if (!context.task.has ("description"))
throw std::string ("Additional text must be provided.");
int rc = 0;
int count = 0;
std::stringstream out;
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
Filter filter;
context.tdb.loadPending (tasks, filter);
// Filter sequence.
std::vector <Task> all = tasks;
context.filter.applySequence (tasks, context.sequence);
if (tasks.size () == 0)
{
context.footnote ("No tasks specified.");
return 1;
}
Permission permission;
if (context.sequence.size () > (size_t) context.config.getInteger ("bulk"))
permission.bigSequence ();
std::vector <Task>::iterator task;
for (task = tasks.begin (); task != tasks.end (); ++task)
{
std::vector <Task>::iterator other;
for (other = all.begin (); other != all.end (); ++other)
{
if (other->id == task->id || // Self
(task->has ("parent") &&
task->get ("parent") == other->get ("parent")) || // Sibling
other->get ("uuid") == task->get ("parent")) // Parent
{
Task before (*other);
// A non-zero value forces a file write.
int changes = 0;
// Apply other deltas.
changes += deltaAppend (*other);
changes += deltaTags (*other);
changes += deltaAttributes (*other);
changes += deltaSubstitutions (*other);
if (taskDiff (before, *other))
{
// Only allow valid tasks.
other->validate ();
if (changes && permission.confirmed (before, taskDifferences (before, *other) + "Proceed with change?"))
{
context.tdb.update (*other);
if (context.config.getBoolean ("echo.command"))
out << "Appended '"
<< context.task.get ("description")
<< "' to task "
<< other->id
<< ".\n";
if (before.get ("project") != other->get ("project"))
context.footnote (onProjectChange (before, *other));
++count;
}
}
}
}
}
context.tdb.commit ();
context.tdb.unlock ();
if (context.config.getBoolean ("echo.command"))
out << "Appended " << count << " task" << (count == 1 ? ".\n" : "s.\n");
output = out.str ();
return rc;
}
////////////////////////////////////////////////////////////////////////////////

42
src/commands/CmdAppend.h Normal file
View file

@ -0,0 +1,42 @@
////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the
//
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA
// 02110-1301
// USA
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_CMDAPPEND
#define INCLUDED_CMDAPPEND
#define L10N // Localization complete.
#include <string>
#include <Command.h>
class CmdAppend : public Command
{
public:
CmdAppend ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -96,10 +96,6 @@ int CmdHelp::execute (const std::string& command_line, std::string& output)
view.set (row, 1, "task log [tags] [attrs] desc...");
view.set (row, 2, "Adds a new task that is already completed.");
row = view.addRow ();
view.set (row, 1, "task append ID [tags] [attrs] desc...");
view.set (row, 2, "Appends more description to an existing task.");
row = view.addRow ();
view.set (row, 1, "task annotate ID desc...");
view.set (row, 2, "Adds an annotation to an existing task.");

View file

@ -28,6 +28,7 @@
#include <iostream>
#include <vector>
#include <Command.h>
#include <CmdAppend.h>
#include <CmdCommands.h>
#include <CmdCount.h>
#include <CmdCustom.h>
@ -57,6 +58,7 @@ void Command::factory (std::map <std::string, Command*>& all)
{
Command* c;
c = new CmdAppend (); all[c->keyword ()] = c;
c = new CmdCompletionCommands (); all[c->keyword ()] = c;
c = new CmdCompletionIds (); all[c->keyword ()] = c;
c = new CmdCompletionProjects (); all[c->keyword ()] = c;

View file

@ -52,7 +52,6 @@ bool nag (Task&);
// command.cpp
int handleAdd (std::string&);
int handleLog (std::string&);
int handleAppend (std::string&);
int handleDone (std::string&);
int handleModify (std::string&);
int handleCompletionConfig (std::string&);