From edb54a51b3fbbcaca96039844b0b2cfcc097fe8f Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 21 Feb 2015 13:38:53 +0100 Subject: [PATCH] CmdContext: Add list subcommand --- src/commands/CmdContext.cpp | 66 +++++++++++++++++++++++++++++++++++++ src/commands/CmdContext.h | 2 ++ 2 files changed, 68 insertions(+) diff --git a/src/commands/CmdContext.cpp b/src/commands/CmdContext.cpp index 0ac4dc09f..9813f0196 100644 --- a/src/commands/CmdContext.cpp +++ b/src/commands/CmdContext.cpp @@ -27,7 +27,9 @@ #include #include #include +#include #include +#include #include #include @@ -60,6 +62,8 @@ int CmdContext::execute (std::string& output) rc = defineContext(words, out); else if (subcommand == "delete") rc = deleteContext(words, out); + else if (subcommand == "list") + rc = listContexts(words, out); } output = out.str (); @@ -90,6 +94,25 @@ std::string CmdContext::joinWords (std::vector & words, unsigned in return value; } +//////////////////////////////////////////////////////////////////////////////// +// Returns all user defined contexts. +// +std::vector CmdContext::getContexts () +{ + std::vector contexts; + + 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; +} + //////////////////////////////////////////////////////////////////////////////// int CmdContext::defineContext (std::vector & words, std::stringstream& out) { @@ -141,6 +164,49 @@ int CmdContext::deleteContext (std::vector & words, std::stringstre return 0; } +//////////////////////////////////////////////////////////////////////////////// +int CmdContext::listContexts (std::vector & words, std::stringstream& out) +{ + int rc = 0; + std::vector contexts = getContexts(); + + if (contexts.size ()) + { + 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")); + view.add (Column::factory ("string", "Definition")); + + Color label (context.config.get ("color.label")); + view.colorHeader (label); + + std::vector ::iterator userContext; + for (userContext = contexts.begin (); userContext != contexts.end (); ++userContext) + { + std::string definition = context.config.get ("context." + *userContext); + + int row = view.addRow (); + view.set (row, 0, *userContext); + view.set (row, 1, definition); + } + + out << optionalBlankLine () + << view.render () + << optionalBlankLine (); + } + else + { + out << "No contexts defined." << "\n"; + rc = 1; + } + + return rc; +} + //////////////////////////////////////////////////////////////////////////////// CmdCompletionContext::CmdCompletionContext () { diff --git a/src/commands/CmdContext.h b/src/commands/CmdContext.h index 9a9c01d6a..41ff887cf 100644 --- a/src/commands/CmdContext.h +++ b/src/commands/CmdContext.h @@ -36,8 +36,10 @@ public: CmdContext (); int execute (std::string&); std::string joinWords (std::vector & words, unsigned int from, unsigned int to = 0); + std::vector getContexts (); int defineContext (std::vector & words, std::stringstream& out); int deleteContext (std::vector & words, std::stringstream& out); + int listContexts (std::vector & words, std::stringstream& out); }; class CmdCompletionContext : public Command