From 6cf7644e02452479b19d2d4a6a0c71bda61ff290 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 30 May 2011 11:18:59 -0400 Subject: [PATCH] Commands - config - Migrated config and _config to CmdConfig. --- src/Cmd.cpp | 6 +- src/Context.cpp | 4 +- src/command.cpp | 135 ------------------------- src/commands/CMakeLists.txt | 1 + src/commands/CmdConfig.cpp | 192 ++++++++++++++++++++++++++++++++++++ src/commands/CmdConfig.h | 49 +++++++++ src/commands/CmdHelp.cpp | 4 - src/commands/Command.cpp | 3 + src/main.h | 2 - 9 files changed, 247 insertions(+), 149 deletions(-) create mode 100644 src/commands/CmdConfig.cpp create mode 100644 src/commands/CmdConfig.h diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 435c3b6fa..a6950d362 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -129,13 +129,11 @@ void Cmd::load () { if (commands.size () == 0) { - commands.push_back ("_config"); commands.push_back ("_query"); commands.push_back ("export.csv"); commands.push_back ("export.ical"); commands.push_back ("export.yaml"); commands.push_back ("calendar"); - commands.push_back ("config"); commands.push_back ("delete"); commands.push_back ("done"); commands.push_back ("duplicate"); @@ -202,13 +200,11 @@ void Cmd::allCommands (std::vector & all) const // Commands that do not directly modify the data files. bool Cmd::isReadOnlyCommand () { - if (command == "_config" || - command == "_query" || + if (command == "_query" || command == "export.csv" || command == "export.ical" || command == "export.yaml" || command == "calendar" || - command == "config" || command == "push" || command == "summary" || command == "timesheet" || diff --git a/src/Context.cpp b/src/Context.cpp index 404f257c4..6e03ddd0f 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -247,8 +247,7 @@ int Context::dispatch (std::string &out) Timer t ("Context::dispatch"); // TODO Chain-of-command pattern dispatch. - if (cmd.command == "config") { rc = handleConfig (out); } - else if (cmd.command == "summary") { rc = handleReportSummary (out); } + if (cmd.command == "summary") { rc = handleReportSummary (out); } else if (cmd.command == "calendar") { rc = handleReportCalendar (out); } else if (cmd.command == "timesheet") { rc = handleReportTimesheet (out); } else if (cmd.command == "done") { rc = handleDone (out); } @@ -263,7 +262,6 @@ int Context::dispatch (std::string &out) handleMerge (out); } else if (cmd.command == "push") { handlePush (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 == "" && sequence.size ()) { rc = handleModify (out); } diff --git a/src/command.cpp b/src/command.cpp index e958d4a36..5088311c4 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -51,21 +51,6 @@ extern Context context; -//////////////////////////////////////////////////////////////////////////////// -int handleCompletionConfig (std::string& outs) -{ - std::vector 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) { @@ -290,126 +275,6 @@ void handlePull (std::string&) "'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 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) { diff --git a/src/commands/CMakeLists.txt b/src/commands/CMakeLists.txt index 7468e575f..e9c8d21de 100644 --- a/src/commands/CMakeLists.txt +++ b/src/commands/CMakeLists.txt @@ -12,6 +12,7 @@ set (commands_SRCS Command.cpp Command.h CmdBurndown.cpp CmdBurndown.h CmdCommands.cpp CmdCommands.h CmdColor.cpp CmdColor.h + CmdConfig.cpp CmdConfig.h CmdCount.cpp CmdCount.h CmdCustom.cpp CmdCustom.h CmdDenotate.cpp CmdDenotate.h diff --git a/src/commands/CmdConfig.cpp b/src/commands/CmdConfig.cpp new file mode 100644 index 000000000..8a4b7ffe6 --- /dev/null +++ b/src/commands/CmdConfig.cpp @@ -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 +#include +#include +#include +#include +#include + +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 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 configs; + context.config.all (configs); + std::sort (configs.begin (), configs.end ()); + + std::vector ::iterator config; + for (config = configs.begin (); config != configs.end (); ++config) + output += *config + "\n"; + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdConfig.h b/src/commands/CmdConfig.h new file mode 100644 index 000000000..75b027bf9 --- /dev/null +++ b/src/commands/CmdConfig.h @@ -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 +#include + +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 +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdHelp.cpp b/src/commands/CmdHelp.cpp index 015f1c1f8..d26851c28 100644 --- a/src/commands/CmdHelp.cpp +++ b/src/commands/CmdHelp.cpp @@ -162,10 +162,6 @@ int CmdHelp::execute (const std::string&, std::string& output) row = view.addRow (); view.set (row, 1, "task pull 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" diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 70de8d610..d525d6757 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -75,10 +76,12 @@ void Command::factory (std::map & all) c = new CmdBurndownWeekly (); all[c->keyword ()] = c; c = new CmdColor (); 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 CmdCompletionProjects (); all[c->keyword ()] = c; c = new CmdCompletionTags (); 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 CmdDenotate (); all[c->keyword ()] = c; c = new CmdDiagnostics (); all[c->keyword ()] = c; diff --git a/src/main.h b/src/main.h index bff29d4e6..822ee5314 100644 --- a/src/main.h +++ b/src/main.h @@ -52,9 +52,7 @@ bool nag (Task&); // command.cpp int handleDone (std::string&); int handleModify (std::string&); -int handleCompletionConfig (std::string&); int handleQuery (std::string&); -int handleConfig (std::string&); int handleDelete (std::string&); int handleDuplicate (std::string&); void handleUndo ();