Streamline %cmd_map

%cmd_map was previously a hash containing:

{
	real_function => alias_function,
	...
}

Now the structure of the hash is:

{
	alias_or_real_func => [ real_func/if_any, autocompleter_func ],
	...
}

This allows for a more streamlined definition of commands and aliases,
and makes it possible to keep all data in %cmd_map instead of having
additional special cases.

FossilOrigin-Name: ea3c724d1676d6f2ddfddfce22c3d32135ab20d26e81f10c39c4c10bc902b5e9
This commit is contained in:
docelic@crystallabs.io 2019-04-28 21:42:26 +00:00
parent e522c8d4f7
commit c43244840a

120
shelldap
View file

@ -1195,7 +1195,7 @@ sub debug
### Autocomplete values: Returns cached children entries.
###
sub autocomplete_cwd
sub comp_cwd
{
my $self = shift;
return @{ $self->{'cwd_entries'} };
@ -1231,52 +1231,6 @@ sub comp_inspect
}
### Inject various autocomplete and alias routines into the symbol table.
###
{
no warnings;
no strict 'refs';
# command, alias
my %cmd_map = (
whoami => 'id',
list => 'ls',
grep => 'search',
edit => 'vi',
delete => 'rm',
copy => 'cp',
cat => 'read',
move => 'mv',
less => undef,
cd => undef,
passwd => undef
);
# setup autocompletes
foreach ( %cmd_map ) {
next unless $_;
my $sub = "comp_$_";
*$sub = \&autocomplete_cwd;
}
*comp_touch = \&comp_create;
*comp_export = \&comp_setenv;
# setup alias subs
#
# Term::Shell has an alias_* feature, but
# it seems to work about 90% of the time.
# that last 10% is something of a mystery.
#
$cmd_map{'create'} = 'touch';
foreach my $cmd ( keys %cmd_map ) {
next unless defined $cmd_map{$cmd};
my $alias_sub = 'run_' . $cmd_map{$cmd};
my $real_sub = 'run_' . $cmd;
*$alias_sub = \&$real_sub;
}
}
### Given an $arrayref, remove LDIF continuation wrapping in place,
### effectively making each entry a single line for LCS comparisons.
###
@ -1409,6 +1363,44 @@ sub diff {
### S H E L L M E T H O D S
########################################################################
# alias_or_command => [ real_command_name, completion_function ]
#
# It is not necessary to list all real commands here, but you can/should
# do so to assign autocompleter function to them, and/or to define aliases.
my %cmd_map = (
#'whoami' => [ undef ],
#'pwd' => [ undef ],
'list' => [ undef, 'comp_cwd' ],
'grep' => [ undef, 'comp_cwd' ],
'edit' => [ undef, 'comp_cwd' ],
'delete' => [ undef, 'comp_cwd' ],
'copy' => [ undef, 'comp_cwd' ],
'cat' => [ undef, 'comp_cwd' ],
'move' => [ undef, 'comp_cwd' ],
'less' => [ undef, 'comp_cwd' ],
'cd' => [ undef, 'comp_cwd' ],
'create' => [ undef, 'comp_create' ],
'setenv' => [ undef, 'comp_setenv' ],
#'passwd' => [ undef ],
##'clear' => [ undef ],
#'env' => [ undef ],
#'help' => [ undef ],
#'mkdir' => [ undef ],
#'inspect' => [ undef ],
'id' => [ 'whoami' ],
'ls' => [ 'list', 'comp_cwd' ],
'search' => [ 'grep', 'comp_cwd' ],
'vi' => [ 'edit', 'comp_cwd' ],
'rm' => [ 'delete', 'comp_cwd' ],
'cp' => [ 'copy', 'comp_cwd' ],
'read' => [ 'read', 'comp_cwd' ],
'mv' => [ 'move', 'comp_cwd' ],
'touch' => [ 'create', 'comp_create' ],
'export' => [ 'setenv', 'comp_setenv' ],
);
### Don't die on a newline, just no-op.
###
sub run_ { return; }
@ -1927,9 +1919,14 @@ sub run_grep
sub run_help
{
my $self = shift;
my $command = shift;
my $section = 'SHELL COMMANDS';
$section .= "/$command" if $command;
my $command = shift;
if( $command ) {
# If it is an alias, resolve to real name:
$command = $cmd_map{$command}[0] if $cmd_map{$command}[0];
$section .= "/$command"
}
return Pod::Usage::pod2usage(
-exitval => 'NOEXIT',
@ -2338,6 +2335,31 @@ sub run_inspect
}
### Inject various autocomplete and alias routines into the symbol table.
###
# setup alias subs
#
# Term::Shell has an alias_* feature, but
# it seems to work about 90% of the time.
# that last 10% is something of a mystery.
#
{ no strict 'refs';
while(my($cmd, $data) = each %cmd_map ) {
if( $$data[0]) {
my $alias_sub = 'run_' . $cmd;
my $real_sub = 'run_' . $$data[0];
*$alias_sub = \&$real_sub;
}
if( $$data[1]) {
my $comp_sub = "comp_$cmd";
*$comp_sub = \&{$$data[1]}
}
}
}
### Recursively walk an objectClass hierarchy, returning an array
### of inheritence.
###