2021-09-16 08:52:45 +02:00
|
|
|
import { Terminal } from "./Terminal";
|
2023-05-29 12:54:16 +02:00
|
|
|
import { trimQuotes } from "./utils/helpers/string";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2023-05-05 09:55:59 +02:00
|
|
|
export const Aliases = new Map<string, string>();
|
|
|
|
export const GlobalAliases = new Map<string, string>();
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function loadAliases(saveString: string): void {
|
2023-05-05 09:55:59 +02:00
|
|
|
Aliases.clear();
|
|
|
|
const parsedAliases: unknown = JSON.parse(saveString);
|
|
|
|
if (!parsedAliases || typeof parsedAliases !== "object") return;
|
|
|
|
for (const [name, alias] of Object.entries(parsedAliases)) {
|
|
|
|
if (typeof name === "string" && typeof alias === "string") Aliases.set(name, alias);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function loadGlobalAliases(saveString: string): void {
|
2023-05-05 09:55:59 +02:00
|
|
|
GlobalAliases.clear();
|
|
|
|
const parsedAliases: unknown = JSON.parse(saveString);
|
|
|
|
if (!parsedAliases || typeof parsedAliases !== "object") return;
|
|
|
|
for (const [name, alias] of Object.entries(parsedAliases)) {
|
|
|
|
if (typeof name === "string" && typeof alias === "string") GlobalAliases.set(name, alias);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Prints all aliases to terminal
|
2019-04-05 11:08:41 +02:00
|
|
|
export function printAliases(): void {
|
2023-05-05 09:55:59 +02:00
|
|
|
for (const [name, alias] of Aliases) Terminal.print("alias " + name + "=" + alias);
|
|
|
|
for (const [name, alias] of GlobalAliases) Terminal.print("global alias " + name + "=" + alias);
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Returns true if successful, false otherwise
|
2021-04-29 02:07:26 +02:00
|
|
|
export function parseAliasDeclaration(dec: string, global = false): boolean {
|
2022-07-26 14:08:51 +02:00
|
|
|
const re = /^([\w|!%,@-]+)=(.+)$/;
|
2021-09-05 01:09:30 +02:00
|
|
|
const matches = dec.match(re);
|
2022-07-26 14:08:51 +02:00
|
|
|
if (matches == null || matches.length != 3) {
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-29 12:54:16 +02:00
|
|
|
matches[2] = trimQuotes(matches[2]);
|
2021-12-29 20:04:58 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (global) {
|
2022-07-26 14:08:51 +02:00
|
|
|
addGlobalAlias(matches[1], matches[2]);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
2022-07-26 14:08:51 +02:00
|
|
|
addAlias(matches[1], matches[2]);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
function addAlias(name: string, value: string): void {
|
2023-05-05 09:55:59 +02:00
|
|
|
GlobalAliases.delete(name);
|
|
|
|
Aliases.set(name, value.trim());
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
function addGlobalAlias(name: string, value: string): void {
|
2023-05-05 09:55:59 +02:00
|
|
|
Aliases.delete(name);
|
|
|
|
GlobalAliases.set(name, value.trim());
|
2017-06-30 18:44:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function removeAlias(name: string): boolean {
|
2023-05-05 09:55:59 +02:00
|
|
|
const hadAlias = Aliases.has(name) || GlobalAliases.has(name);
|
|
|
|
Aliases.delete(name);
|
|
|
|
GlobalAliases.delete(name);
|
|
|
|
return hadAlias;
|
2017-06-21 19:12:08 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
/**
|
|
|
|
* Returns the original string with any aliases substituted in.
|
|
|
|
* Aliases are only applied to "whole words", one level deep
|
|
|
|
*/
|
2019-04-05 11:08:41 +02:00
|
|
|
export function substituteAliases(origCommand: string): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
const commandArray = origCommand.split(" ");
|
|
|
|
if (commandArray.length > 0) {
|
2022-10-09 07:25:31 +02:00
|
|
|
// For the alias and unalias commands, don't substitute
|
2021-09-05 01:09:30 +02:00
|
|
|
if (commandArray[0] === "unalias" || commandArray[0] === "alias") {
|
|
|
|
return commandArray.join(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
let somethingSubstituted = true;
|
|
|
|
let depth = 0;
|
|
|
|
|
|
|
|
while (somethingSubstituted && depth < 10) {
|
|
|
|
depth++;
|
|
|
|
somethingSubstituted = false;
|
2023-05-05 09:55:59 +02:00
|
|
|
const alias = Aliases.get(commandArray[0])?.split(" ");
|
|
|
|
if (alias !== undefined) {
|
2021-09-05 01:09:30 +02:00
|
|
|
somethingSubstituted = true;
|
|
|
|
commandArray.splice(0, 1, ...alias);
|
|
|
|
//commandArray[0] = alias;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < commandArray.length; ++i) {
|
2023-05-05 09:55:59 +02:00
|
|
|
const alias = GlobalAliases.get(commandArray[i])?.split(" ");
|
|
|
|
if (alias !== undefined) {
|
2021-09-05 01:09:30 +02:00
|
|
|
somethingSubstituted = true;
|
|
|
|
commandArray.splice(i, 1, ...alias);
|
|
|
|
i += alias.length - 1;
|
|
|
|
//commandArray[i] = alias;
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return commandArray.join(" ");
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|