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 {
|
2017-05-23 17:12:09 +02:00
|
|
|
for (var name in Aliases) {
|
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
post("alias " + name + "=" + Aliases[name]);
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 18:44:03 +02:00
|
|
|
for (var name in GlobalAliases) {
|
|
|
|
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
|
2019-04-05 11:08:41 +02:00
|
|
|
export function parseAliasDeclaration(dec: string, global: boolean=false) {
|
2017-06-30 18:44:03 +02:00
|
|
|
var re = /^([_|\w|!|%|,|@]+)="(.+)"$/;
|
2017-05-23 17:12:09 +02:00
|
|
|
var matches = dec.match(re);
|
|
|
|
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];
|
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
Aliases[name] = value;
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
GlobalAliases[name] = value;
|
|
|
|
}
|
|
|
|
|
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){
|
|
|
|
// For the unalias command, dont substite
|
|
|
|
if (commandArray[0] === "unalias") { return commandArray.join(" "); }
|
|
|
|
|
2017-06-30 18:44:03 +02:00
|
|
|
var alias = getAlias(commandArray[0]);
|
2017-05-23 17:12:09 +02:00
|
|
|
if (alias != null) {
|
2017-06-30 18:44:03 +02:00
|
|
|
commandArray[0] = alias;
|
|
|
|
} else {
|
|
|
|
var alias = getGlobalAlias(commandArray[0]);
|
|
|
|
if (alias != null) {
|
|
|
|
commandArray[0] = alias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = 0; i < commandArray.length; ++i) {
|
|
|
|
var alias = getGlobalAlias(commandArray[i]);
|
|
|
|
if (alias != null) {
|
|
|
|
commandArray[i] = alias;
|
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return commandArray.join(" ");
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|