From e91063426a4ccb72c1764797f22b2d655f320366 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 21 Feb 2015 12:17:10 +0100 Subject: [PATCH] CmdContext: Add initial implementation of the context define subcommand --- src/commands/CmdContext.cpp | 57 +++++++++++++++++++++++++++++++++++++ src/commands/CmdContext.h | 2 ++ 2 files changed, 59 insertions(+) diff --git a/src/commands/CmdContext.cpp b/src/commands/CmdContext.cpp index dc0fe5878..95c4bcfa9 100644 --- a/src/commands/CmdContext.cpp +++ b/src/commands/CmdContext.cpp @@ -29,6 +29,7 @@ #include #include #include +#include extern Context context; @@ -51,9 +52,65 @@ int CmdContext::execute (std::string& output) // Get the non-attribute, non-fancy command line arguments. std::vector words = context.cli.getWords (); + if (words.size () > 0) + { + std::string subcommand = words[0]; + if (subcommand == "define") + rc = defineContext(words, out); + } + + output = out.str (); return rc; } +//////////////////////////////////////////////////////////////////////////////// +// Joins all the words in the specified interval & words, unsigned int from, unsigned int to /* = 0 */) +{ + std::string value = ""; + + if (to == 0) + to = words.size(); + + for (unsigned int i = from; i < to; ++i) + { + if (i > from) + value += " "; + + value += words[i]; + } + + return value; +} + +//////////////////////////////////////////////////////////////////////////////// +int CmdContext::defineContext (std::vector & words, std::stringstream& out) +{ + // task context define home project:Home + if (words.size () > 2) + { + std::string name = "context." + words[1]; + std::string value = joinWords(words, 2); + // TODO: Check if the value is a proper filter + + bool confirmation = context.config.getBoolean ("confirmation"); + bool success = CmdConfig::setConfigVariable(name, value, confirmation); + + if (success) + out << "Context '" << words[1] << "' successfully defined." << "\n"; + else + out << "Context '" << words[1] << "' was not defined." << "\n"; + } + else + throw "You have to specify both context name and definition."; + + return 0; +} + //////////////////////////////////////////////////////////////////////////////// CmdCompletionContext::CmdCompletionContext () { diff --git a/src/commands/CmdContext.h b/src/commands/CmdContext.h index 93c61a65c..63f1b2847 100644 --- a/src/commands/CmdContext.h +++ b/src/commands/CmdContext.h @@ -35,6 +35,8 @@ class CmdContext : public Command public: CmdContext (); int execute (std::string&); + std::string joinWords (std::vector & words, unsigned int from, unsigned int to = 0); + int defineContext (std::vector & words, std::stringstream& out); }; class CmdCompletionContext : public Command