Command - count

- Migrated handleCount to CmdCount.
This commit is contained in:
Paul Beckingham 2011-05-28 16:05:07 -04:00
parent 27d6e7cc81
commit a7bc09d487
9 changed files with 114 additions and 35 deletions

View file

@ -7,6 +7,7 @@ include_directories (${CMAKE_SOURCE_DIR}
set (commands_SRCS Command.cpp Command.h
CmdCommands.cpp CmdCommands.h
CmdCount.cpp CmdCount.h
CmdCustom.cpp CmdCustom.h
CmdDiagnostics.cpp CmdDiagnostics.h
CmdEdit.cpp CmdEdit.h

69
src/commands/CmdCount.cpp Normal file
View file

@ -0,0 +1,69 @@
////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006 - 2011, 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
//
////////////////////////////////////////////////////////////////////////////////
#include <sstream>
#include <Context.h>
#include <main.h>
#include <CmdCount.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
CmdCount::CmdCount ()
{
_keyword = "count";
_usage = "task count [<filter>]";
_description = "Shows only the number of matching tasks.";
_read_only = true;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdCount::execute (const std::string& command_line, std::string& output)
{
// Scan the pending tasks, applying any filter.
std::vector <Task> 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 <Task>::iterator it;
for (it = tasks.begin (); it != tasks.end (); ++it)
if (it->getStatus () != Task::recurring)
++count;
std::stringstream out;
out << count << "\n";
output = out.str ();
return 0;
}
////////////////////////////////////////////////////////////////////////////////

42
src/commands/CmdCount.h Normal file
View file

@ -0,0 +1,42 @@
////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006 - 2011, 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
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_CMDCOUNT
#define INCLUDED_CMDCOUNT
#define L10N // Localization complete.
#include <string>
#include <Command.h>
class CmdCount : public Command
{
public:
CmdCount ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -232,10 +232,6 @@ int CmdHelp::execute (const std::string& command_line, std::string& output)
view.set (row, 2, "Displays all possible colors, a named sample, or a "
"legend containing all currently defined colors.");
row = view.addRow ();
view.set (row, 1, "task count [filter]");
view.set (row, 2, "Shows only the number of matching tasks.");
row = view.addRow ();
view.set (row, 1, "task config [name [value | '']]");
view.set (row, 2, "Add, modify and remove settings in the task configuration.");

View file

@ -29,6 +29,7 @@
#include <vector>
#include <Command.h>
#include <CmdCommands.h>
#include <CmdCount.h>
#include <CmdCustom.h>
#include <CmdDiagnostics.h>
#include <CmdEdit.h>
@ -58,6 +59,7 @@ void Command::factory (std::map <std::string, Command*>& all)
c = new CmdCompletionIds (); all[c->keyword ()] = c;
c = new CmdCompletionTags (); all[c->keyword ()] = c;
c = new CmdCompletionVersion (); all[c->keyword ()] = c;
c = new CmdCount (); all[c->keyword ()] = c;
c = new CmdDiagnostics (); all[c->keyword ()] = c;
c = new CmdEdit (); all[c->keyword ()] = c;
c = new CmdExec (); all[c->keyword ()] = c;