Merge pull request #358 from vladimir-kotikov/alias-rework

Rework `alias` command to not to use external tools
This commit is contained in:
Martin Kemp 2015-01-08 22:08:11 +00:00
commit 6c69536295
4 changed files with 28 additions and 70 deletions

View File

@ -1,6 +1,12 @@
@echo off
set ALIASES=%CMDER_ROOT%\config\aliases
if ["%*"] == [""] echo Use /? for help & echo. & goto :p_show
if ["%1"] == ["/?"] goto:p_help
if ["%1"] == ["/reload"] goto:p_reload
:: /d flag for delete existing alias
if ["%1"] == ["/d"] goto:p_del %*
if ["%2"] == [""] echo Insufficient parameters. & goto:p_help
::validate alias
setlocal
@ -14,21 +20,36 @@ if not ["%_temp%"] == ["%_temp2%"] (
goto:eof
)
echo %* >> "%CMDER_ROOT%\config\aliases"
doskey /macrofile="%CMDER_ROOT%\config\aliases"
perl "%CMDER_ROOT%\scripts\clean_aliases.pl"
echo Alias created
:: replace already defined alias
findstr /b /v /i "%_temp%=" "%ALIASES%" >> "%ALIASES%.tmp"
echo %* >> "%ALIASES%.tmp" && type "%ALIASES%.tmp" > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
doskey /macrofile="%ALIASES%"
endlocal
goto:eof
:p_del
findstr /b /v /i "%2=" "%ALIASES%" >> "%ALIASES%.tmp"
type "%ALIASES%".tmp > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
doskey /macrofile=%ALIASES%
goto:eof
:p_reload
doskey /macrofile="%CMDER_ROOT%\config\aliases"
doskey /macrofile="%ALIASES%"
echo Aliases reloaded
goto:eof
:p_show
type "%ALIASES%" || echo No aliases found at "%ALIASES%"
goto :eof
:p_help
echo.Usage:
echo. alias name=full command
echo. alias [/reload] [/d] [name=full command]
echo. /reload Reload the aliases file
echo. /d Delete an alias (must be followed by the alias name)
echo.
echo. If alias is called with any parameters, it will display the list of existing aliases.
echo. In the command, you can use the following notations:
echo. $* allows the alias to assume all the parameters of the supplied command.
echo. $1-$9 Allows you to seperate parameter by number, much like %%1 in batch.
echo. $T is the command seperator, allowing you to string several commands together into one alias.

View File

@ -1,28 +0,0 @@
@echo off
if ["%1"] == ["/?"] goto:p_help
if ["%1"] == [""] echo Insufficient parameters. & goto:p_help
setlocal
:: Check if alias exists
doskey /macros | findstr /b %1= >NUL || goto :p_not_found
:: Remove alias from current shell
doskey %1=
:: Remove alias from aliases file
copy /y "%CMDER_ROOT%\config\aliases" "%TEMP%\aliases.prev" >NUL
type "%TEMP%\aliases.prev" | findstr /b /v %1= > "%CMDER_ROOT%\config\aliases"
echo Alias removed
endlocal
goto:eof
:p_not_found
echo Alias not defined.
goto:eof
:p_help
echo.Usage:
echo. unalias name
echo. For more information, read DOSKEY/?

View File

@ -4,3 +4,4 @@ ls=ls --color $*
pwd=cd
clear=cls
history=cat %CMDER_ROOT%\config\.history
unalias=alias /d $1

View File

@ -1,36 +0,0 @@
# Cmder adds aliases to its aliases file without caring for duplicates.
# This can result in the aliases file becoming bloated. This script cleans
#the aliases file.
use Env;
my %aliases;
my $alias_file = $CMDER_ROOT . "/config/aliases";
# First step
# Read the aliases file line by line, and put every entry in
# a dictionary. The newer aliases being the last, the new will
# always be kept over the old.
open (my $file_handle, '<', $alias_file) or die "cannot open '$alias_file' $!";
while(my $line = <$file_handle>)
{
if ($line =~ /([^=\s<>]+)=(.*)/)
{
$aliases{ $1 } = $2;
}
else
{
print "Invalid alias: $line"
}
}
close($file_handle);
# Second step
# Write back the aliases. Sort them to make the file look nice.
open(my $file_handle, '>', $alias_file) or die "cannot open '$alias_file' $!";
foreach my $key (sort keys %aliases)
{
print $file_handle "$key=$aliases{ $key }\n";
}
close($file_handle);