2019-04-11 10:37:40 +02:00
|
|
|
import { IMap } from "./types";
|
|
|
|
import { post } from "./ui/postToTerminal";
|
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 {
|
2017-08-30 19:44:29 +02:00
|
|
|
if (saveString === "") {
|
|
|
|
Aliases = {};
|
|
|
|
} else {
|
|
|
|
Aliases = JSON.parse(saveString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function loadGlobalAliases(saveString: string): void {
|
2017-08-30 19:44:29 +02:00
|
|
|
if (saveString === "") {
|
|
|
|
GlobalAliases = {};
|
|
|
|
} else {
|
|
|
|
GlobalAliases = JSON.parse(saveString);
|
|
|
|
}
|
|
|
|
}
|
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-05-01 09:17:31 +02:00
|
|
|
for (const name in Aliases) {
|
2017-05-23 17:12:09 +02:00
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
post("alias " + name + "=" + Aliases[name]);
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
for (const name in GlobalAliases) {
|
2017-06-30 18:44:03 +02:00
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
post("global alias " + name + "=" + GlobalAliases[name]);
|
|
|
|
}
|
|
|
|
}
|
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-04-30 05:52:56 +02:00
|
|
|
const re = /^([_|\w|!|%|,|@]+)="(.+)"$/;
|
|
|
|
const matches = dec.match(re);
|
2017-05-23 17:12:09 +02:00
|
|
|
if (matches == null || matches.length != 3) {return false;}
|
2017-06-30 18:44:03 +02:00
|
|
|
if (global){
|
|
|
|
addGlobalAlias(matches[1],matches[2]);
|
|
|
|
} else {
|
|
|
|
addAlias(matches[1], matches[2]);
|
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
function addAlias(name: string, value: string): void {
|
|
|
|
if (name in GlobalAliases) {
|
2017-06-30 18:44:03 +02:00
|
|
|
delete GlobalAliases[name];
|
|
|
|
}
|
2021-05-08 18:44:21 +02:00
|
|
|
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 {
|
2017-06-30 18:44:03 +02:00
|
|
|
if (name in Aliases){
|
|
|
|
delete Aliases[name];
|
|
|
|
}
|
2021-05-08 18:44:21 +02:00
|
|
|
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 {
|
2017-05-23 17:12:09 +02:00
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
return Aliases[name];
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2017-05-23 17:12:09 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
function getGlobalAlias(name: string): string | null {
|
2017-06-30 18:44:03 +02:00
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
return GlobalAliases[name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
export function removeAlias(name: string): boolean {
|
2017-06-21 19:12:08 +02:00
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
delete Aliases[name];
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2017-07-05 19:53:51 +02:00
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
delete GlobalAliases[name];
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2017-06-21 19:12:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
const commandArray = origCommand.split(" ");
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length > 0){
|
2021-05-08 18:44:21 +02:00
|
|
|
// 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(" ");
|
2017-06-30 18:44:03 +02:00
|
|
|
if (alias != null) {
|
2021-05-08 18:44:21 +02:00
|
|
|
somethingSubstituted = true
|
|
|
|
commandArray.splice(0, 1, ...alias);
|
|
|
|
//commandArray[0] = alias;
|
2017-06-30 18:44:03 +02:00
|
|
|
}
|
2021-05-08 18:44:21 +02:00
|
|
|
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-06-30 18:44:03 +02:00
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return commandArray.join(" ");
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|