diff --git a/ChangeLog b/ChangeLog index ba6496c24..75386ef0e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,8 @@ + Added burndown charts - burndown.daily, burndown.weekly, burndown.monthly, that use color.burndown.pending, color.burndown.started and color.burndown.done colors. + + Added feature #546, which is a 'count' command that counts tasks, and is + intended to help scripts that manipulate task output. + Fixed bug #529, where the 'depends' attribute was not mentioned in the task man page (thanks to Dirk Deimeke). + Fixed bug #535 which omitted the holidays-NO.rc file from the packages diff --git a/NEWS b/NEWS index b6e17f3e2..c44725301 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,7 @@ New Features in taskwarrior 1.9.4 - New burndown charts. + - New 'count' helper command. Please refer to the ChangeLog file for full details. There are too many to list here. @@ -10,6 +11,8 @@ New commands in taskwarrior 1.9.4 - 'burndown.daily', 'burndown.weekly', 'burndown.monthly', also with 'burndown' that is an alias to burndown.weekly. + - 'count' accepts a filter, and simply returns a count of the tasks + matching the filter. Used as a helper command by scripts. New configuration options in taskwarrior 1.9.4 diff --git a/doc/man/task.1 b/doc/man/task.1 index 0e3fbee37..e64ce913f 100644 --- a/doc/man/task.1 +++ b/doc/man/task.1 @@ -188,6 +188,10 @@ Overwrites the task database with those files found at the URL. Displays all possible colors, a named sample, or a legend containing all currently defined colors. +.TP +.B count [filter] +Displays only a count of tasks matching the filter. + .TP .B version Shows the taskwarrior version number diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 288249084..fb2774bcf 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -145,6 +145,7 @@ void Cmd::load () commands.push_back ("burndown.daily"); commands.push_back ("burndown.weekly"); commands.push_back ("burndown.monthly"); + commands.push_back ("count"); // Commands whose names are localized. commands.push_back (context.stringtable.get (CMD_ADD, "add")); @@ -250,6 +251,7 @@ bool Cmd::isReadOnlyCommand () command == "burndown.daily" || command == "burndown.weekly" || command == "burndown.monthly" || + command == "count" || command == context.stringtable.get (CMD_CALENDAR, "calendar") || command == context.stringtable.get (CMD_COLORS, "colors") || command == context.stringtable.get (CMD_DIAGNOSTICS, "diagnostics") || diff --git a/src/Context.cpp b/src/Context.cpp index a3ed18cb3..893dac89c 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -249,6 +249,7 @@ int Context::dispatch (std::string &out) else if (cmd.command == "push") { handlePush (out); } else if (cmd.command == "pull") { handlePull (out); } else if (cmd.command == "diagnostics") { handleDiagnostics (out); } + else if (cmd.command == "count") { rc = handleCount (out); } else if (cmd.command == "_projects") { rc = handleCompletionProjects (out); } else if (cmd.command == "_tags") { rc = handleCompletionTags (out); } else if (cmd.command == "_commands") { rc = handleCompletionCommands (out); } diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 40ed5715c..1c868a4af 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -147,6 +147,8 @@ Hooks::Hooks () validProgramEvents.push_back ("post-done-command"); validProgramEvents.push_back ("pre-duplicate-command"); validProgramEvents.push_back ("post-duplicate-command"); + validProgramEvents.push_back ("pre-count-command"); + validProgramEvents.push_back ("post-count-command"); validProgramEvents.push_back ("pre-edit-command"); validProgramEvents.push_back ("post-edit-command"); validProgramEvents.push_back ("pre-export-command"); diff --git a/src/command.cpp b/src/command.cpp index 0d01725bf..44de0e87f 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -2032,6 +2032,37 @@ int handleDuplicate (std::string& outs) return rc; } +//////////////////////////////////////////////////////////////////////////////// +int handleCount (std::string& outs) +{ + int rc = 0; + + if (context.hooks.trigger ("pre-count-command")) + { + // Scan the pending tasks, applying any filter. + std::vector tasks; + context.tdb.lock (context.config.getBoolean ("locking")); + handleRecurrence (); + context.tdb.load (tasks, context.filter); + context.tdb.commit (); + context.tdb.unlock (); + + // Find number of matching tasks. Skip recurring parent tasks. + int count = 0; + std::vector ::iterator it; + for (it = tasks.begin (); it != tasks.end (); ++it) + if (it->getStatus () != Task::recurring) + ++count; + + std::stringstream out; + out << count << "\n"; + outs = out.str (); + context.hooks.trigger ("post-count-command"); + } + + return rc; +} + //////////////////////////////////////////////////////////////////////////////// #ifdef FEATURE_SHELL void handleShell () diff --git a/src/main.h b/src/main.h index 8f91473aa..e28399bac 100644 --- a/src/main.h +++ b/src/main.h @@ -78,6 +78,7 @@ int handleColor (std::string&); int handleAnnotate (std::string&); int handleDenotate (std::string&); int handleDuplicate (std::string&); +int handleCount (std::string&); void handleUndo (); void handleMerge (std::string&); void handlePush (std::string&); diff --git a/src/report.cpp b/src/report.cpp index 759d2a856..53c872857 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -244,6 +244,10 @@ int shortUsage (std::string& outs) table.addCell (row, 2, "Displays all possible colors, a named sample, or a " "legend containing all currently defined colors."); + row = table.addRow (); + table.addCell (row, 1, "task count [filter]"); + table.addCell (row, 2, "Shows only the number of matching tasks."); + row = table.addRow (); table.addCell (row, 1, "task version"); table.addCell (row, 2, "Shows the task version number."); diff --git a/src/tests/count.t b/src/tests/count.t new file mode 100755 index 000000000..c98a7a14c --- /dev/null +++ b/src/tests/count.t @@ -0,0 +1,79 @@ +#! /usr/bin/perl +################################################################################ +## taskwarrior - a command line task list manager. +## +## Copyright 2010, Paul Beckingham, Federico Hernandez. +## All rights reserved. +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, write to the +## +## Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, +## Boston, MA +## 02110-1301 +## USA +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 10; + +# Create the rc file. +if (open my $fh, '>', 'count.rc') +{ + print $fh "data.location=.\n", + "confirmation=off\n"; + close $fh; + ok (-r 'count.rc', 'Created count.rc'); +} + +# Test the count command. +qx{../task rc:count.rc add one}; +qx{../task rc:count.rc log two}; +qx{../task rc:count.rc add three}; +qx{../task rc:count.rc delete 3}; +qx{../task rc:count.rc add four wait:eom}; +qx{../task rc:count.rc add five due:eom recur:monthly}; + +my $output = qx{../task rc:count.rc count}; +like ($output, qr/^5$/ms, 'count'); + +$output = qx{../task rc:count.rc count status:deleted}; +like ($output, qr/^1$/ms, 'count status:deleted'); + +$output = qx{../task rc:count.rc count e}; +like ($output, qr/^3$/ms, 'count e'); + +$output = qx{../task rc:count.rc count description.startswith:f}; +like ($output, qr/^2$/ms, 'count description.startswith:f'); + +$output = qx{../task rc:count.rc count due.any:}; +like ($output, qr/^1$/ms, 'count due.any:'); + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'completed.data'; +ok (!-r 'completed.data', 'Removed completed.data'); + +unlink 'undo.data'; +ok (!-r 'undo.data', 'Removed undo.data'); + +unlink 'count.rc'; +ok (!-r 'count.rc', 'Removed count.rc'); + +exit 0; +