2017-08-30 19:44:29 +02:00
|
|
|
import {post} from "./Terminal.js";
|
|
|
|
|
|
|
|
let Aliases = {};
|
|
|
|
let GlobalAliases = {};
|
|
|
|
|
|
|
|
function loadAliases(saveString) {
|
|
|
|
if (saveString === "") {
|
|
|
|
Aliases = {};
|
|
|
|
} else {
|
|
|
|
Aliases = JSON.parse(saveString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadGlobalAliases(saveString) {
|
|
|
|
if (saveString === "") {
|
|
|
|
GlobalAliases = {};
|
|
|
|
} else {
|
|
|
|
GlobalAliases = JSON.parse(saveString);
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
|
|
|
|
//Print all aliases to terminal
|
|
|
|
function printAliases() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//True if successful, false otherwise
|
2017-06-30 18:44:03 +02:00
|
|
|
function parseAliasDeclaration(dec,global=false) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addAlias(name, value) {
|
2017-06-30 18:44:03 +02:00
|
|
|
if (name in GlobalAliases){
|
|
|
|
delete GlobalAliases[name];
|
|
|
|
}
|
2017-05-23 17:12:09 +02:00
|
|
|
Aliases[name] = value;
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:44:03 +02:00
|
|
|
function addGlobalAlias(name, value) {
|
|
|
|
if (name in Aliases){
|
|
|
|
delete Aliases[name];
|
|
|
|
}
|
|
|
|
GlobalAliases[name] = value;
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:12:09 +02:00
|
|
|
function getAlias(name) {
|
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
return Aliases[name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:44:03 +02:00
|
|
|
function getGlobalAlias(name) {
|
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
return GlobalAliases[name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-21 19:12:08 +02:00
|
|
|
function removeAlias(name) {
|
|
|
|
if (Aliases.hasOwnProperty(name)) {
|
|
|
|
delete Aliases[name];
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-05 19:53:51 +02:00
|
|
|
if (GlobalAliases.hasOwnProperty(name)) {
|
|
|
|
delete GlobalAliases[name];
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-21 19:12:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:12:09 +02:00
|
|
|
//Returns the original string with any aliases substituted in
|
|
|
|
//Aliases only applied to "whole words", one level deep
|
|
|
|
function substituteAliases(origCommand) {
|
|
|
|
var commandArray = origCommand.split(" ");
|
2017-06-30 18:44:03 +02:00
|
|
|
if (commandArray.length>0){
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export {Aliases, GlobalAliases, printAliases, parseAliasDeclaration,
|
|
|
|
removeAlias, substituteAliases, loadAliases, loadGlobalAliases};
|