- Implemented simple word substitution aliases. This is not the ultimate form
  which will involve the ability to insert arbitrary constructs.  Later.
This commit is contained in:
Paul Beckingham 2014-06-03 00:10:15 -04:00
parent a470e50ee6
commit 1cd09bc87b
7 changed files with 42 additions and 41 deletions

View file

@ -35,7 +35,7 @@
extern Context context; extern Context context;
// Alias expansion limit. Any more indicates some kind of error. // Alias expansion limit. Any more indicates some kind of error.
const int safetyValveDefault = 10; const int safetyValveDefault = 10;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -64,6 +64,9 @@ void Alias::load ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// An alias must be a distinct word on the command line. // An alias must be a distinct word on the command line.
//
// TODO This is straight word substitution. What this really needs is node
// insertion, for more advanced aliases.
void Alias::resolve (Tree* tree) void Alias::resolve (Tree* tree)
{ {
bool something; bool something;
@ -85,20 +88,27 @@ void Alias::resolve (Tree* tree)
if (! (*i)->hasTag ("?")) if (! (*i)->hasTag ("?"))
continue; continue;
std::string raw = (*i)->attribute ("raw"); std::map <std::string, std::string>::iterator a;
std::map <std::string, std::string>::iterator match = context.aliases.find (raw); if ((*i)->_branches.size ())
if (match != context.aliases.end ())
{ {
something = true; std::vector <Tree*>::iterator b;
for (b = (*i)->_branches.begin (); b != (*i)->_branches.end (); ++b)
std::vector <std::string> words;
Lexer::word_split (words, context.aliases[raw]);
std::vector <std::string>::iterator word;
for (word = words.begin (); word != words.end (); ++word)
{ {
// TODO Insert branch (words) in place of (*i). a = _aliases.find ((*b)->attribute ("raw"));
std::cout << "# alias word '" << *word << "'\n"; if (a != _aliases.end ())
{
(*b)->attribute ("raw", a->second);
something = true;
}
}
}
else
{
a = _aliases.find ((*i)->attribute ("raw"));
if (a != _aliases.end ())
{
(*i)->attribute ("raw", a->second);
something = true;
} }
} }
} }

View file

@ -39,7 +39,7 @@ public:
void load (); void load ();
void resolve (Tree*); void resolve (Tree*);
private: public:
std::map <std::string, std::string> _aliases; std::map <std::string, std::string> _aliases;
}; };

View file

@ -145,9 +145,8 @@ int Context::initialize (int argc, const char** argv)
createDefaultConfig (); createDefaultConfig ();
// Handle Aliases. // Handle Aliases.
loadAliases (); alias.load ();
aliases2.load (); alias.resolve (parser.tree ());
aliases2.resolve (parser.tree ());
// Initialize the color rules, if necessary. // Initialize the color rules, if necessary.
if (color ()) if (color ())
@ -669,20 +668,6 @@ void Context::createDefaultConfig ()
config.createDefaultData (data_dir); config.createDefaultData (data_dir);
} }
////////////////////////////////////////////////////////////////////////////////
void Context::loadAliases ()
{
aliases.clear ();
std::vector <std::string> vars;
config.all (vars);
std::vector <std::string>::iterator var;
for (var = vars.begin (); var != vars.end (); ++var)
if (var->substr (0, 6) == "alias.")
aliases[var->substr (6)] = config.get (*var);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Context::decomposeSortField ( void Context::decomposeSortField (
const std::string& field, const std::string& field,

View file

@ -77,7 +77,6 @@ private:
void staticInitialization (); void staticInitialization ();
void assumeLocations (); void assumeLocations ();
void createDefaultConfig (); void createDefaultConfig ();
void loadAliases ();
void updateXtermTitle (); void updateXtermTitle ();
void updateVerbosity (); void updateVerbosity ();
@ -90,8 +89,7 @@ public:
Config config; Config config;
TDB2 tdb2; TDB2 tdb2;
std::map <std::string, std::string> aliases; Alias alias;
Alias aliases2;
Hooks hooks; Hooks hooks;
DOM dom; DOM dom;

View file

@ -52,7 +52,7 @@ int CmdCompletionAliases::execute (std::string& output)
std::vector <std::string> aliases; std::vector <std::string> aliases;
std::map <std::string, std::string>::iterator it; std::map <std::string, std::string>::iterator it;
for (it = context.aliases.begin (); it != context.aliases.end (); ++it) for (it = context.alias._aliases.begin (); it != context.alias._aliases.end (); ++it)
aliases.push_back (it->first); aliases.push_back (it->first);
// Sort alphabetically. // Sort alphabetically.

View file

@ -97,8 +97,8 @@ int CmdHelp::execute (std::string& output)
view.set (row, 1, " "); view.set (row, 1, " ");
std::map <std::string, std::string>::iterator alias; std::map <std::string, std::string>::iterator alias;
for (alias = context.aliases.begin (); for (alias = context.alias._aliases.begin ();
alias != context.aliases.end (); alias != context.alias._aliases.end ();
++alias) ++alias)
{ {
row = view.addRow (); row = view.addRow ();

View file

@ -27,7 +27,7 @@
use strict; use strict;
use warnings; use warnings;
use Test::More tests => 3; use Test::More tests => 5;
# Ensure environment has no influence. # Ensure environment has no influence.
delete $ENV{'TASKDATA'}; delete $ENV{'TASKDATA'};
@ -38,22 +38,30 @@ if (open my $fh, '>', 'alias.rc')
{ {
print $fh "data.location=.\n", print $fh "data.location=.\n",
"alias.foo=_projects\n", "alias.foo=_projects\n",
"alias.bar=foo\n"; "alias.bar=foo\n",
"alias.baz=bar\n",
"alias.qux=baz\n";
close $fh; close $fh;
} }
# Add a task with certain project, then access that task via aliases. # Add a task with a certain project, then access that task via aliases.
qx{../src/task rc:alias.rc add project:ALIAS foo 2>&1}; qx{../src/task rc:alias.rc add project:ALIAS foo 2>&1};
my $output = qx{../src/task rc:alias.rc _projects 2>&1}; my $output = qx{../src/task rc:alias.rc _projects 2>&1};
like ($output, qr/ALIAS/, 'task _projects -> ALIAS'); like ($output, qr/ALIAS/, 'task _projects -> ALIAS');
$output = qx{../src/task rc:alias.rc foo 2>&1}; $output = qx{../src/task rc:alias.rc qux 2>&1};
like ($output, qr/ALIAS/, 'task foo -> _projects -> ALIAS'); like ($output, qr/ALIAS/, 'task foo -> _projects -> ALIAS');
$output = qx{../src/task rc:alias.rc bar 2>&1}; $output = qx{../src/task rc:alias.rc bar 2>&1};
like ($output, qr/ALIAS/, 'task bar -> foo -> _projects -> ALIAS'); like ($output, qr/ALIAS/, 'task bar -> foo -> _projects -> ALIAS');
$output = qx{../src/task rc:alias.rc baz 2>&1};
like ($output, qr/ALIAS/, 'task baz -> bar -> foo -> _projects -> ALIAS');
$output = qx{../src/task rc:alias.rc qux 2>&1};
like ($output, qr/ALIAS/, 'task qux -> baz -> bar -> foo -> _projects -> ALIAS');
# Cleanup. # Cleanup.
unlink qw(pending.data completed.data undo.data backlog.data alias.rc); unlink qw(pending.data completed.data undo.data backlog.data alias.rc);
exit 0; exit 0;