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

@ -93,6 +93,7 @@ void Context::initialize ()
// Load the configuration file from the home directory. If the file cannot
// be found, offer to create a sample one.
loadCorrectConfigFile ();
loadAliases ();
// When redirecting output to a file, do not use color, curses.
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 ()
{
@ -352,6 +371,26 @@ void Context::loadCorrectConfigFile ()
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 ()
{