Commands - config

- Migrated config and _config to CmdConfig.
This commit is contained in:
Paul Beckingham 2011-05-30 11:18:59 -04:00
parent 0f5a4434ff
commit 6cf7644e02
9 changed files with 247 additions and 149 deletions

View file

@ -129,13 +129,11 @@ void Cmd::load ()
{ {
if (commands.size () == 0) if (commands.size () == 0)
{ {
commands.push_back ("_config");
commands.push_back ("_query"); commands.push_back ("_query");
commands.push_back ("export.csv"); commands.push_back ("export.csv");
commands.push_back ("export.ical"); commands.push_back ("export.ical");
commands.push_back ("export.yaml"); commands.push_back ("export.yaml");
commands.push_back ("calendar"); commands.push_back ("calendar");
commands.push_back ("config");
commands.push_back ("delete"); commands.push_back ("delete");
commands.push_back ("done"); commands.push_back ("done");
commands.push_back ("duplicate"); commands.push_back ("duplicate");
@ -202,13 +200,11 @@ void Cmd::allCommands (std::vector <std::string>& all) const
// Commands that do not directly modify the data files. // Commands that do not directly modify the data files.
bool Cmd::isReadOnlyCommand () bool Cmd::isReadOnlyCommand ()
{ {
if (command == "_config" || if (command == "_query" ||
command == "_query" ||
command == "export.csv" || command == "export.csv" ||
command == "export.ical" || command == "export.ical" ||
command == "export.yaml" || command == "export.yaml" ||
command == "calendar" || command == "calendar" ||
command == "config" ||
command == "push" || command == "push" ||
command == "summary" || command == "summary" ||
command == "timesheet" || command == "timesheet" ||

View file

@ -247,8 +247,7 @@ int Context::dispatch (std::string &out)
Timer t ("Context::dispatch"); Timer t ("Context::dispatch");
// TODO Chain-of-command pattern dispatch. // TODO Chain-of-command pattern dispatch.
if (cmd.command == "config") { rc = handleConfig (out); } if (cmd.command == "summary") { rc = handleReportSummary (out); }
else if (cmd.command == "summary") { rc = handleReportSummary (out); }
else if (cmd.command == "calendar") { rc = handleReportCalendar (out); } else if (cmd.command == "calendar") { rc = handleReportCalendar (out); }
else if (cmd.command == "timesheet") { rc = handleReportTimesheet (out); } else if (cmd.command == "timesheet") { rc = handleReportTimesheet (out); }
else if (cmd.command == "done") { rc = handleDone (out); } else if (cmd.command == "done") { rc = handleDone (out); }
@ -263,7 +262,6 @@ int Context::dispatch (std::string &out)
handleMerge (out); } handleMerge (out); }
else if (cmd.command == "push") { handlePush (out); } else if (cmd.command == "push") { handlePush (out); }
else if (cmd.command == "pull") { handlePull (out); } else if (cmd.command == "pull") { handlePull (out); }
else if (cmd.command == "_config") { rc = handleCompletionConfig (out); }
else if (cmd.command == "_query") { rc = handleQuery (out); } else if (cmd.command == "_query") { rc = handleQuery (out); }
else if (cmd.command == "" && else if (cmd.command == "" &&
sequence.size ()) { rc = handleModify (out); } sequence.size ()) { rc = handleModify (out); }

View file

@ -51,21 +51,6 @@
extern Context context; extern Context context;
////////////////////////////////////////////////////////////////////////////////
int handleCompletionConfig (std::string& outs)
{
std::vector <std::string> configs;
context.config.all (configs);
std::sort (configs.begin (), configs.end ());
std::stringstream out;
foreach (config, configs)
out << *config << "\n";
outs = out.str ();
return 0;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int handleQuery (std::string& outs) int handleQuery (std::string& outs)
{ {
@ -290,126 +275,6 @@ void handlePull (std::string&)
"'pull.default.uri' entry in your .taskrc file."); "'pull.default.uri' entry in your .taskrc file.");
} }
////////////////////////////////////////////////////////////////////////////////
int handleConfig (std::string& outs)
{
int rc = 0;
std::stringstream out;
// Obtain the arguments from the description. That way, things like '--'
// have already been handled.
std::vector <std::string> args;
split (args, context.task.get ("description"), ' ');
// Support:
// task config name value # set name to value
// task config name "" # set name to blank
// task config name # remove name
if (args.size () > 0)
{
std::string name = args[0];
std::string value = "";
if (args.size () > 1)
{
for (unsigned int i = 1; i < args.size (); ++i)
{
if (i > 1)
value += " ";
value += args[i];
}
}
if (name != "")
{
bool change = false;
// Read .taskrc (or equivalent)
std::string contents;
File::read (context.config.original_file, contents);
// task config name value
// task config name ""
if (args.size () > 1 ||
context.args[context.args.size () - 1] == "")
{
// Find existing entry & overwrite
std::string::size_type pos = contents.find (name + "=");
if (pos != std::string::npos)
{
std::string::size_type eol = contents.find_first_of ("\r\f\n", pos);
if (eol == std::string::npos)
throw std::string ("Cannot find EOL after entry '") + name + "'.";
if (confirm (std::string ("Are you sure you want to change the value of '")
+ name
+ "' from '"
+ context.config.get(name)
+ "' to '"
+ value + "'?"))
{
contents = contents.substr (0, pos)
+ name + "=" + value
+ contents.substr (eol);
change = true;
}
}
// Not found, so append instead.
else
{
if (confirm (std::string ("Are you sure you want to add '") + name + "' with a value of '" + value + "'?"))
{
contents = contents
+ "\n"
+ name + "=" + value
+ "\n";
change = true;
}
}
}
// task config name
else
{
// Remove name
std::string::size_type pos = contents.find (name + "=");
if (pos == std::string::npos)
throw std::string ("No entry named '") + name + "' found.";
std::string::size_type eol = contents.find_first_of ("\r\f\n", pos);
if (eol == std::string::npos)
throw std::string ("Cannot find EOL after entry '") + name + "'.";
if (confirm (std::string ("Are you sure you want to remove '") + name + "'?"))
{
contents = contents.substr (0, pos) + contents.substr (eol + 1);
change = true;
}
}
// Write .taskrc (or equivalent)
if (change)
{
File::write (context.config.original_file, contents);
out << "Config file "
<< context.config.original_file.data
<< " modified.\n";
}
else
out << "No changes made.\n";
}
else
throw std::string ("Specify the name of a config variable to modify.");
outs = out.str ();
}
else
throw std::string ("Specify the name of a config variable to modify.");
return rc;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int handleDelete (std::string& outs) int handleDelete (std::string& outs)
{ {

View file

@ -12,6 +12,7 @@ set (commands_SRCS Command.cpp Command.h
CmdBurndown.cpp CmdBurndown.h CmdBurndown.cpp CmdBurndown.h
CmdCommands.cpp CmdCommands.h CmdCommands.cpp CmdCommands.h
CmdColor.cpp CmdColor.h CmdColor.cpp CmdColor.h
CmdConfig.cpp CmdConfig.h
CmdCount.cpp CmdCount.h CmdCount.cpp CmdCount.h
CmdCustom.cpp CmdCustom.h CmdCustom.cpp CmdCustom.h
CmdDenotate.cpp CmdDenotate.h CmdDenotate.cpp CmdDenotate.h

192
src/commands/CmdConfig.cpp Normal file
View file

@ -0,0 +1,192 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <algorithm>
#include <Context.h>
#include <text.h>
#include <util.h>
#include <CmdConfig.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
CmdConfig::CmdConfig ()
{
_keyword = "config";
_usage = "task config [name [value | '']]";
_description = "Add, modify and remove settings in the task configuration.";
_read_only = true;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdConfig::execute (const std::string&, std::string& output)
{
int rc = 0;
std::stringstream out;
// Obtain the arguments from the description. That way, things like '--'
// have already been handled.
std::vector <std::string> args;
split (args, context.task.get ("description"), ' ');
// Support:
// task config name value # set name to value
// task config name "" # set name to blank
// task config name # remove name
if (args.size () > 0)
{
std::string name = args[0];
std::string value = "";
if (args.size () > 1)
{
for (unsigned int i = 1; i < args.size (); ++i)
{
if (i > 1)
value += " ";
value += args[i];
}
}
if (name != "")
{
bool change = false;
// Read .taskrc (or equivalent)
std::string contents;
File::read (context.config.original_file, contents);
// task config name value
// task config name ""
if (args.size () > 1 ||
context.args[context.args.size () - 1] == "")
{
// Find existing entry & overwrite
std::string::size_type pos = contents.find (name + "=");
if (pos != std::string::npos)
{
std::string::size_type eol = contents.find_first_of ("\r\f\n", pos);
if (eol == std::string::npos)
throw std::string ("Cannot find EOL after entry '") + name + "'.";
if (confirm (std::string ("Are you sure you want to change the value of '")
+ name
+ "' from '"
+ context.config.get(name)
+ "' to '"
+ value + "'?"))
{
contents = contents.substr (0, pos)
+ name + "=" + value
+ contents.substr (eol);
change = true;
}
}
// Not found, so append instead.
else
{
if (confirm (std::string ("Are you sure you want to add '") + name + "' with a value of '" + value + "'?"))
{
contents = contents
+ "\n"
+ name + "=" + value
+ "\n";
change = true;
}
}
}
// task config name
else
{
// Remove name
std::string::size_type pos = contents.find (name + "=");
if (pos == std::string::npos)
throw std::string ("No entry named '") + name + "' found.";
std::string::size_type eol = contents.find_first_of ("\r\f\n", pos);
if (eol == std::string::npos)
throw std::string ("Cannot find EOL after entry '") + name + "'.";
if (confirm (std::string ("Are you sure you want to remove '") + name + "'?"))
{
contents = contents.substr (0, pos) + contents.substr (eol + 1);
change = true;
}
}
// Write .taskrc (or equivalent)
if (change)
{
File::write (context.config.original_file, contents);
out << "Config file "
<< context.config.original_file.data
<< " modified.\n";
}
else
out << "No changes made.\n";
}
else
throw std::string ("Specify the name of a config variable to modify.");
output = out.str ();
}
else
throw std::string ("Specify the name of a config variable to modify.");
return rc;
}
////////////////////////////////////////////////////////////////////////////////
CmdCompletionConfig::CmdCompletionConfig ()
{
_keyword = "_config";
_usage = "task _config";
_description = "Lists all supported configuration variables, for completion "
"purposes.";
_read_only = true;
_displays_id = false;
}
////////////////////////////////////////////////////////////////////////////////
int CmdCompletionConfig::execute (const std::string&, std::string& output)
{
std::vector <std::string> configs;
context.config.all (configs);
std::sort (configs.begin (), configs.end ());
std::vector <std::string>::iterator config;
for (config = configs.begin (); config != configs.end (); ++config)
output += *config + "\n";
return 0;
}
////////////////////////////////////////////////////////////////////////////////

49
src/commands/CmdConfig.h Normal file
View file

@ -0,0 +1,49 @@
////////////////////////////////////////////////////////////////////////////////
// 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_CMDCONFIG
#define INCLUDED_CMDCONFIG
#define L10N // Localization complete.
#include <string>
#include <Command.h>
class CmdConfig : public Command
{
public:
CmdConfig ();
int execute (const std::string&, std::string&);
};
class CmdCompletionConfig : public Command
{
public:
CmdCompletionConfig ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -162,10 +162,6 @@ int CmdHelp::execute (const std::string&, std::string& output)
row = view.addRow (); row = view.addRow ();
view.set (row, 1, "task pull URL"); view.set (row, 1, "task pull URL");
view.set (row, 2, "Overwrites the local *.data files with those found at the URL."); view.set (row, 2, "Overwrites the local *.data files with those found at the URL.");
row = view.addRow ();
view.set (row, 1, "task config [name [value | '']]");
view.set (row, 2, "Add, modify and remove settings in the task configuration.");
*/ */
output = "\n" output = "\n"

View file

@ -34,6 +34,7 @@
#include <CmdBurndown.h> #include <CmdBurndown.h>
#include <CmdColor.h> #include <CmdColor.h>
#include <CmdCommands.h> #include <CmdCommands.h>
#include <CmdConfig.h>
#include <CmdCount.h> #include <CmdCount.h>
#include <CmdCustom.h> #include <CmdCustom.h>
#include <CmdDenotate.h> #include <CmdDenotate.h>
@ -75,10 +76,12 @@ void Command::factory (std::map <std::string, Command*>& all)
c = new CmdBurndownWeekly (); all[c->keyword ()] = c; c = new CmdBurndownWeekly (); all[c->keyword ()] = c;
c = new CmdColor (); all[c->keyword ()] = c; c = new CmdColor (); all[c->keyword ()] = c;
c = new CmdCompletionCommands (); all[c->keyword ()] = c; c = new CmdCompletionCommands (); all[c->keyword ()] = c;
c = new CmdCompletionConfig (); all[c->keyword ()] = c;
c = new CmdCompletionIds (); all[c->keyword ()] = c; c = new CmdCompletionIds (); all[c->keyword ()] = c;
c = new CmdCompletionProjects (); all[c->keyword ()] = c; c = new CmdCompletionProjects (); all[c->keyword ()] = c;
c = new CmdCompletionTags (); all[c->keyword ()] = c; c = new CmdCompletionTags (); all[c->keyword ()] = c;
c = new CmdCompletionVersion (); all[c->keyword ()] = c; c = new CmdCompletionVersion (); all[c->keyword ()] = c;
c = new CmdConfig (); all[c->keyword ()] = c;
c = new CmdCount (); all[c->keyword ()] = c; c = new CmdCount (); all[c->keyword ()] = c;
c = new CmdDenotate (); all[c->keyword ()] = c; c = new CmdDenotate (); all[c->keyword ()] = c;
c = new CmdDiagnostics (); all[c->keyword ()] = c; c = new CmdDiagnostics (); all[c->keyword ()] = c;

View file

@ -52,9 +52,7 @@ bool nag (Task&);
// command.cpp // command.cpp
int handleDone (std::string&); int handleDone (std::string&);
int handleModify (std::string&); int handleModify (std::string&);
int handleCompletionConfig (std::string&);
int handleQuery (std::string&); int handleQuery (std::string&);
int handleConfig (std::string&);
int handleDelete (std::string&); int handleDelete (std::string&);
int handleDuplicate (std::string&); int handleDuplicate (std::string&);
void handleUndo (); void handleUndo ();