From c933ed2cf7392fbf38dc587dac66553a7619927f Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 12 Apr 2014 16:03:19 -0400 Subject: [PATCH] Code Cleanup - Migrated the recur.cpp getDueState() function into Task::getDateState(), which can assess any date attribute. - Improved implementation to distinguish between: - not due, or not due for rc.due days - due after today - due later today - due earlier today - due before today This greater precision should address some outstanding issues. --- src/Task.cpp | 46 ++++++++++++++++++++++-- src/Task.h | 5 +++ src/commands/CmdCalendar.cpp | 27 +++++++------- src/main.h | 2 -- src/recur.cpp | 69 ++---------------------------------- src/rules.cpp | 7 ++-- 6 files changed, 68 insertions(+), 88 deletions(-) diff --git a/src/Task.cpp b/src/Task.cpp index 8bee7d291..909538966 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -323,6 +323,40 @@ void Task::setStatus (Task::status status) recalc_urgency = true; } +//////////////////////////////////////////////////////////////////////////////// +// Determines status of a date attribute. +Task::dateState Task::getDateState (const std::string& name) const +{ + std::string value = get (name); + if (value.length ()) + { + Date reference (value); + Date now; + Date today ("today"); + + if (reference < today) + return dateBeforeToday; + + if (reference.sameDay (now)) + { + if (reference < now) + return dateEarlierToday; + else + return dateLaterToday; + } + + int imminentperiod = context.config.getInteger ("due"); + if (imminentperiod == 0) + return dateAfterToday; + + Date imminentDay = today + imminentperiod * 86400; + if (reference < imminentDay) + return dateAfterToday; + } + + return dateNotDue; +} + #ifdef PRODUCT_TASKWARRIOR //////////////////////////////////////////////////////////////////////////////// // Ready means pending, not blocked and either not scheduled or scheduled before @@ -345,7 +379,9 @@ bool Task::is_due () const if (status != Task::completed && status != Task::deleted) { - if (getDueState (get ("due")) == 1) + Task::dateState state = getDateState ("due"); + if (state == dateAfterToday || + state == dateLaterToday) return true; } } @@ -381,7 +417,9 @@ bool Task::is_duetoday () const if (status != Task::completed && status != Task::deleted) { - if (getDueState (get ("due")) == 2) + Task::dateState state = getDateState ("due"); + if (state == dateEarlierToday || + state == dateLaterToday) return true; } } @@ -479,7 +517,9 @@ bool Task::is_overdue () const if (status != Task::completed && status != Task::deleted) { - if (getDueState (get ("due")) == 3) + Task::dateState state = getDateState ("due"); + if (state == dateEarlierToday || + state == dateBeforeToday) return true; } } diff --git a/src/Task.h b/src/Task.h index 74372968a..ed1c024b9 100644 --- a/src/Task.h +++ b/src/Task.h @@ -72,6 +72,9 @@ public: // Status values. enum status {pending, completed, deleted, recurring, waiting}; + // Date state values. + enum dateState {dateNotDue, dateAfterToday, dateLaterToday, dateEarlierToday, dateBeforeToday}; + // Public data. int id; float urgency_value; @@ -117,6 +120,8 @@ public: status getStatus () const; void setStatus (status); + dateState getDateState (const std::string&) const; + int getTagCount () const; bool hasTag (const std::string&) const; void addTag (const std::string&); diff --git a/src/commands/CmdCalendar.cpp b/src/commands/CmdCalendar.cpp index 7e903b97b..484710b73 100644 --- a/src/commands/CmdCalendar.cpp +++ b/src/commands/CmdCalendar.cpp @@ -561,23 +561,24 @@ std::string CmdCalendar::renderMonths ( duedmy.month () == months[mpl] && duedmy.year () == years[mpl]) { - switch (getDueState (due)) + switch (task->getDateState ("due")) { - case 1: // imminent - cellColor.blend (color_due); - break; + case Task::dateNotDue: + break; - case 2: // today - cellColor.blend (color_duetoday); - break; + case Task::dateAfterToday: + cellColor.blend (color_due); + break; - case 3: // overdue - cellColor.blend (color_overdue); - break; + case Task::dateEarlierToday: + case Task::dateLaterToday: + cellColor.blend (color_duetoday); + cellColor.blend (color_duetoday); + break; - case 0: // not due at all - default: - break; + case Task::dateBeforeToday: + cellColor.blend (color_overdue); + break; } } } diff --git a/src/main.h b/src/main.h index 9f9389242..df6990882 100644 --- a/src/main.h +++ b/src/main.h @@ -43,8 +43,6 @@ void handleRecurrence (); Date getNextRecurrence (Date&, std::string&); bool generateDueDates (Task&, std::vector &); void updateRecurrenceMask (Task&); -int getDueState (const std::string&); -int getDueState (const Date&); bool nag (Task&); // rules.cpp diff --git a/src/recur.cpp b/src/recur.cpp index 5c6b55d1c..b5e69523c 100644 --- a/src/recur.cpp +++ b/src/recur.cpp @@ -382,71 +382,6 @@ void updateRecurrenceMask (Task& task) } } -//////////////////////////////////////////////////////////////////////////////// -// Determines whether a task is overdue. Returns -// 0 = not due at all -// 1 = imminent -// 2 = today -// 3 = overdue -int getDueState (const std::string& due) -{ - if (due.length ()) - { - Date dt (::atoi (due.c_str ())); - - // rightNow is the current date + time. - static Date rightNow; - Date thisDay (rightNow.month (), rightNow.day (), rightNow.year ()); - - if (dt < rightNow) - return 3; - - if (rightNow.sameDay (dt)) - return 2; - - int imminentperiod = context.config.getInteger ("due"); - - if (imminentperiod == 0) - return 1; - - Date imminentDay = thisDay + imminentperiod * 86400; - if (dt < imminentDay) - return 1; - } - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -// Determines whether a task is overdue. Returns -// 0 = not due at all -// 1 = imminent -// 2 = today -// 3 = overdue -int getDueState (const Date& due) -{ - // rightNow is the current date + time. - static Date rightNow; - Date thisDay (rightNow.month (), rightNow.day (), rightNow.year ()); - - if (due < rightNow) - return 3; - - if (rightNow.sameDay (due)) - return 2; - - int imminentperiod = context.config.getInteger ("due"); - - if (imminentperiod == 0) - return 1; - - Date imminentDay = thisDay + imminentperiod * 86400; - if (due < imminentDay) - return 1; - - return 0; -} - //////////////////////////////////////////////////////////////////////////////// // Returns a Boolean indicator as to whether a nag message was generated, so // that commands can control the number of nag messages displayed (ie one is @@ -477,7 +412,7 @@ bool nag (Task& task) { if (t->id == task.id) { - if (getDueState (t->get ("due")) == 3) + if (t->getDateState ("due") == Task::dateBeforeToday) isOverdue = true; std::string priority = t->get ("priority"); @@ -486,7 +421,7 @@ bool nag (Task& task) } else if (t->getStatus () == Task::pending) { - if (getDueState (t->get ("due")) == 3) + if (t->getDateState ("due") == Task::dateBeforeToday) overdue++; std::string priority = t->get ("priority"); diff --git a/src/rules.cpp b/src/rules.cpp index 88f5f11b4..3d7652a70 100644 --- a/src/rules.cpp +++ b/src/rules.cpp @@ -222,7 +222,7 @@ static void colorizeDue (Task& task, const Color& base, Color& c) Task::status status = task.getStatus (); if (status != Task::completed && status != Task::deleted && - getDueState (task.get ("due")) == 1) + task.getDateState ("due") == Task::dateAfterToday) c.blend (base); } } @@ -233,9 +233,10 @@ static void colorizeDueToday (Task& task, const Color& base, Color& c) if (task.has ("due")) { Task::status status = task.getStatus (); + Task::dateState dateState = task.getDateState ("due"); if (status != Task::completed && status != Task::deleted && - getDueState (task.get ("due")) == 2) + (dateState == Task::dateLaterToday || dateState == Task::dateEarlierToday)) c.blend (base); } } @@ -248,7 +249,7 @@ static void colorizeOverdue (Task& task, const Color& base, Color& c) Task::status status = task.getStatus (); if (status != Task::completed && status != Task::deleted && - getDueState (task.get ("due")) == 3) + task.getDateState ("due") == Task::dateBeforeToday) c.blend (base); } }