From a32263e7ead83545681d2383f04ea85cf8291e95 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 25 Jun 2009 00:47:07 -0400 Subject: [PATCH] Enhancement - next - The next report is now a custom report. There is also a nasty piece of logic that lets the next report exist as a custom report, and also with it's own handleReportNext function to prep and filter the tasks, then hand off to runCustomReport. --- src/Cmd.cpp | 9 +- src/Config.cpp | 22 ++++ src/TDB.cpp | 16 +-- src/TDB.h | 1 - src/main.h | 2 +- src/report.cpp | 282 +++++++++++++++---------------------------- src/tests/.gitignore | 1 + 7 files changed, 134 insertions(+), 199 deletions(-) diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 9a3d2b6f7..1e6919e54 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -147,12 +147,19 @@ void Cmd::load () if (i->substr (0, 7) == "report.") { std::string report = i->substr (7, std::string::npos); + + // Oh, what a massive hack. Shame. Shame. + // The "next" report is in limbo between being a built-in report and + // a custom report. The projection is defined as a custom report, but + // the restriction is different. + if (report.substr (0, 4) == "next") + continue; + std::string::size_type columns = report.find (".columns"); if (columns != std::string::npos) { report = report.substr (0, columns); - // Make sure a custom report does not clash with a built-in // command. if (std::find (commands.begin (), commands.end (), report) != commands.end ()) diff --git a/src/Config.cpp b/src/Config.cpp index b8106d65a..0f67e8c6f 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -224,6 +224,17 @@ void Config::createDefault (const std::string& home) fprintf (out, "report.waiting.sort=wait+,priority-,project+\n"); // TODO i18n fprintf (out, "report.waiting.filter=status:waiting\n"); // TODO i18n + fprintf (out, "report.all.description=Lists all tasks matching the specified criteria\n"); // TODO i18n + fprintf (out, "report.all.columns=id,project,priority,due,active,age,description\n"); // TODO i18n + fprintf (out, "report.all.labels=ID,Project,Pri,Due,Active,Age,Description\n"); // TODO i18n + fprintf (out, "report.all.sort=due+,priority-,project+\n"); // TODO i18n + + fprintf (out, "report.next.description=Lists all tasks matching the specified criteria\n"); // TODO i18n + fprintf (out, "report.next.columns=id,project,priority,due,active,age,description\n"); // TODO i18n + fprintf (out, "report.next.labels=ID,Project,Pri,Due,Active,Age,Description\n"); // TODO i18n + fprintf (out, "report.next.sort=due+,priority-,project+\n"); // TODO i18n + fprintf (out, "report.next.filter=status:pending\n"); // TODO i18n + fclose (out); std::cout << "Done." << std::endl; // TODO i18n @@ -302,6 +313,17 @@ void Config::setDefaults () set ("report.waiting.labels", "ID,Project,Pri,Wait,Age,Description"); // TODO i18n set ("report.waiting.sort", "wait+,priority-,project+"); // TODO i18n set ("report.waiting.filter", "status:waiting"); // TODO i18n + + set ("report.all.description", "Lists all tasks matching the specified criteria"); // TODO i18n + set ("report.all.columns", "id,project,priority,due,active,age,description"); // TODO i18n + set ("report.all.labels", "ID,Project,Pri,Due,Active,Age,Description"); // TODO i18n + set ("report.all.sort", "due+,priority-,project+"); // TODO i18n + + set ("report.next.description", "Lists all tasks matching the specified criteria"); // TODO i18n + set ("report.next.columns", "id,project,priority,due,active,age,description"); // TODO i18n + set ("report.next.labels", "ID,Project,Pri,Due,Active,Age,Description"); // TODO i18n + set ("report.next.sort", "due+,priority-,project+"); // TODO i18n + set ("report.next.filter", "status:pending"); // TODO i18n } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/TDB.cpp b/src/TDB.cpp index 413d62141..f5efb43f0 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -382,19 +382,6 @@ int TDB::commit () return quantity; } -//////////////////////////////////////////////////////////////////////////////// -// TODO -> FF4 -void TDB::upgrade () -{ - // TODO Read all pending - // TODO Write out all pending - - // TODO Read all completed - // TODO Write out all completed - - throw std::string ("unimplemented TDB::upgrade"); -} - //////////////////////////////////////////////////////////////////////////////// // Scans the pending tasks for any that are completed or deleted, and if so, // moves them to the completed.data file. Returns a count of tasks moved. @@ -435,7 +422,10 @@ int TDB::gc () // Wake up tasks that are waiting. Date wait_date (::atoi (task->get ("wait").c_str ())); if (now > wait_date) + { task->setStatus (Task::pending); + task->remove ("wait"); + } still_pending.push_back (*task); } diff --git a/src/TDB.h b/src/TDB.h index 3ca81c404..5112e4ad9 100644 --- a/src/TDB.h +++ b/src/TDB.h @@ -59,7 +59,6 @@ public: void add (const Task&); // Single task add to pending void update (const Task&); // Single task update to pending int commit (); // Write out all tasks - void upgrade (); // Convert both files to FF4 int gc (); // Clean up pending int nextId (); diff --git a/src/main.h b/src/main.h index 72a048c8a..cb3b07bb0 100644 --- a/src/main.h +++ b/src/main.h @@ -45,7 +45,7 @@ void validSortColumns (const std::vector &, const std::vector &, std::vector &); +void gatherNextTasks (std::vector &); void onChangeCallback (); // recur.cpp diff --git a/src/report.cpp b/src/report.cpp index 643719ecf..bae306c96 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -679,138 +679,50 @@ std::string handleReportSummary () // std::string handleReportNext () { + // Load report configuration. + std::string columnList = context.config.get ("report.next.columns"); + std::string labelList = context.config.get ("report.next.labels"); + std::string sortList = context.config.get ("report.next.sort"); + std::string filterList = context.config.get ("report.next.filter"); + + std::vector filterArgs; + split (filterArgs, filterList, ' '); + { + Cmd cmd ("next"); + Task task; + Sequence sequence; + Subst subst; + Filter filter; + context.parse (filterArgs, cmd, task, sequence, subst, filter); + + context.sequence.combine (sequence); + + // Allow limit to be overridden by the command line. + if (!context.task.has ("limit") && task.has ("limit")) + context.task.set ("limit", task.get ("limit")); + + foreach (att, filter) + context.filter.push_back (*att); + } + // Get all the tasks. std::vector tasks; context.tdb.lock (context.config.get ("locking", true)); handleRecurrence (); - context.tdb.loadPending (tasks, context.filter); + context.tdb.load (tasks, context.filter); context.tdb.commit (); context.tdb.unlock (); // Restrict to matching subset. - std::vector matching; - gatherNextTasks (tasks, matching); + gatherNextTasks (tasks); - initializeColorRules (); - - // Create a table for output. - Table table; - table.setTableWidth (context.getWidth ()); - table.setDateFormat (context.config.get ("dateformat", "m/d/Y")); - table.addColumn ("ID"); - table.addColumn ("Project"); - table.addColumn ("Pri"); - table.addColumn ("Due"); - table.addColumn ("Active"); - table.addColumn ("Age"); - table.addColumn ("Description"); - - if ((context.config.get ("color", true) || context.config.get (std::string ("_forcecolor"), false)) && - context.config.get (std::string ("fontunderline"), "true")) - { - table.setColumnUnderline (0); - table.setColumnUnderline (1); - table.setColumnUnderline (2); - table.setColumnUnderline (3); - table.setColumnUnderline (4); - table.setColumnUnderline (5); - table.setColumnUnderline (6); - } - else - table.setTableDashedUnderline (); - - table.setColumnWidth (0, Table::minimum); - table.setColumnWidth (1, Table::minimum); - table.setColumnWidth (2, Table::minimum); - table.setColumnWidth (3, Table::minimum); - table.setColumnWidth (4, Table::minimum); - table.setColumnWidth (5, Table::minimum); - table.setColumnWidth (6, Table::flexible); - - table.setColumnJustification (0, Table::right); - table.setColumnJustification (3, Table::right); - table.setColumnJustification (5, Table::right); - - table.sortOn (3, Table::ascendingDate); - table.sortOn (2, Table::descendingPriority); - table.sortOn (1, Table::ascendingCharacter); - - // Iterate over each task, and apply selection criteria. - foreach (i, matching) - { - Date now; - - // Now format the matching task. - bool imminent = false; - bool overdue = false; - std::string due = tasks[*i].get ("due"); - if (due.length ()) - { - switch (getDueState (due)) - { - case 2: overdue = true; break; - case 1: imminent = true; break; - case 0: - default: break; - } - - Date dt (::atoi (due.c_str ())); - due = dt.toString (context.config.get ("dateformat", "m/d/Y")); - } - - std::string active; - if (tasks[*i].has ("start")) - active = "*"; - - std::string age; - std::string created = tasks[*i].get ("entry"); - if (created.length ()) - { - Date dt (::atoi (created.c_str ())); - age = formatSeconds ((time_t) (now - dt)); - } - - // All criteria match, so add tasks[*i] to the output table. - int row = table.addRow (); - table.addCell (row, 0, tasks[*i].id); - table.addCell (row, 1, tasks[*i].get ("project")); - table.addCell (row, 2, tasks[*i].get ("priority")); - table.addCell (row, 3, due); - table.addCell (row, 4, active); - table.addCell (row, 5, age); - table.addCell (row, 6, getFullDescription (tasks[*i])); - - if (context.config.get ("color", true) || context.config.get (std::string ("_forcecolor"), false)) - { - Text::color fg = Text::colorCode (tasks[*i].get ("fg")); - Text::color bg = Text::colorCode (tasks[*i].get ("bg")); - autoColorize (tasks[*i], fg, bg); - table.setRowFg (row, fg); - table.setRowBg (row, bg); - - if (fg == Text::nocolor) - { - if (overdue) - table.setCellFg (row, 3, Text::colorCode (context.config.get ("color.overdue", "red"))); - else if (imminent) - table.setCellFg (row, 3, Text::colorCode (context.config.get ("color.due", "yellow"))); - } - } - } - - std::stringstream out; - if (table.rowCount ()) - out << optionalBlankLine () - << table.render () - << optionalBlankLine () - << table.rowCount () - << (table.rowCount () == 1 ? " task" : " tasks") - << std::endl; - else - out << "No matches." - << std::endl; - - return out.str (); + return runCustomReport ( + "next", + columnList, + labelList, + sortList, + filterList, + tasks); } //////////////////////////////////////////////////////////////////////////////// @@ -1848,34 +1760,33 @@ std::string handleReportStats () } //////////////////////////////////////////////////////////////////////////////// -void gatherNextTasks ( - std::vector & tasks, - std::vector & all) +void gatherNextTasks (std::vector & tasks) { // For counting tasks by project. std::map countByProject; std::map matching; - + std::vector filtered; Date now; // How many items per project? Default 3. int limit = context.config.get ("next", 3); // due:< 1wk, pri:* - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - if (tasks[i].has ("due")) + if (task->has ("due")) { - Date d (::atoi (tasks[i].get ("due").c_str ())); + Date d (::atoi (task->get ("due").c_str ())); if (d < now + (7 * 24 * 60 * 60)) // if due:< 1wk { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } @@ -1883,20 +1794,21 @@ void gatherNextTasks ( } // due:*, pri:H - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - if (tasks[i].has ("due")) + if (task->has ("due")) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "H") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } @@ -1904,38 +1816,40 @@ void gatherNextTasks ( } // pri:H - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "H") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } } // due:*, pri:M - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - if (tasks[i].has ("due")) + if (task->has ("due")) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "M") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } @@ -1943,38 +1857,40 @@ void gatherNextTasks ( } // pri:M - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "M") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } } // due:*, pri:L - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - if (tasks[i].has ("due")) + if (task->has ("due")) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "L") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } @@ -1982,47 +1898,47 @@ void gatherNextTasks ( } // pri:L - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "L") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } } // due:, pri: - for (unsigned int i = 0; i < tasks.size (); ++i) + foreach (task, tasks) { - if (tasks[i].getStatus () == Task::pending) + if (task->getStatus () == Task::pending) { - if (tasks[i].has ("due")) + if (task->has ("due")) { - std::string priority = tasks[i].get ("priority"); + std::string priority = task->get ("priority"); if (priority == "") { - std::string project = tasks[i].get ("project"); - if (countByProject[project] < limit && matching.find (i) == matching.end ()) + std::string project = task->get ("project"); + if (countByProject[project] < limit && matching.find (task->id) == matching.end ()) { ++countByProject[project]; - matching[i] = true; + matching[task->id] = true; + filtered.push_back (*task); } } } } } - // Convert map to vector. - foreach (i, matching) - all.push_back (i->first); + tasks = filtered; } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/tests/.gitignore b/src/tests/.gitignore index dd6441689..ffffbf248 100644 --- a/src/tests/.gitignore +++ b/src/tests/.gitignore @@ -15,3 +15,4 @@ subst.t filt.t cmd.t config.t +*.log