mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Commands - config
- Migrated config and _config to CmdConfig.
This commit is contained in:
parent
0f5a4434ff
commit
6cf7644e02
9 changed files with 247 additions and 149 deletions
|
@ -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
|
||||
|
|
192
src/commands/CmdConfig.cpp
Normal file
192
src/commands/CmdConfig.cpp
Normal 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
49
src/commands/CmdConfig.h
Normal 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
|
||||
////////////////////////////////////////////////////////////////////////////////
|
|
@ -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"
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <CmdBurndown.h>
|
||||
#include <CmdColor.h>
|
||||
#include <CmdCommands.h>
|
||||
#include <CmdConfig.h>
|
||||
#include <CmdCount.h>
|
||||
#include <CmdCustom.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 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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue