Command - prepend

- Migrated handlePrepend to CmdPrepend.
This commit is contained in:
Paul Beckingham 2011-05-28 16:40:39 -04:00
parent 920e1c6c86
commit ef65617258
9 changed files with 177 additions and 92 deletions

View file

@ -153,7 +153,6 @@ void Cmd::load ()
commands.push_back ("duplicate");
commands.push_back ("import");
commands.push_back ("log");
commands.push_back ("prepend");
commands.push_back ("start");
commands.push_back ("stop");
commands.push_back ("summary");
@ -256,7 +255,6 @@ bool Cmd::isWriteCommand ()
command == "duplicate" ||
command == "import" ||
command == "log" ||
command == "prepend" ||
command == "pull" ||
command == "start" ||
command == "stop" ||

View file

@ -262,7 +262,6 @@ int Context::dispatch (std::string &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 == "prepend") { rc = handlePrepend (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

@ -1164,90 +1164,6 @@ int handleAppend (std::string& outs)
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handlePrepend (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 += deltaPrepend (*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) + "Are you sure?"))
{
context.tdb.update (*other);
if (context.config.getBoolean ("echo.command"))
out << "Prepended '"
<< 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 << "Prepended " << count << " task" << (count == 1 ? ".\n" : "s.\n");
outs = out.str ();
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int handleDuplicate (std::string& outs)
{

View file

@ -17,6 +17,7 @@ set (commands_SRCS Command.cpp Command.h
CmdInfo.cpp CmdInfo.h
CmdInstall.cpp CmdInstall.h
CmdLogo.cpp CmdLogo.h
CmdPrepend.cpp CmdPrepend.h
CmdProjects.cpp CmdProjects.h
CmdShell.cpp CmdShell.h
CmdShow.cpp CmdShow.h

View file

@ -100,10 +100,6 @@ int CmdHelp::execute (const std::string& command_line, std::string& output)
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 prepend ID [tags] [attrs] desc...");
view.set (row, 2, "Prepends 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.");

132
src/commands/CmdPrepend.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 <CmdPrepend.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
CmdPrepend::CmdPrepend ()
{
_keyword = "prepend";
_usage = "task prepend <IDs> [tags] [attrs] desc...";
_description = "Prepends more description to an existing task.";
_read_only = false;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdPrepend::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 += deltaPrepend (*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) + "Are you sure?"))
{
context.tdb.update (*other);
if (context.config.getBoolean ("echo.command"))
out << "Prepended '"
<< 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 << "Prepended " << count << " task" << (count == 1 ? ".\n" : "s.\n");
output = out.str ();
return rc;
}
////////////////////////////////////////////////////////////////////////////////

42
src/commands/CmdPrepend.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_CMDPREPEND
#define INCLUDED_CMDPREPEND
#define L10N // Localization complete.
#include <string>
#include <Command.h>
class CmdPrepend : public Command
{
public:
CmdPrepend ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -39,6 +39,7 @@
#include <CmdInfo.h>
#include <CmdInstall.h>
#include <CmdLogo.h>
#include <CmdPrepend.h>
#include <CmdProjects.h>
#include <CmdShell.h>
#include <CmdShow.h>
@ -70,6 +71,7 @@ void Command::factory (std::map <std::string, Command*>& all)
c = new CmdInfo (); all[c->keyword ()] = c;
c = new CmdInstall (); all[c->keyword ()] = c;
c = new CmdLogo (); all[c->keyword ()] = c;
c = new CmdPrepend (); all[c->keyword ()] = c;
c = new CmdProjects (); all[c->keyword ()] = c;
c = new CmdShell (); all[c->keyword ()] = c;
c = new CmdShow (); all[c->keyword ()] = c;

View file

@ -53,7 +53,6 @@ bool nag (Task&);
int handleAdd (std::string&);
int handleLog (std::string&);
int handleAppend (std::string&);
int handlePrepend (std::string&);
int handleDone (std::string&);
int handleModify (std::string&);
int handleCompletionConfig (std::string&);