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
commit 60a229030b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 20 deletions

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

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