Enhancement - duplicate command

- Added duplicate command that duplicates a task, and en passant
  applies and specified changes (thanks to David J Patrick).
This commit is contained in:
Paul Beckingham 2009-05-07 23:08:53 -04:00
parent aec64afc5c
commit b23bad9a5b
9 changed files with 170 additions and 13 deletions

View file

@ -12,6 +12,8 @@
is a set of IDs. This allows commands like "task delete 1 2 5-10,12".
+ Fixed bug in the ghistory report, which caused it to only show a new
month if a task was added during that month.
+ New command "duplicate" which allows an existing task to be duplicated,
and also have modifications applied (thanks to David J Patrick).
------ old releases ------------------------------

View file

@ -1,3 +1,21 @@
Adding New Command Checklist
----------------------------
- Create new handler in command.cpp or report.cpp.
- Add prototype to task.h.
- Add call to appropriate section in task.cpp.
- Add usage info in task.cpp.
- Add command name to list in parse.cpp.
- Add unit tests in src/tests.
- Add new configuration details to html/config.html.
- Add command details to html/advanced.html.
- Add description to ChangeLog, with attribution if the idea
came from a single user, but not if it came from multiple
users.
- Add description to html/task.html.
Release Checklist
-----------------

View file

@ -120,6 +120,13 @@ ID Project Pri Due Active Age Description
variable.
</p>
<strong>% task duplicate 1 /foo/bar/g +tag priority:H</strong>
<p>
This duplicates task 1, then applies the modifications specified,
which change all "foo" to "bar" in the description and annotations,
adds the tag "tag", and sets the priority to "H".
</p>
<strong>% task delete &lt;id&gt;</strong>
<p>
There are two ways of getting rid of tasks - mark them as done, or

View file

@ -123,6 +123,8 @@
is a set of IDs. This allows commands like "task delete 1 2 5-10,12".
<li>Fixed bug in the ghistory report, which caused it to only show a new
month if a task was added during that month.
<li>New command "duplicate" which allows an existing task to be duplicated,
and also have modifications applied (thanks to David J Patrick).
</ul>
<p>

View file

@ -755,7 +755,62 @@ std::string handleAppend (TDB& tdb, T& task, Config& conf)
}
if (conf.get ("echo.command", true))
out << "Modified " << count << " task" << (count == 1 ? "" : "s") << std::endl;
out << "Appended " << count << " task" << (count == 1 ? "" : "s") << std::endl;
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string handleDuplicate (TDB& tdb, T& task, Config& conf)
{
int count = 0;
std::stringstream out;
std::vector <T> all;
tdb.allPendingT (all);
std::vector <T> filtered = all;
filterSequence (filtered, task);
foreach (seq, filtered)
{
if (seq->getStatus () != T::recurring && seq->getAttribute ("parent") == "")
{
T dup (*seq);
dup.setUUID (uuid ()); // Needs a new UUID.
// Apply deltas.
deltaDescription (dup, task);
deltaTags (dup, task);
deltaAttributes (dup, task);
deltaSubstitutions (dup, task);
// A New task needs a new entry time.
char entryTime[16];
sprintf (entryTime, "%u", (unsigned int) time (NULL));
dup.setAttribute ("entry", entryTime);
if (!tdb.addT (dup))
throw std::string ("Could not create new task.");
if (conf.get ("echo.command", true))
out << "Duplicated "
<< seq->getId ()
<< " '"
<< seq->getDescription ()
<< "'"
<< std::endl;
++count;
}
else
out << "Task "
<< seq->getId ()
<< " '"
<< seq->getDescription ()
<< "' is a recurring task, and cannot be duplicated."
<< std::endl;
}
if (conf.get ("echo.command", true))
out << "Duplicated " << count << " task" << (count == 1 ? "" : "s") << std::endl;
return out.str ();
}

View file

@ -127,6 +127,7 @@ static const char* commands[] =
"completed",
"delete",
"done",
"duplicate",
"export",
"help",
"history",

View file

@ -108,6 +108,10 @@ static std::string shortUsage (Config& conf)
table.addCell (row, 1, "task ID /from/to/g");
table.addCell (row, 2, "Performs all substitutions on the task description, for fixing mistakes");
row = table.addRow ();
table.addCell (row, 1, "task duplicate ID [tags] [attrs] [desc...]");
table.addCell (row, 2, "Duplicates the specified task, and allows modifications");
row = table.addRow ();
table.addCell (row, 1, "task delete ID");
table.addCell (row, 2, "Deletes the specified task");
@ -872,6 +876,7 @@ std::string runTaskCommand (
else if (command == "stop") { cmdMod = true; out = handleStop (tdb, task, conf); }
else if (command == "undo") { cmdMod = true; out = handleUndo (tdb, task, conf); }
else if (command == "import") { cmdMod = true; out = handleImport (tdb, task, conf); }
else if (command == "duplicate") { cmdMod = true; out = handleDuplicate (tdb, task, conf); }
// Command that display IDs and therefore need TDB::gc first.
else if (command == "completed") { if (gc) gcMod = tdb.gc (); out = handleCompleted (tdb, task, conf); }

View file

@ -89,6 +89,7 @@ std::string handleStop (TDB&, T&, Config&);
std::string handleUndo (TDB&, T&, Config&);
std::string handleColor (Config&);
std::string handleAnnotate (TDB&, T&, Config&);
std::string handleDuplicate (TDB&, T&, Config&);
T findT (int, const std::vector <T>&);
int deltaAppend (T&, T&);
int deltaDescription (T&, T&);

66
src/tests/duplicate.t Executable file
View file

@ -0,0 +1,66 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2009, Paul Beckingham.
## 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
##
################################################################################
use strict;
use warnings;
use Test::More tests => 11;
# Create the rc file.
if (open my $fh, '>', 'dup.rc')
{
print $fh "data.location=.\n";
close $fh;
ok (-r 'dup.rc', 'Created dup.rc');
}
# Test the duplicate command.
qx{../task rc:dup.rc add foo};
qx{../task rc:dup.rc duplicate 1};
my $output = qx{../task rc:dup.rc info 2};
like ($output, qr/ID\s+2/, 'duplicate new id');
like ($output, qr/Status\s+Pending/, 'duplicate same status');
like ($output, qr/Description\s+foo/, 'duplicate same description');
# Test the en passant modification while duplicating.
qx{../task rc:dup.rc duplicate 1 priority:H /foo/FOO/ +tag};
$output = qx{../task rc:dup.rc info 3};
like ($output, qr/ID\s+3/, 'duplicate new id');
like ($output, qr/Status\s+Pending/, 'duplicate same status');
like ($output, qr/Description\s+FOO/, 'duplicate modified description');
like ($output, qr/Priority\s+H/, 'duplicate added priority');
like ($output, qr/Tags\s+tag/, 'duplicate added tag');
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'dup.rc';
ok (!-r 'dup.rc', 'Removed dup.rc');
exit 0;