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

@ -727,7 +727,7 @@ std::string handleAppend (TDB& tdb, T& task, Config& conf)
if (other->getId () == seq->getId () || // Self
(seq->getAttribute ("parent") != "" &&
seq->getAttribute ("parent") == other->getAttribute ("parent")) || // Sibling
other->getUUID () == seq->getAttribute ("parent")) // Parent
other->getUUID () == seq->getAttribute ("parent")) // Parent
{
// A non-zero value forces a file write.
int changes = 0;
@ -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");
@ -861,17 +865,18 @@ std::string runTaskCommand (
else if (command == "help") { out = longUsage ( conf); }
// Commands that cause updates.
else if (command == "" && task.getId ()) { cmdMod = true; out = handleModify (tdb, task, conf); }
else if (command == "add") { cmdMod = true; out = handleAdd (tdb, task, conf); }
else if (command == "append") { cmdMod = true; out = handleAppend (tdb, task, conf); }
else if (command == "annotate") { cmdMod = true; out = handleAnnotate (tdb, task, conf); }
else if (command == "done") { cmdMod = true; out = handleDone (tdb, task, conf); }
else if (command == "undelete") { cmdMod = true; out = handleUndelete (tdb, task, conf); }
else if (command == "delete") { cmdMod = true; out = handleDelete (tdb, task, conf); }
else if (command == "start") { cmdMod = true; out = handleStart (tdb, task, conf); }
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 == "" && task.getId ()) { cmdMod = true; out = handleModify (tdb, task, conf); }
else if (command == "add") { cmdMod = true; out = handleAdd (tdb, task, conf); }
else if (command == "append") { cmdMod = true; out = handleAppend (tdb, task, conf); }
else if (command == "annotate") { cmdMod = true; out = handleAnnotate (tdb, task, conf); }
else if (command == "done") { cmdMod = true; out = handleDone (tdb, task, conf); }
else if (command == "undelete") { cmdMod = true; out = handleUndelete (tdb, task, conf); }
else if (command == "delete") { cmdMod = true; out = handleDelete (tdb, task, conf); }
else if (command == "start") { cmdMod = true; out = handleStart (tdb, task, conf); }
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;