From 78afa4e110b70ec49a7685504b2f69dfe299ae29 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 23 Jun 2009 17:38:58 -0400 Subject: [PATCH] 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. --- src/Cmd.cpp | 24 ------------------------ src/Cmd.h | 3 --- src/Context.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/Context.h | 6 +++++- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 554b2b5e8..9a3d2b6f7 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -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; - } - } -*/ } } diff --git a/src/Cmd.h b/src/Cmd.h index 15ff7aaa8..314a6802d 100644 --- a/src/Cmd.h +++ b/src/Cmd.h @@ -27,10 +27,8 @@ #ifndef INCLUDED_CMD #define INCLUDED_CMD -//#include #include #include -#include "Cmd.h" class Cmd { @@ -59,7 +57,6 @@ private: private: std::vector commands; std::vector customReports; -// std::map aliases; }; #endif diff --git a/src/Context.cpp b/src/Context.cpp index 54e4a850e..6b4c043c6 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -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 ::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 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 () { diff --git a/src/Context.h b/src/Context.h index da55ec719..23003e2db 100644 --- a/src/Context.h +++ b/src/Context.h @@ -65,8 +65,11 @@ public: void parse (std::vector &, Cmd&, Task&, Sequence&, Subst&, Filter&); void clear (); + std::string canonicalize (const std::string&) const; + private: void loadCorrectConfigFile (); + void loadAliases (); void autoFilter (Task&, Filter&); public: @@ -81,7 +84,8 @@ public: std::string program; std::vector args; Cmd cmd; - std::vector tagAdditions; // TODO This is redundant, remove. + std::map aliases; + std::vector tagAdditions; std::vector tagRemovals; private: