From ee23a099f2c39d60fefc19fe0a0331adfee41757 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Mon, 23 Feb 2015 07:51:57 +0100 Subject: [PATCH] CmdContext: Minor style and message changes --- src/commands/CmdConfig.cpp | 4 +- src/commands/CmdContext.cpp | 118 +++++++++++++++++++++++++++--------- test/context.t | 12 ++-- 3 files changed, 96 insertions(+), 38 deletions(-) diff --git a/src/commands/CmdConfig.cpp b/src/commands/CmdConfig.cpp index 4dd3e9b3b..52e883d45 100644 --- a/src/commands/CmdConfig.cpp +++ b/src/commands/CmdConfig.cpp @@ -156,13 +156,13 @@ int CmdConfig::execute (std::string& output) if (words.size ()) { bool confirmation = context.config.getBoolean ("confirmation"); - bool change = false; bool found = false; std::string name = words[0]; std::string value = ""; + // Join the remaining words into config variable's value if (words.size () > 1) { for (unsigned int i = 1; i < words.size (); ++i) @@ -199,7 +199,7 @@ int CmdConfig::execute (std::string& output) throw format (STRING_CMD_CONFIG_NO_ENTRY, name); } - // Write .taskrc (or equivalent) + // Show feedback depending on whether .taskrc has been rewritten if (change) { out << format (STRING_CMD_CONFIG_FILE_MOD, diff --git a/src/commands/CmdContext.cpp b/src/commands/CmdContext.cpp index 5378c51d2..0d940e98d 100644 --- a/src/commands/CmdContext.cpp +++ b/src/commands/CmdContext.cpp @@ -39,7 +39,7 @@ extern Context context; CmdContext::CmdContext () { _keyword = "context"; - _usage = "task context [name [value | '']]"; + _usage = "task context [ | subcommand]"; _description = STRING_CMD_CONTEXT_USAGE; _read_only = true; _displays_id = false; @@ -59,17 +59,17 @@ int CmdContext::execute (std::string& output) std::string subcommand = words[0]; if (subcommand == "define") - rc = defineContext(words, out); + rc = defineContext (words, out); else if (subcommand == "delete") - rc = deleteContext(words, out); + rc = deleteContext (words, out); else if (subcommand == "list") - rc = listContexts(words, out); + rc = listContexts (words, out); else if (subcommand == "none") - rc = unsetContext(words, out); + rc = unsetContext (words, out); else if (subcommand == "show") - rc = showContext(words, out); + rc = showContext (words, out); else - rc = setContext(words, out); + rc = setContext (words, out); } output = out.str (); @@ -77,10 +77,10 @@ int CmdContext::execute (std::string& output) } //////////////////////////////////////////////////////////////////////////////// -// Joins all the words in the specified interval & words, unsigned int from, unsigned int to /* = 0 */) { @@ -109,68 +109,98 @@ std::vector CmdContext::getContexts () Config::const_iterator name; for (name = context.config.begin (); name != context.config.end (); ++name) - { if (name->first.substr (0, 8) == "context.") - { contexts.push_back (name->first.substr (8)); - } - } return contexts; } //////////////////////////////////////////////////////////////////////////////// +// Defines a new user-provided context. +// - The context definition is written into .taskrc as a context. variable. +// - Deletion of the context requires confirmation if rc.confirmation=yes. +// +// Returns: 0 if the addition of the config variable was successful, 1 otherwise +// +// Invoked with: task context define +// Example: task context define home project:Home +// int CmdContext::defineContext (std::vector & words, std::stringstream& out) { - // task context define home project:Home + int rc = 0; + 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 + // Set context definition config variable bool confirmation = context.config.getBoolean ("confirmation"); bool success = CmdConfig::setConfigVariable(name, value, confirmation); if (success) - out << "Context '" << words[1] << "' successfully defined." << "\n"; + out << "Context '" << words[1] << "' defined." << "\n"; else + { out << "Context '" << words[1] << "' was not defined." << "\n"; + rc = 1; + } } else - throw "You have to specify both context name and definition."; + throw "Both context name and its definition must be provided."; - return 0; + return rc; } //////////////////////////////////////////////////////////////////////////////// +// Deletes the specified context. +// - If the deleted context is currently active, unset it. +// - Deletion of the context requires confirmation if rc.confirmation=yes. +// +// Returns: 0 if the removal of the config variable was successful, 1 otherwise +// +// Invoked with: task context delete +// Example: task context delete home +// int CmdContext::deleteContext (std::vector & words, std::stringstream& out) { - // task context delete home + int rc = 0; + if (words.size () > 1) { + // Delete the specified context std::string name = "context." + words[1]; bool confirmation = context.config.getBoolean ("confirmation"); - int status = CmdConfig::unsetConfigVariable(name, confirmation); + rc = CmdConfig::unsetConfigVariable(name, confirmation); + // If the currently set context was deleted, unset it std::string currentContext = context.config.get ("context"); if (currentContext == words[1]) CmdConfig::unsetConfigVariable("context", false); - if (status == 0) - out << "Context '" << words[1] << "' successfully undefined." << "\n"; + // Output feedback + if (rc == 0) + out << "Context '" << words[1] << "' undefined." << "\n"; else out << "Context '" << words[1] << "' was not undefined." << "\n"; } else - throw "You have to specify context name."; + throw "Context name needs to be specified."; - return 0; + return rc; } //////////////////////////////////////////////////////////////////////////////// +// Render a list of context names and their definitions. +// +// Returns: 0 the resulting list is non-empty, 1 otherwise +// +// Invoked with: task context list +// Example: task context list +// int CmdContext::listContexts (std::vector & words, std::stringstream& out) { int rc = 0; @@ -180,8 +210,6 @@ int CmdContext::listContexts (std::vector & words, std::stringstrea { std::sort (contexts.begin (), contexts.end ()); - // Render a list of UDA name, type, label, allowed values, - // possible default value, and finally the usage count. ViewText view; view.width (context.getWidth ()); view.add (Column::factory ("string", "Name")); @@ -214,29 +242,51 @@ int CmdContext::listContexts (std::vector & words, std::stringstrea } //////////////////////////////////////////////////////////////////////////////// +// Sets the specified context as currently active. +// - If some other context was active, the value of currently active context +// is replaced, not added. +// - Setting of the context does not require confirmation. +// +// Returns: 0 if the setting of the context was successful, 1 otherwise +// +// Invoked with: task context +// Example: task context home +// int CmdContext::setContext (std::vector & words, std::stringstream& out) { - // task context home + int rc = 0; std::string value = words[0]; std::vector contexts = getContexts (); + // Check that the specified context is defined if (std::find (contexts.begin (), contexts.end (), value) == contexts.end()) throw format ("Context '{1}' not found.", value); + // Set the active context. + // Should always succeed, as we do not require confirmation. bool success = CmdConfig::setConfigVariable("context", value, false); if (success) out << "Context '" << value << "' applied." << "\n"; else + { out << "Context '" << value << "' was not applied." << "\n"; + rc = 1; + } - return 0; + return rc; } //////////////////////////////////////////////////////////////////////////////// +// Shows the currently active context. +// +// Returns: Always returns 0. +// +// Invoked with: task context show +// Example: task context show +// int CmdContext::showContext (std::vector & words, std::stringstream& out) { - // task context show std::string currentContext = context.config.get ("context"); if (currentContext == "") @@ -251,9 +301,17 @@ int CmdContext::showContext (std::vector & words, std::stringstream } //////////////////////////////////////////////////////////////////////////////// +// Unsets the currently active context. +// - Unsetting of the context does not require confirmation. +// +// Returns: 0 if the unsetting of the context was successful, 1 otherwise (also +// returned if no context is currently active) +// +// Invoked with: task context none +// Example: task context none +// int CmdContext::unsetContext (std::vector & words, std::stringstream& out) { - // task context none int rc = 0; int status = CmdConfig::unsetConfigVariable("context", false); diff --git a/test/context.t b/test/context.t index 05a4c9362..a185c77bc 100755 --- a/test/context.t +++ b/test/context.t @@ -48,7 +48,7 @@ class ContextManagementTest(TestCase): output = self.t(('context', 'define', 'work', 'project:Work'))[1] # Assert successful output - self.assertIn("Context 'work' successfully defined.", output) + self.assertIn("Context 'work' defined.", output) # Assert the config contains context definition self.assertIn('context.work=project:Work\n', self.t.taskrc_content) @@ -66,7 +66,7 @@ class ContextManagementTest(TestCase): output = self.t(('context', 'define', 'work', 'project:Work'))[1] # Assert successful output - self.assertIn("Context 'work' successfully defined.", output) + self.assertIn("Context 'work' defined.", output) # Assert the config contains context definition self.assertIn('context.work=project:Work\n', self.t.taskrc_content) @@ -84,7 +84,7 @@ class ContextManagementTest(TestCase): output = self.t(('context', 'define', 'work', '+work'))[1] # Assert successful output - self.assertIn("Context 'work' successfully defined.", output) + self.assertIn("Context 'work' defined.", output) # Assert the config does not contain the old context definition self.assertNotIn('context.work=project:Work\n', self.t.taskrc_content) @@ -105,7 +105,7 @@ class ContextManagementTest(TestCase): output = self.t(('context', 'delete', 'work'))[1] # Assert correct output - self.assertIn("Context 'work' successfully undefined.", output) + self.assertIn("Context 'work' undefined.", output) # Assert that taskrc does not countain context work definition self.assertFalse(any('context.work=' in line for line in self.t.taskrc_content)) @@ -115,7 +115,7 @@ class ContextManagementTest(TestCase): Test deletion of undefined context. """ - output = self.t(('context', 'delete', 'work'))[1] + output = self.t.runError(('context', 'delete', 'work'))[1] # Assert correct output self.assertIn("Context 'work' was not undefined.", output) @@ -133,7 +133,7 @@ class ContextManagementTest(TestCase): output = self.t(('context', 'delete', 'work'))[1] # Assert correct output - self.assertIn("Context 'work' successfully undefined.", output) + self.assertIn("Context 'work' undefined.", output) # Assert that taskrc does not countain context work definition self.assertFalse(any('context.work=' in line for line in self.t.taskrc_content))