Code Cleanup

- Relocated code from helper.cpp to feedback.cpp.
- Fixed typo.
This commit is contained in:
Paul Beckingham 2012-03-03 14:24:41 -05:00
parent 524f7f0919
commit 0f4bcefcff
4 changed files with 90 additions and 89 deletions

2
NEWS
View file

@ -82,7 +82,7 @@ Newly deprecated features in taskwarrior 2.0.0
in the scripts/add-ons/export-ical.pl script.
- Use of 'report.<name>.limit' configuration variable. This is now handled
in the report filter as 'limit:<tasks>'.
- Used of 't', '+', 'enable' and 'enabled' in configuration values to mean
- Use of 't', '+', 'enable' and 'enabled' in configuration values to mean
'on/yes/true'.
---

View file

@ -43,6 +43,8 @@
extern Context context;
static void countTasks (const std::vector <Task>&, const std::string&, int&, int&);
////////////////////////////////////////////////////////////////////////////////
bool taskDiff (const Task& before, const Task& after)
{
@ -406,4 +408,89 @@ void feedback_unblocked (const Task& task)
}
}
///////////////////////////////////////////////////////////////////////////////
std::string onProjectChange (Task& task, bool scope /* = true */)
{
std::stringstream msg;
std::string project = task.get ("project");
if (project != "")
{
if (scope)
msg << format (STRING_HELPER_PROJECT_CHANGE, project)
<< " ";
// Count pending and done tasks, for this project.
int count_pending = 0;
int count_done = 0;
std::vector <Task> all = context.tdb2.all_tasks ();
countTasks (all, project, count_pending, count_done);
// count_done count_pending percentage
// ---------- ------------- ----------
// 0 0 0%
// >0 0 100%
// 0 >0 0%
// >0 >0 calculated
int percentage = 0;
if (count_done == 0)
percentage = 0;
else if (count_pending == 0)
percentage = 100;
else
percentage = (count_done * 100 / (count_done + count_pending));
msg << format (STRING_HELPER_PROJECT_COMPL, project, percentage)
<< " "
<< format (STRING_HELPER_PROJECT_REM, count_pending, count_pending + count_done)
<< "\n";
}
return msg.str ();
}
///////////////////////////////////////////////////////////////////////////////
std::string onProjectChange (Task& task1, Task& task2)
{
if (task1.get ("project") == task2.get ("project"))
return onProjectChange (task1);
std::string messages = onProjectChange (task1);
messages += onProjectChange (task2);
return messages;
}
///////////////////////////////////////////////////////////////////////////////
static void countTasks (
const std::vector <Task>& all,
const std::string& project,
int& count_pending,
int& count_done)
{
std::vector <Task>::const_iterator it;
for (it = all.begin (); it != all.end (); ++it)
{
if (it->get ("project") == project)
{
switch (it->getStatus ())
{
case Task::pending:
case Task::waiting:
++count_pending;
break;
case Task::completed:
++count_done;
break;
case Task::deleted:
case Task::recurring:
default:
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -50,7 +50,6 @@
#include <i18n.h>
#include <main.h>
static void countTasks (const std::vector <Task>&, const std::string&, int&, int&);
extern Context context;
@ -120,89 +119,4 @@ std::string getDueDate (Task& task, const std::string& format)
return due;
}
///////////////////////////////////////////////////////////////////////////////
std::string onProjectChange (Task& task, bool scope /* = true */)
{
std::stringstream msg;
std::string project = task.get ("project");
if (project != "")
{
if (scope)
msg << format (STRING_HELPER_PROJECT_CHANGE, project)
<< " ";
// Count pending and done tasks, for this project.
int count_pending = 0;
int count_done = 0;
std::vector <Task> all = context.tdb2.all_tasks ();
countTasks (all, project, count_pending, count_done);
// count_done count_pending percentage
// ---------- ------------- ----------
// 0 0 0%
// >0 0 100%
// 0 >0 0%
// >0 >0 calculated
int percentage = 0;
if (count_done == 0)
percentage = 0;
else if (count_pending == 0)
percentage = 100;
else
percentage = (count_done * 100 / (count_done + count_pending));
msg << format (STRING_HELPER_PROJECT_COMPL, project, percentage)
<< " "
<< format (STRING_HELPER_PROJECT_REM, count_pending, count_pending + count_done)
<< "\n";
}
return msg.str ();
}
///////////////////////////////////////////////////////////////////////////////
std::string onProjectChange (Task& task1, Task& task2)
{
if (task1.get ("project") == task2.get ("project"))
return onProjectChange (task1);
std::string messages = onProjectChange (task1);
messages += onProjectChange (task2);
return messages;
}
///////////////////////////////////////////////////////////////////////////////
static void countTasks (
const std::vector <Task>& all,
const std::string& project,
int& count_pending,
int& count_done)
{
std::vector <Task>::const_iterator it;
for (it = all.begin (); it != all.end (); ++it)
{
if (it->get ("project") == project)
{
switch (it->getStatus ())
{
case Task::pending:
case Task::waiting:
++count_pending;
break;
case Task::completed:
++count_done;
break;
case Task::deleted:
case Task::recurring:
default:
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -51,8 +51,6 @@ bool nag (Task&);
// helpers.cpp
std::string getFullDescription (Task&, const std::string&);
std::string getDueDate (Task&, const std::string&);
std::string onProjectChange (Task&, bool scope = true);
std::string onProjectChange (Task&, Task&);
// rules.cpp
void initializeColorRules ();
@ -81,6 +79,8 @@ void feedback_affected (const std::string&, int);
void feedback_affected (const std::string&, const Task&);
void feedback_special_tags (const Task&, const std::string&);
void feedback_unblocked (const Task&);
std::string onProjectChange (Task&, bool scope = true);
std::string onProjectChange (Task&, Task&);
// sort.cpp
void sort_tasks (std::vector <Task>&, std::vector <int>&, const std::string&);