Enhancement - Aliases

- Moved alias mapping to Context.
- Added Context::canonicalize to resolve aliases.
- Added Context::loadAliases to reload on config file change.
- Removed old alias processing from Cmd.
- Doesn't work yet, but the data is loaded.
This commit is contained in:
Paul Beckingham 2009-06-23 17:38:58 -04:00
parent b6bc72c449
commit 78afa4e110
4 changed files with 44 additions and 28 deletions

View file

@ -165,30 +165,6 @@ void Cmd::load ()
} }
} }
} }
/*
// Now load the aliases.
foreach (i, all)
{
if (i->substr (0, 6) == "alias.")
{
std::string name = i->substr (6, std::string::npos);
std::string alias = context.config.get (name);
// Make sure a custom report does not clash with a built-in
// command.
if (std::find (commands.begin (), commands.end (), report) != commands.end ())
throw std::string ("Alias '") + name +
"' conflicts with built-in task command.";
if (std::find (customReports.begin (), customReports.end (), report) != customReports.end ())
throw std::string ("Alias '") + name +
"' conflicts with custom report.";
aliases[name] = alias;
}
}
*/
} }
} }

View file

@ -27,10 +27,8 @@
#ifndef INCLUDED_CMD #ifndef INCLUDED_CMD
#define INCLUDED_CMD #define INCLUDED_CMD
//#include <map>
#include <vector> #include <vector>
#include <string> #include <string>
#include "Cmd.h"
class Cmd class Cmd
{ {
@ -59,7 +57,6 @@ private:
private: private:
std::vector <std::string> commands; std::vector <std::string> commands;
std::vector <std::string> customReports; std::vector <std::string> customReports;
// std::map <std::string, std::string> aliases;
}; };
#endif #endif

View file

@ -93,6 +93,7 @@ void Context::initialize ()
// Load the configuration file from the home directory. If the file cannot // Load the configuration file from the home directory. If the file cannot
// be found, offer to create a sample one. // be found, offer to create a sample one.
loadCorrectConfigFile (); loadCorrectConfigFile ();
loadAliases ();
// When redirecting output to a file, do not use color, curses. // When redirecting output to a file, do not use color, curses.
if (!isatty (fileno (stdout))) if (!isatty (fileno (stdout)))
@ -289,6 +290,24 @@ void Context::shadow ()
} }
} }
////////////////////////////////////////////////////////////////////////////////
// Only allows aliases 10 deep.
std::string Context::canonicalize (const std::string& input) const
{
std::string canonical = input;
// Follow the chain.
int i = 10; // Safety valve.
std::map <std::string, std::string>::const_iterator found;
while ((found = aliases.find (canonical)) != aliases.end () && i-- > 0)
canonical = found->second;
if (i < 1)
return input;
return canonical;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Context::loadCorrectConfigFile () void Context::loadCorrectConfigFile ()
{ {
@ -352,6 +371,26 @@ void Context::loadCorrectConfigFile ()
args = filtered; args = filtered;
} }
////////////////////////////////////////////////////////////////////////////////
void Context::loadAliases ()
{
aliases.clear ();
std::vector <std::string> vars;
config.all (vars);
foreach (var, vars)
{
if (var->substr (0, 6) == "alias.")
{
std::string alias = var->substr (6, std::string::npos);
std::string canonical = config.get (*var);
aliases[alias] = canonical;
debug (std::string ("Alias ") + alias + " -> " + canonical);
}
}
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Context::parse () void Context::parse ()
{ {

View file

@ -65,8 +65,11 @@ public:
void parse (std::vector <std::string>&, Cmd&, Task&, Sequence&, Subst&, Filter&); void parse (std::vector <std::string>&, Cmd&, Task&, Sequence&, Subst&, Filter&);
void clear (); void clear ();
std::string canonicalize (const std::string&) const;
private: private:
void loadCorrectConfigFile (); void loadCorrectConfigFile ();
void loadAliases ();
void autoFilter (Task&, Filter&); void autoFilter (Task&, Filter&);
public: public:
@ -81,7 +84,8 @@ public:
std::string program; std::string program;
std::vector <std::string> args; std::vector <std::string> args;
Cmd cmd; Cmd cmd;
std::vector <std::string> tagAdditions; // TODO This is redundant, remove. std::map <std::string, std::string> aliases;
std::vector <std::string> tagAdditions;
std::vector <std::string> tagRemovals; std::vector <std::string> tagRemovals;
private: private: