- Converted reports/command to return strings

This commit is contained in:
Paul Beckingham
2008-10-23 00:44:21 -04:00
parent ce561a6c43
commit b176591261
7 changed files with 466 additions and 368 deletions

View File

@@ -323,18 +323,18 @@ int main (int argc, char** argv)
tdb.onChange (&onChangeCallback);
}
runTaskCommand (argc, argv, tdb, conf);
std::cout << runTaskCommand (argc, argv, tdb, conf);
}
catch (std::string& error)
{
std::cout << error << std::endl;
std::cerr << error << std::endl;
return -1;
}
catch (...)
{
std::cout << "Unknown error." << std::endl;
std::cerr << "Unknown error." << std::endl;
return -2;
}
@@ -685,17 +685,17 @@ void onChangeCallback ()
if (gConf && gTdb)
{
std::cout << "--- valid globals" << std::endl;
gConf->set ("curses", "off");
gConf->set ("color", "off");
// Determine if shadow file is enabled.
std::string shadowFile = expandPath (gConf->get ("shadow.file"));
if (shadowFile != "")
{
std::string oldCurses = gConf->get ("curses");
std::string oldColor = gConf->get ("color");
gConf->set ("curses", "off");
gConf->set ("color", "off");
std::cout << "--- shadowFile " << shadowFile<< std::endl;
// Capture std::cout for the shadow file.
std::ofstream shadow (shadowFile.c_str ());
std::streambuf* original = std::cout.rdbuf (shadow.rdbuf ());
// Run report. Use shadow.command, using default.command as a fallback
// with "list" as a default.
@@ -703,15 +703,24 @@ void onChangeCallback ()
gConf->get ("default.command", "list"));
std::vector <std::string> args;
split (args, command, ' ');
runTaskCommand (args, *gTdb, *gConf);
std::string result = runTaskCommand (args, *gTdb, *gConf);
std::ofstream out (shadowFile.c_str ());
if (out.good ())
{
out << result;
out.close ();
}
else
throw std::string ("Could not write file '") + shadowFile + "'";
// Restore std::cout.
std::cout.rdbuf (original);
shadow.close ();
std::cout << "--- Complete " << std::endl;
gConf->set ("curses", oldCurses);
gConf->set ("color", oldColor);
}
else
throw std::string ("Could not write to '") + shadowFile + "'.";
throw std::string ("No specified shadow file '") + shadowFile + "'.";
}
else
throw std::string ("Internal error (TDB/Config).");
@@ -729,7 +738,7 @@ void onChangeCallback ()
}
////////////////////////////////////////////////////////////////////////////////
void runTaskCommand (
std::string runTaskCommand (
int argc,
char** argv,
TDB& tdb,
@@ -739,11 +748,11 @@ void runTaskCommand (
for (int i = 1; i < argc; ++i)
args.push_back (argv[i]);
runTaskCommand (args, tdb, conf);
return runTaskCommand (args, tdb, conf);
}
////////////////////////////////////////////////////////////////////////////////
void runTaskCommand (
std::string runTaskCommand (
std::vector <std::string>& args,
TDB& tdb,
Config& conf)
@@ -763,36 +772,40 @@ void runTaskCommand (
T task;
parse (args, command, task, conf);
if (command == "add") handleAdd (tdb, task, conf);
else if (command == "projects") handleProjects (tdb, task, conf);
else if (command == "tags") handleTags (tdb, task, conf);
else if (command == "list") handleList (tdb, task, conf);
else if (command == "info") handleInfo (tdb, task, conf);
else if (command == "undelete") handleUndelete (tdb, task, conf);
else if (command == "long") handleLongList (tdb, task, conf);
else if (command == "ls") handleSmallList (tdb, task, conf);
else if (command == "colors") handleColor ( conf);
else if (command == "completed") handleCompleted (tdb, task, conf);
else if (command == "delete") handleDelete (tdb, task, conf);
else if (command == "start") handleStart (tdb, task, conf);
else if (command == "done") handleDone (tdb, task, conf);
else if (command == "undo") handleUndo (tdb, task, conf);
else if (command == "export") handleExport (tdb, task, conf);
else if (command == "version") handleVersion ( conf);
else if (command == "summary") handleReportSummary (tdb, task, conf);
else if (command == "next") handleReportNext (tdb, task, conf);
else if (command == "history") handleReportHistory (tdb, task, conf);
else if (command == "ghistory") handleReportGHistory (tdb, task, conf);
else if (command == "calendar") handleReportCalendar (tdb, task, conf);
else if (command == "active") handleReportActive (tdb, task, conf);
else if (command == "overdue") handleReportOverdue (tdb, task, conf);
else if (command == "oldest") handleReportOldest (tdb, task, conf);
else if (command == "newest") handleReportNewest (tdb, task, conf);
else if (command == "stats") handleReportStats (tdb, task, conf);
else if (command == "usage") handleReportUsage (tdb, task, conf);
else if (command == "" && task.getId ()) handleModify (tdb, task, conf);
else if (command == "help") longUsage (conf);
else shortUsage (conf);
std::string out = "";
if (command == "add") handleAdd (tdb, task, conf);
else if (command == "projects") out = handleProjects (tdb, task, conf);
else if (command == "tags") out = handleTags (tdb, task, conf);
else if (command == "list") out = handleList (tdb, task, conf);
else if (command == "info") out = handleInfo (tdb, task, conf);
else if (command == "undelete") out = handleUndelete (tdb, task, conf);
else if (command == "long") out = handleLongList (tdb, task, conf);
else if (command == "ls") out = handleSmallList (tdb, task, conf);
else if (command == "colors") out = handleColor ( conf);
else if (command == "completed") out = handleCompleted (tdb, task, conf);
else if (command == "delete") out = handleDelete (tdb, task, conf);
else if (command == "start") out = handleStart (tdb, task, conf);
else if (command == "done") handleDone (tdb, task, conf);
else if (command == "undo") out = handleUndo (tdb, task, conf);
else if (command == "export") handleExport (tdb, task, conf);
else if (command == "version") out = handleVersion ( conf);
else if (command == "summary") out = handleReportSummary (tdb, task, conf);
else if (command == "next") out = handleReportNext (tdb, task, conf);
else if (command == "history") out = handleReportHistory (tdb, task, conf);
else if (command == "ghistory") out = handleReportGHistory (tdb, task, conf);
else if (command == "calendar") out = handleReportCalendar (tdb, task, conf);
else if (command == "active") out = handleReportActive (tdb, task, conf);
else if (command == "overdue") out = handleReportOverdue (tdb, task, conf);
else if (command == "oldest") out = handleReportOldest (tdb, task, conf);
else if (command == "newest") out = handleReportNewest (tdb, task, conf);
else if (command == "stats") out = handleReportStats (tdb, task, conf);
else if (command == "usage") out = handleReportUsage (tdb, task, conf);
else if (command == "" && task.getId ()) handleModify (tdb, task, conf);
else if (command == "help") longUsage (conf);
else shortUsage (conf);
return out;
}
////////////////////////////////////////////////////////////////////////////////