CmdContext: Add list subcommand

This commit is contained in:
Tomas Babej 2015-02-21 13:38:53 +01:00 committed by Paul Beckingham
parent 636f6bfd96
commit edb54a51b3
2 changed files with 68 additions and 0 deletions

View file

@ -27,7 +27,9 @@
#include <cmake.h>
#include <Context.h>
#include <sstream>
#include <algorithm>
#include <i18n.h>
#include <text.h>
#include <CmdContext.h>
#include <CmdConfig.h>
@ -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 <std::string>& words, unsigned in
return value;
}
////////////////////////////////////////////////////////////////////////////////
// Returns all user defined contexts.
//
std::vector <std::string> CmdContext::getContexts ()
{
std::vector <std::string> 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 <std::string>& words, std::stringstream& out)
{
@ -141,6 +164,49 @@ int CmdContext::deleteContext (std::vector <std::string>& words, std::stringstre
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int CmdContext::listContexts (std::vector <std::string>& words, std::stringstream& out)
{
int rc = 0;
std::vector <std::string> 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 <std::string>::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 ()
{

View file

@ -36,8 +36,10 @@ public:
CmdContext ();
int execute (std::string&);
std::string joinWords (std::vector <std::string>& words, unsigned int from, unsigned int to = 0);
std::vector <std::string> getContexts ();
int defineContext (std::vector <std::string>& words, std::stringstream& out);
int deleteContext (std::vector <std::string>& words, std::stringstream& out);
int listContexts (std::vector <std::string>& words, std::stringstream& out);
};
class CmdCompletionContext : public Command