//////////////////////////////////////////////////////////////////////////////// // Accept a list of projects, and return an indented list // that reflects the hierarchy. // // Input - "one" // "one.two" // "one.two.three" // "one.four" // "two" // Output - "one" // " one.two" // " one.two.three" // " one.four" // "two" // // There are two optional arguments, 'whitespace', and 'delimiter', // // - whitespace is the string used to build the prefixes of indented items. // - defaults to two spaces, " " // - delimiter is the character used to split up projects into subprojects. // - defaults to the period, '.' // const std::vector<std::string> indentTree ( const std::vector<std::string>& values, const std::string& whitespace /* = " " */, char delimiter/* = '.' */) { std::vector <std::string> modified; std::vector <std::string>::const_iterator i; for (i = values.begin (); i != values.end (); ++i) modified.push_back (indentProject (*i, whitespace, delimiter)); return modified; }
int main (int argc, char** argv) { UnitTest t (40); // TODO bool confirm (const std::string&); // TODO int confirm3 (const std::string&); // TODO int confirm4 (const std::string&); // TODO void delay (float); // std::string formatBytes (size_t); t.is (formatBytes (0), "0 B", "0 -> 0 B"); t.is (formatBytes (994), "994 B", "994 -> 994 B"); t.is (formatBytes (995), "1.0 KiB", "995 -> 1.0 KiB"); t.is (formatBytes (999), "1.0 KiB", "999 -> 1.0 KiB"); t.is (formatBytes (1000), "1.0 KiB", "1000 -> 1.0 KiB"); t.is (formatBytes (1001), "1.0 KiB", "1001 -> 1.0 KiB"); t.is (formatBytes (999999), "1.0 MiB", "999999 -> 1.0 MiB"); t.is (formatBytes (1000000), "1.0 MiB", "1000000 -> 1.0 MiB"); t.is (formatBytes (1000001), "1.0 MiB", "1000001 -> 1.0 MiB"); t.is (formatBytes (999999999), "1.0 GiB", "999999999 -> 1.0 GiB"); t.is (formatBytes (1000000000), "1.0 GiB", "1000000000 -> 1.0 GiB"); t.is (formatBytes (1000000001), "1.0 GiB", "1000000001 -> 1.0 GiB"); // TODO const std::string uuid (); // TODO These are in feedback.cpp, not util.cpp. // std::string taskDiff (const Task&, const Task&); Task left; left.set ("zero", "0"); left.set ("one", 1); left.set ("two", 2); Task right; right.set ("zero", "00"); right.set ("two", 2); right.set ("three", 3); Task rightAgain (right); std::string output = taskDifferences (left, right); t.ok (taskDiff (left, right), "Detected changes"); t.ok (output.find ("Zero will be changed from '0' to '00'") != std::string::npos, "Detected change zero:0 -> zero:00"); t.ok (output.find ("One will be deleted") != std::string::npos, "Detected deletion one:1 ->"); t.ok (output.find ("Two") == std::string::npos, "Detected no change two:2 -> two:2"); t.ok (output.find ("Three will be set to '3'") != std::string::npos, "Detected addition -> three:3"); t.notok (taskDiff (right, rightAgain), "No changes detected"); output = taskDifferences (right, rightAgain); t.ok (output.find ("No changes will be made") != std::string::npos, "No changes detected"); // void combine (std::vector <int>&, const std::vector <int>&); std::vector <int> vleft; vleft.push_back (1); vleft.push_back (2); vleft.push_back (3); std::vector <int> vright; vright.push_back (4); combine (vleft, vright); t.is (vleft.size (), (size_t)4, "1,2,3 + 4 -> [4]"); t.is (vleft[0], 1, "1,2,3 + 4 -> 1,2,3,4"); t.is (vleft[1], 2, "1,2,3 + 4 -> 1,2,3,4"); t.is (vleft[2], 3, "1,2,3 + 4 -> 1,2,3,4"); t.is (vleft[3], 4, "1,2,3 + 4 -> 1,2,3,4"); vright.push_back (3); vright.push_back (5); combine (vleft, vright); t.is (vleft.size (), (size_t)5, "1,2,3,4 + 3,4,5 -> [5]"); t.is (vleft[0], 1, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); t.is (vleft[1], 2, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); t.is (vleft[2], 3, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); t.is (vleft[3], 4, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); t.is (vleft[4], 5, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); // std::vector<std::string> indentTree (const std::vector<std::string>&, const std::string whitespace=" ", char delimiter='.'); std::vector <std::string> flat; flat.push_back ("one"); flat.push_back ("one.two"); flat.push_back ("one.two.three"); flat.push_back ("one.four"); flat.push_back ("two"); std::vector <std::string> structured = indentTree (flat, " ", '.'); t.is (structured.size (), (size_t) 5, "indentTree yields 5 strings"); t.is (structured[0], "one", "indentTree 'one' -> 'one'"); t.is (structured[1], " one.two", "indentTree 'one.two' -> ' one.two'"); t.is (structured[2], " one.two.three", "indentTree 'one.two.three' -> ' one.two.three'"); t.is (structured[3], " one.four", "indentTree 'one.four' -> ' one.four'"); t.is (structured[4], "two", "indentTree 'two' -> 'two'"); // std::vector<std::string> indentProject (const std::string&, const std::string whitespace=" ", char delimiter='.'); t.is (indentProject (""), "", "indentProject '' -> ''"); t.is (indentProject ("one"), "one", "indentProject 'one' -> 'one'"); t.is (indentProject ("one.two"), " one.two", "indentProject 'one.two' -> ' one.two'"); t.is (indentProject ("one.two.three"), " one.two.three", "indentProject 'one.two.three' -> ' one.two.three'"); // TODO const std::string encode (const std::string& value); // TODO const std::string decode (const std::string& value); return 0; }
//////////////////////////////////////////////////////////////////////////////// // Project Remaining Avg Age Complete 0% 100% // A 12 13d 55% XXXXXXXXXXXXX----------- // B 109 3d 12h 10% XXX--------------------- int CmdSummary::execute (std::string& output) { int rc = 0; // Scan the pending tasks. handleRecurrence (); // Apply filter. std::vector <Task> filtered; filter (filtered); context.tdb2.commit (); // Generate unique list of project names from all pending tasks. std::map <std::string, bool> allProjects; std::vector <Task>::iterator task; for (task = filtered.begin (); task != filtered.end (); ++task) if (task->getStatus () == Task::pending) allProjects[task->get ("project")] = false; // Initialize counts, sum. std::map <std::string, int> countPending; std::map <std::string, int> countCompleted; std::map <std::string, double> sumEntry; std::map <std::string, int> counter; time_t now = time (NULL); // Initialize counters. std::map <std::string, bool>::iterator project; for (project = allProjects.begin (); project != allProjects.end (); ++project) { countPending [project->first] = 0; countCompleted [project->first] = 0; sumEntry [project->first] = 0.0; counter [project->first] = 0; } // Count the various tasks. for (task = filtered.begin (); task != filtered.end (); ++task) { std::string project = task->get ("project"); ++counter[project]; if (task->getStatus () == Task::pending || task->getStatus () == Task::waiting) { ++countPending[project]; time_t entry = strtol (task->get ("entry").c_str (), NULL, 10); if (entry) sumEntry[project] = sumEntry[project] + (double) (now - entry); } else if (task->getStatus () == Task::completed) { ++countCompleted[project]; time_t entry = strtol (task->get ("entry").c_str (), NULL, 10); time_t end = strtol (task->get ("end").c_str (), NULL, 10); if (entry && end) sumEntry[project] = sumEntry[project] + (double) (end - entry); } } // Create a table for output. ViewText view; view.width (context.getWidth ()); view.add (Column::factory ("string", STRING_CMD_SUMMARY_PROJECT)); view.add (Column::factory ("string.right", STRING_CMD_SUMMARY_REMAINING)); view.add (Column::factory ("string.right", STRING_CMD_SUMMARY_AVG_AGE)); view.add (Column::factory ("string.right", STRING_CMD_SUMMARY_COMPLETE)); view.add (Column::factory ("string.left_fixed", "0% 100%")); Color bar_color (context.config.get ("color.summary.bar")); Color bg_color (context.config.get ("color.summary.background")); int barWidth = 30; std::vector <std::string> processed; std::map <std::string, bool>::iterator i; for (i = allProjects.begin (); i != allProjects.end (); ++i) { if (countPending[i->first] > 0) { const std::vector <std::string> parents = extractParents (i->first); std::vector <std::string>::const_iterator parent; for (parent = parents.begin (); parent != parents.end (); parent++) { if (std::find (processed.begin (), processed.end (), *parent) == processed.end ()) { int row = view.addRow (); view.set (row, 0, indentProject (*parent)); processed.push_back (*parent); } } int row = view.addRow (); view.set (row, 0, (i->first == "" ? STRING_CMD_SUMMARY_NONE : indentProject (i->first, " ", '.'))); view.set (row, 1, countPending[i->first]); if (counter[i->first]) view.set (row, 2, Duration ((int) (sumEntry[i->first] / (double)counter[i->first])).format ()); int c = countCompleted[i->first]; int p = countPending[i->first]; int completedBar = (c * barWidth) / (c + p); std::string bar; std::string subbar; if (context.color ()) { bar += bar_color.colorize (std::string ( completedBar, ' ')); bar += bg_color.colorize (std::string (barWidth - completedBar, ' ')); } else { bar += std::string ( completedBar, '=') + std::string (barWidth - completedBar, ' '); } view.set (row, 4, bar); char percent[12]; sprintf (percent, "%d%%", 100 * c / (c + p)); view.set (row, 3, percent); processed.push_back (i->first); } } std::stringstream out; if (view.rows ()) { out << optionalBlankLine () << view.render () << optionalBlankLine (); if (view.rows ()) out << format (STRING_CMD_PROJECTS_SUMMARY2, view.rows ()); else out << STRING_CMD_PROJECTS_SUMMARY; out << "\n"; } else { out << STRING_CMD_PROJECTS_NO << "\n"; rc = 1; } output = out.str (); return rc; }