Enhancement - edit command

- Mere beginnings of the edit command.  No functionality yet.
This commit is contained in:
Paul Beckingham 2009-05-10 01:54:43 -04:00
parent de4194479a
commit 407ef39c54
5 changed files with 91 additions and 0 deletions

View file

@ -860,6 +860,76 @@ std::string handleDuplicate (TDB& tdb, T& task, Config& conf)
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
// Introducing the Silver Bullet. This feature is the catch-all fixative for
// various other ills. This is like opening up the hood and going in with a
// wrench. To be used sparingly.
std::string handleEdit (TDB& tdb, T& task, Config& conf)
{
std::stringstream out;
std::vector <T> all;
tdb.pendingT (all);
filterSequence (all, task);
foreach (seq, all)
{
// Check for file permissions.
std::string dataLocation = expandPath (conf.get ("data.location"));
if (access (dataLocation.c_str (), X_OK))
throw std::string ("Your data.location directory is not writable.");
// TODO Create a temp file name in data.location.
std::stringstream pattern;
pattern << dataLocation
<< "/task."
<< seq->getId ()
<< ".XXXXXX";
std::cout << "# pattern=" << pattern << std::endl;
char cpattern [PATH_MAX];
strcpy (cpattern, pattern.str ().c_str ());
char* file = mktemp (cpattern);
std::cout << "# file=" << file << std::endl;
// TODO Format the contents, T -> text.
std::stringstream before;
before << "# Edit only the items within the chevrons << >>. "
<< "All other edits will be ignored." << std::endl
<< "ID: " << seq->getId () << std::endl
<< "Project: <<" << seq->getAttribute ("project") << ">>" << std::endl
<< "Priority: <<" << seq->getAttribute ("priority") << ">>" << std::endl
<< "Description: <<" << seq->getDescription () << ">>" << std::endl;
// Write to file.
spit (file, before.str ());
// TODO Determine correct editor: $EDITOR > $VISUAL > vi
std::string editor = "/usr/bin/vi";
// system ("$EDITOR $file");
std::string command = editor + " " + file;
system (command.c_str ());
// Slurp file.
std::vector <std::string> after;
slurp (file, after, true);
// TODO Parse file, text -> T.
foreach (line, after)
{
// TODO Handle each type of line.
}
// Modify task.
tdb.modifyT (*seq);
// Cleanup.
unlink (file);
}
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string handleColor (Config& conf)
{

View file

@ -129,6 +129,7 @@ static const char* commands[] =
"delete",
"done",
"duplicate",
"edit",
"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 edit ID");
table.addCell (row, 2, "Launches an editor to let you modify all aspects of a task directly, therefore it is to be used carefully");
row = table.addRow ();
table.addCell (row, 1, "task duplicate ID [tags] [attrs] [desc...]");
table.addCell (row, 2, "Duplicates the specified task, and allows modifications");
@ -889,6 +893,7 @@ std::string runTaskCommand (
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); }
else if (command == "edit") { cmdMod = true; out = handleEdit (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

@ -90,6 +90,7 @@ std::string handleUndo (TDB&, T&, Config&);
std::string handleColor (Config&);
std::string handleAnnotate (TDB&, T&, Config&);
std::string handleDuplicate (TDB&, T&, Config&);
std::string handleEdit (TDB&, T&, Config&);
T findT (int, const std::vector <T>&);
int deltaAppend (T&, T&);
int deltaDescription (T&, T&);
@ -151,6 +152,7 @@ std::string expandPath (const std::string&);
#endif
bool slurp (const std::string&, std::vector <std::string>&, bool trimLines = false);
void spit (const std::string&, const std::string&);
// rules.cpp
void initializeColorRules (Config&);

View file

@ -430,3 +430,16 @@ bool slurp (
}
////////////////////////////////////////////////////////////////////////////////
void spit (const std::string& file, const std::string& contents)
{
std::ofstream out (file.c_str ());
if (out.good ())
{
out << contents;
out.close ();
}
else
throw std::string ("Could not write file '") + file + "'";
}
////////////////////////////////////////////////////////////////////////////////