mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-27 01:53:48 +01:00
Merge pull request #951 from Daniferrito/aliases
Recursive and multi-command alias
This commit is contained in:
commit
60a229030b
33
src/Alias.ts
33
src/Alias.ts
@ -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]);
|
||||
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;
|
||||
} else {
|
||||
const alias = getGlobalAlias(commandArray[0]);
|
||||
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]);
|
||||
const alias = getGlobalAlias(commandArray[i])?.split(" ");
|
||||
if (alias != null) {
|
||||
commandArray[i] = alias;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user