Merge pull request #951 from Daniferrito/aliases

Recursive and multi-command alias
This commit is contained in:
hydroflame
2021-05-15 11:06:06 -04:00
committed by GitHub
2 changed files with 29 additions and 20 deletions

View File

@ -51,14 +51,14 @@ function addAlias(name: string, value: string): void {
if (name in GlobalAliases) { if (name in GlobalAliases) {
delete GlobalAliases[name]; delete GlobalAliases[name];
} }
Aliases[name] = value; Aliases[name] = value.trim();
} }
function addGlobalAlias(name: string, value: string): void { function addGlobalAlias(name: string, value: string): void {
if (name in Aliases){ if (name in Aliases){
delete Aliases[name]; delete Aliases[name];
} }
GlobalAliases[name] = value; GlobalAliases[name] = value.trim();
} }
function getAlias(name: string): string | null { function getAlias(name: string): string | null {
@ -97,22 +97,29 @@ export function removeAlias(name: string): boolean {
export function substituteAliases(origCommand: string): string { export function substituteAliases(origCommand: string): string {
const commandArray = origCommand.split(" "); const commandArray = origCommand.split(" ");
if (commandArray.length > 0){ if (commandArray.length > 0){
// For the unalias command, dont substite // For the alias and unalias commands, dont substite
if (commandArray[0] === "unalias") { return commandArray.join(" "); } if (commandArray[0] === "unalias" || commandArray[0] === "alias") { return commandArray.join(" "); }
const alias = getAlias(commandArray[0]); let somethingSubstituted = true;
if (alias != null) { let depth = 0;
commandArray[0] = alias;
} else { while(somethingSubstituted && depth < 10){
const alias = getGlobalAlias(commandArray[0]); depth++;
somethingSubstituted = false
const alias = getAlias(commandArray[0])?.split(" ");
if (alias != null) { if (alias != null) {
commandArray[0] = alias; somethingSubstituted = true
commandArray.splice(0, 1, ...alias);
//commandArray[0] = alias;
} }
} for (let i = 0; i < commandArray.length; ++i) {
for (let i = 0; i < commandArray.length; ++i) { const alias = getGlobalAlias(commandArray[i])?.split(" ");
const alias = getGlobalAlias(commandArray[i]); if (alias != null) {
if (alias != null) { somethingSubstituted = true
commandArray[i] = alias; commandArray.splice(i, 1, ...alias);
i += alias.length - 1;
//commandArray[i] = alias;
}
} }
} }
} }

View File

@ -625,7 +625,12 @@ let Terminal = {
Terminal.commandHistoryIndex = Terminal.commandHistory.length; Terminal.commandHistoryIndex = Terminal.commandHistory.length;
// Split commands and execute sequentially // Split commands and execute sequentially
commands = commands.split(";"); commands = commands
.match(/(?:'[^']*'|"[^"]*"|[^;"])*/g)
.map(substituteAliases)
.map(c => c.match(/(?:'[^']*'|"[^"]*"|[^;"])*/g))
.flat();
console.log(commands);
for (let i = 0; i < commands.length; i++) { for (let i = 0; i < commands.length; i++) {
if(commands[i].match(/^\s*$/)) { continue; } // Don't run commands that only have whitespace if(commands[i].match(/^\s*$/)) { continue; } // Don't run commands that only have whitespace
Terminal.executeCommand(commands[i].trim()); Terminal.executeCommand(commands[i].trim());
@ -727,9 +732,6 @@ let Terminal = {
return; return;
} }
// Process any aliases
command = substituteAliases(command);
// Allow usage of ./ // Allow usage of ./
if (command.startsWith("./")) { if (command.startsWith("./")) {
command = "run " + command.slice(2); command = "run " + command.slice(2);
@ -873,7 +875,7 @@ let Terminal = {
if (commandArray.length === 3) { if (commandArray.length === 3) {
if (commandArray[1] === "-g") { if (commandArray[1] === "-g") {
if (parseAliasDeclaration(commandArray[2], true)) { if (parseAliasDeclaration(commandArray[2], true)) {
post(`Set global alias ${commandArray[1]}`); post(`Set global alias ${commandArray[2]}`);
return; return;
} }
} }