From 371ca27da0a8516c9aa0f160df5e71caff3f2455 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 21 Feb 2015 18:04:08 +0100 Subject: [PATCH] CmdContext: Add support for setting context --- src/commands/CmdContext.cpp | 22 ++++++++++++++++++++++ src/commands/CmdContext.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/commands/CmdContext.cpp b/src/commands/CmdContext.cpp index 55866419b..367cc6eff 100644 --- a/src/commands/CmdContext.cpp +++ b/src/commands/CmdContext.cpp @@ -64,6 +64,8 @@ int CmdContext::execute (std::string& output) rc = deleteContext(words, out); else if (subcommand == "list") rc = listContexts(words, out); + else + rc = setContext(words, out); } output = out.str (); @@ -207,6 +209,26 @@ int CmdContext::listContexts (std::vector & words, std::stringstrea return rc; } +//////////////////////////////////////////////////////////////////////////////// +int CmdContext::setContext (std::vector & words, std::stringstream& out) +{ + // task context home + std::string value = words[0]; + std::vector contexts = getContexts (); + + if (std::find (contexts.begin (), contexts.end (), value) == contexts.end()) + throw format ("Context '{1}' not found.", value); + + bool success = CmdConfig::setConfigVariable("context", value, false); + + if (success) + out << "Context '" << value << "' applied." << "\n"; + else + out << "Context '" << value << "' was not applied." << "\n"; + + return 0; +} + //////////////////////////////////////////////////////////////////////////////// CmdCompletionContext::CmdCompletionContext () { diff --git a/src/commands/CmdContext.h b/src/commands/CmdContext.h index d13745416..c1ef7ab8f 100644 --- a/src/commands/CmdContext.h +++ b/src/commands/CmdContext.h @@ -40,6 +40,7 @@ public: int defineContext (std::vector & words, std::stringstream& out); int deleteContext (std::vector & words, std::stringstream& out); int listContexts (std::vector & words, std::stringstream& out); + int setContext (std::vector & words, std::stringstream& out); }; class CmdCompletionContext : public Command