diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index 0f8e291..a627dfc 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -103,7 +103,9 @@ class Taskwarrior */ public function filterPending($filter = '') { - return $this->filter($filter . ' status:pending'); + $tasks = $this->filter($filter . ' status:pending'); + + return $this->sort($tasks); } /** @@ -235,6 +237,8 @@ class Taskwarrior if ($task->getDue()) { $options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z'); + } else { + $options[] = 'due:'; } $options[] = $task->getDescription(); @@ -254,6 +258,23 @@ class Taskwarrior $this->setValue($task, 'status', $clean->getStatus()); } + /** + * @param Task[] $tasks + * @return Task[] + */ + private function sort(array $tasks) + { + usort($tasks, function (Task $a, Task $b) { + if(0 != $diff = $b->getUrgency() - $a->getUrgency()) { + return $diff; + } + + return $a->getEntry() >= $b->getEntry() ? 1 : -1; + }); + + return $tasks; + } + /** * @return ProcessBuilder */ @@ -295,7 +316,7 @@ class Taskwarrior private function setValue(Task $task, $attr, $value) { $reflectionClass = new \ReflectionClass('DavidBadura\Taskwarrior\Task'); - $prop = $reflectionClass->getProperty($attr); + $prop = $reflectionClass->getProperty($attr); $prop->setAccessible(true); $prop->setValue($task, $value); } diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php index f44e69e..0128ae2 100644 --- a/tests/TaskwarriorTest.php +++ b/tests/TaskwarriorTest.php @@ -217,19 +217,56 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase $task3 = $this->taskwarrior->find($task1->getUuid()); $this->assertEquals($newDate, $task3->getDue()); + + $task2->setDue(null); + + $this->taskwarrior->save($task2); + $this->taskwarrior->clear(); + + $task3 = $this->taskwarrior->find($task1->getUuid()); + $this->assertNull($task3->getDue()); } public function testUrgency() { - $date = $this->createDateTime('1989-01-08 11:12:13'); - $task1 = new Task(); $task1->setDescription('foo1'); - $task1->setDue($date); + $task1->setDue($this->createDateTime('1989-01-08 11:12:13')); $this->taskwarrior->save($task1); $this->assertEquals(12, $task1->getUrgency()); + + $task1->setDue(null); + + $this->taskwarrior->save($task1); + + $this->assertEquals(0, $task1->getUrgency()); + } + + public function testUrgencySort() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + + $task2 = new Task(); + $task2->setDescription('foo2'); + $task2->setDue($this->createDateTime('1989-01-08 11:12:13')); + + $task3 = new Task(); + $task3->setDescription('foo3'); + + $this->taskwarrior->save($task1); + $this->taskwarrior->save($task2); + $this->taskwarrior->save($task3); + + $this->assertEquals(0, $task1->getUrgency()); + $this->assertEquals(12, $task2->getUrgency()); + $this->assertEquals(0, $task3->getUrgency()); + + $tasks = $this->taskwarrior->filterPending(); + + $this->assertEquals(array($task2, $task1, $task3), $tasks); } /**