2019-04-11 10:37:40 +02:00
|
|
|
import { IMap } from "./types";
|
2021-09-16 08:52:45 +02:00
|
|
|
import { Terminal } from "./Terminal";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export let Aliases: IMap<string> = {};
|
|
|
|
export let GlobalAliases: IMap<string> = {};
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function loadAliases(saveString: string): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (saveString === "") {
|
|
|
|
Aliases = {};
|
|
|
|
} else {
|
|
|
|
Aliases = JSON.parse(saveString);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function loadGlobalAliases(saveString: string): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (saveString === "") {
|
|
|
|
GlobalAliases = {};
|
|
|
|
} else {
|
|
|
|
GlobalAliases = JSON.parse(saveString);
|
|
|
|
}
|
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 {
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const name in Aliases) {
|
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
2021-09-16 08:52:45 +02:00
|
|
|
Terminal.print("alias " + name + "=" + Aliases[name]);
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
for (const name in GlobalAliases) {
|
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
2021-09-16 08:52:45 +02:00
|
|
|
Terminal.print("global alias " + name + "=" + GlobalAliases[name]);
|
2017-06-30 18:44:03 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
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 {
|
2021-09-05 01:09:30 +02:00
|
|
|
const re = /^([_|\w|!|%|,|@]+)="(.+)"$/;
|
|
|
|
const matches = dec.match(re);
|
|
|
|
if (matches == null || matches.length != 3) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (global) {
|
|
|
|
addGlobalAlias(matches[1], matches[2]);
|
|
|
|
} else {
|
|
|
|
addAlias(matches[1], matches[2]);
|
|
|
|
}
|
|
|
|
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 {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (name in GlobalAliases) {
|
|
|
|
delete GlobalAliases[name];
|
|
|
|
}
|
|
|
|
Aliases[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 {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (name in Aliases) {
|
|
|
|
delete Aliases[name];
|
|
|
|
}
|
|
|
|
GlobalAliases[name] = value.trim();
|
2017-06-30 18:44:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
function getAlias(name: string): string | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
return Aliases[name];
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return null;
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
function getGlobalAlias(name: string): string | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
return GlobalAliases[name];
|
|
|
|
}
|
|
|
|
return null;
|
2017-06-30 18:44:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function removeAlias(name: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
delete Aliases[name];
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
delete GlobalAliases[name];
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
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) {
|
|
|
|
// For the alias and unalias commands, dont substite
|
|
|
|
if (commandArray[0] === "unalias" || commandArray[0] === "alias") {
|
|
|
|
return commandArray.join(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
let somethingSubstituted = true;
|
|
|
|
let depth = 0;
|
|
|
|
|
|
|
|
while (somethingSubstituted && depth < 10) {
|
|
|
|
depth++;
|
|
|
|
somethingSubstituted = false;
|
|
|
|
const alias = getAlias(commandArray[0])?.split(" ");
|
|
|
|
if (alias != null) {
|
|
|
|
somethingSubstituted = true;
|
|
|
|
commandArray.splice(0, 1, ...alias);
|
|
|
|
//commandArray[0] = 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;
|
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
|
|
|
}
|