Alert toggles for errors and non-error outputs
e.g. for running many commands through console and not wanting alert()s
This commit is contained in:
parent
03d32a8f5b
commit
d5be723fe6
146
mods/prompt.js
146
mods/prompt.js
|
|
@ -35,7 +35,25 @@ function hslStringToUnvalidatedObject(string) {
|
||||||
return {h: hue, s: saturation, l: lightness};
|
return {h: hue, s: saturation, l: lightness};
|
||||||
}
|
}
|
||||||
|
|
||||||
function funniPrompt(argument=null) {
|
function alertIfError(alertError,text) {
|
||||||
|
if(alertError) {
|
||||||
|
alert(text);
|
||||||
|
return(true);
|
||||||
|
} else {
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function alertIfOutput(alertOutput,text) {
|
||||||
|
if(alertOutput) {
|
||||||
|
alert(text);
|
||||||
|
return(true);
|
||||||
|
} else {
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function funniPrompt(argument=null,alertOutput=true,alertError=true) {
|
||||||
argument === null ? inputText = prompt("Enter command") : inputText = argument;
|
argument === null ? inputText = prompt("Enter command") : inputText = argument;
|
||||||
// replace spaces with underscores
|
// replace spaces with underscores
|
||||||
inputAsArray = inputText.split(" ");
|
inputAsArray = inputText.split(" ");
|
||||||
|
|
@ -43,10 +61,9 @@ function funniPrompt(argument=null) {
|
||||||
|
|
||||||
switch(firstItem) {
|
switch(firstItem) {
|
||||||
case "set":
|
case "set":
|
||||||
//alert("To do");
|
|
||||||
if(inputAsArray.length < 4) {
|
if(inputAsArray.length < 4) {
|
||||||
alert("Usage: set [property] [element] [value] <type>\nDon't include framing characters []<>.\nThe element can be \"all\" to set the property for every pixel.\nNote: Strings can't have spaces because spaces are the separator used in the parsing split().\nArguments in [brackets] are required and ones in <angle brackets> are optional.");
|
alertIfError(alertError,"Usage: set [property] [element] [value] <type>\nDon't include framing characters []<>.\nThe element can be \"all\" to set the property for every pixel.\nNote: Strings can't have spaces because spaces are the separator used in the parsing split().\nArguments in [brackets] are required and ones in <angle brackets> are optional.");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
var property = inputAsArray[1];
|
var property = inputAsArray[1];
|
||||||
//console.log("Property gotten: " + property);
|
//console.log("Property gotten: " + property);
|
||||||
|
|
@ -73,8 +90,8 @@ function funniPrompt(argument=null) {
|
||||||
} else if(objectSynonyms.includes(type.toLowerCase())) {
|
} else if(objectSynonyms.includes(type.toLowerCase())) {
|
||||||
type = "object";
|
type = "object";
|
||||||
}*/ else {
|
}*/ else {
|
||||||
alert("Unrecognized type: \"" + type + "\"");
|
alertIfError(alertError,"Unrecognized type: \"" + type + "\".");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
if(type === null) {
|
if(type === null) {
|
||||||
|
|
@ -83,16 +100,16 @@ function funniPrompt(argument=null) {
|
||||||
} else if(defaultNumberTypeValues.includes(property)) {
|
} else if(defaultNumberTypeValues.includes(property)) {
|
||||||
type = "number";
|
type = "number";
|
||||||
} else {
|
} else {
|
||||||
alert("Type could not be assumed from property. Please specify the type as a fourth argument.");
|
alertIfError(alertError,"Type could not be assumed from property. Please specify the type as a fourth argument.");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type === "number") {
|
if(type === "number") {
|
||||||
value = parseFloat(value);
|
value = parseFloat(value);
|
||||||
if(isNaN(value)) {
|
if(isNaN(value)) {
|
||||||
alert("Value is not a number!");
|
alertIfError(alertError,"Value is not a number!");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
} else if(type === "boolean") {
|
} else if(type === "boolean") {
|
||||||
if(trueSynonyms.includes(value.toLowerCase())) {
|
if(trueSynonyms.includes(value.toLowerCase())) {
|
||||||
|
|
@ -100,8 +117,8 @@ function funniPrompt(argument=null) {
|
||||||
} else if(falseSynonyms.includes(value.toLowerCase())) {
|
} else if(falseSynonyms.includes(value.toLowerCase())) {
|
||||||
value = false;
|
value = false;
|
||||||
} else {
|
} else {
|
||||||
alert("Unrecognized boolean value: " + value);
|
alertIfError(alertError,"Unrecognized boolean value: " + value + ".");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//The values start out as strings when split from the array, so string is kind of the default form.
|
//The values start out as strings when split from the array, so string is kind of the default form.
|
||||||
|
|
@ -112,53 +129,54 @@ function funniPrompt(argument=null) {
|
||||||
var originalInput = value; //for error display
|
var originalInput = value; //for error display
|
||||||
value = mostSimilarElement(value);
|
value = mostSimilarElement(value);
|
||||||
if(!elements[value]) {
|
if(!elements[value]) {
|
||||||
alert("Element " + originalInput + " does not exist!");
|
alertIfError(alertError,"Element " + originalInput + " does not exist!");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if(property === "x") {
|
if(property === "x") {
|
||||||
if(!Number.isSafeInteger(value)) {
|
if(!Number.isSafeInteger(value)) {
|
||||||
alert("X cannot be a decimal! And what are you doing trying to set position values anyway?");
|
alertIfError(alertError,"X cannot be a decimal! And what are you doing trying to set position values anyway?");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if(property === "color") {
|
if(property === "color") {
|
||||||
if(!value.startsWith("rgb(")) { //if not RGB
|
if(!value.startsWith("rgb(")) { //if not RGB
|
||||||
if(value.startsWith("hsl(")) { //if HSL
|
if(value.startsWith("hsl(")) { //if HSL
|
||||||
if(!(value.split(",")[1].endsWith('%')) && !(value.split(",")[2].endsWith('%)'))) { //if missing percent symbols
|
if(!(value.split(",")[1].endsWith('%')) && !(value.split(",")[2].endsWith('%)'))) { //if missing percent symbols
|
||||||
alert("Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
alertIfError(alertError,"Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
} else { //if not RGB and not HSL
|
} else { //if not RGB and not HSL
|
||||||
alert("Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
alertIfError(alertError,"Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if(value.split(",").length !== 3) { //if too short or long
|
if(value.split(",").length !== 3) { //if too short or long
|
||||||
alert("Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
alertIfError(alertError,"Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
if(value.startsWith("rgb(")) { //if RGB
|
if(value.startsWith("rgb(")) { //if RGB
|
||||||
var checkedColorObject = rgbStringToUnvalidatedObject(value); //RGB NaN checking
|
var checkedColorObject = rgbStringToUnvalidatedObject(value); //RGB NaN checking
|
||||||
if(isNaN(checkedColorObject.r) || isNaN(checkedColorObject.g) || isNaN(checkedColorObject.b)) {
|
if(isNaN(checkedColorObject.r) || isNaN(checkedColorObject.g) || isNaN(checkedColorObject.b)) {
|
||||||
//console.log(checkedColorObject);
|
//console.log(checkedColorObject);
|
||||||
alert("One or more color values are invalid!");
|
alertIfError(alertError,"One or more color values are invalid!");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
} else if(value.startsWith("hsl(")) { //if HSL
|
} else if(value.startsWith("hsl(")) { //if HSL
|
||||||
var checkedColorObject = hslStringToUnvalidatedObject(value); //HSL NaN checking
|
var checkedColorObject = hslStringToUnvalidatedObject(value); //HSL NaN checking
|
||||||
if(isNaN(checkedColorObject.h) || isNaN(checkedColorObject.s) || isNaN(checkedColorObject.l)) {
|
if(isNaN(checkedColorObject.h) || isNaN(checkedColorObject.s) || isNaN(checkedColorObject.l)) {
|
||||||
//console.log(checkedColorObject);
|
//console.log(checkedColorObject);
|
||||||
alert("One or more color values are invalid!");
|
alertIfError(alertError,"One or more color values are invalid!");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
} else { //if neither
|
} else { //if neither
|
||||||
alert("Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
alertIfError(alertError,"Color must be in the form \"rgb(red,green,blue)\" or \"hsl(hue,saturation%,lightness%)\"!");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
//Actual setting code;
|
//Actual setting code;
|
||||||
|
var setCount = 0;
|
||||||
for (var i = 1; i < width; i++) {
|
for (var i = 1; i < width; i++) {
|
||||||
for (var j = 1; j < height; j++) {
|
for (var j = 1; j < height; j++) {
|
||||||
if (!isEmpty(i,j)) {
|
if (!isEmpty(i,j)) {
|
||||||
|
|
@ -166,19 +184,21 @@ function funniPrompt(argument=null) {
|
||||||
if(pixelMap[i][j].element === inputElement || inputElement === "all") {
|
if(pixelMap[i][j].element === inputElement || inputElement === "all") {
|
||||||
//console.log("Element is a match: " + inputElement + ", " + pixelMap[i][j].element)
|
//console.log("Element is a match: " + inputElement + ", " + pixelMap[i][j].element)
|
||||||
pixelMap[i][j][property] = value;
|
pixelMap[i][j][property] = value;
|
||||||
|
setCount++;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
break;
|
inputElement === "all" ? alertIfOutput(alertOutput,`Set ${property} of ${setCount} pixels to ${value}.`) : alertIfOutput(alertOutput,`Set ${property} of ${setCount} ${inputElement} pixels to ${value}.`)
|
||||||
|
return true;
|
||||||
case "test":
|
case "test":
|
||||||
alert("pong");
|
alertIfOutput(alertOutput,"pong");
|
||||||
console.log("qwertyuiopasdfghjklzxcvbnm");
|
console.log("qwertyuiopasdfghjklzxcvbnm");
|
||||||
break;
|
return true;
|
||||||
case "fill":
|
case "fill":
|
||||||
if(inputAsArray.length < 3) {
|
if(inputAsArray.length < 3) {
|
||||||
alert("Usage: fill [overwrite (should be a bool)] [element] <additional elements>.\nDon't include framing characters []<>.\nArguments in [brackets] are required and ones in <angle brackets> are optional.");
|
alertIfError(alertError,"Usage: fill [overwrite (should be a bool)] [element] <additional elements>.\nDon't include framing characters []<>.\nArguments in [brackets] are required and ones in <angle brackets> are optional.");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
var doOverwrite = inputAsArray[1];
|
var doOverwrite = inputAsArray[1];
|
||||||
|
|
@ -191,8 +211,8 @@ function funniPrompt(argument=null) {
|
||||||
var originalElement = elementInConsideration; //also for error display
|
var originalElement = elementInConsideration; //also for error display
|
||||||
elementInConsideration = mostSimilarElement(elementInConsideration);
|
elementInConsideration = mostSimilarElement(elementInConsideration);
|
||||||
if(!elements[elementInConsideration]) {
|
if(!elements[elementInConsideration]) {
|
||||||
alert("Element " + originalElement + " does not exist!");
|
alertIfError(alertError,"Element " + originalElement + " does not exist!");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
elementList[i] = elementInConsideration;
|
elementList[i] = elementInConsideration;
|
||||||
};
|
};
|
||||||
|
|
@ -203,13 +223,14 @@ function funniPrompt(argument=null) {
|
||||||
} else if(falseSynonyms.includes(doOverwrite.toLowerCase())) {
|
} else if(falseSynonyms.includes(doOverwrite.toLowerCase())) {
|
||||||
doOverwrite = false;
|
doOverwrite = false;
|
||||||
} else {
|
} else {
|
||||||
alert("Unrecognized boolean value: " + doOverwrite + "\n Note that for this command, the boolean value goes first.");
|
alertIfError(alertError,"Unrecognized boolean value: " + doOverwrite + "\n Note that for this command, the boolean value goes first.");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
//console.log(doOverwrite);
|
//console.log(doOverwrite);
|
||||||
//console.log(elementList);
|
//console.log(elementList);
|
||||||
|
|
||||||
//Fill code
|
//Fill code
|
||||||
|
var fillCount = 0;
|
||||||
for (var i = 1; i < width; i++) {
|
for (var i = 1; i < width; i++) {
|
||||||
for (var j = 1; j < height; j++) {
|
for (var j = 1; j < height; j++) {
|
||||||
var randomElement = elementList[Math.floor(Math.random() * elementList.length)];
|
var randomElement = elementList[Math.floor(Math.random() * elementList.length)];
|
||||||
|
|
@ -218,14 +239,16 @@ function funniPrompt(argument=null) {
|
||||||
};
|
};
|
||||||
if (isEmpty(i,j,false)) {
|
if (isEmpty(i,j,false)) {
|
||||||
createPixel(randomElement,i,j);
|
createPixel(randomElement,i,j);
|
||||||
|
fillCount++;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
break;
|
alertIfOutput(alertOutput,`Placed ${fillCount} pixels`);
|
||||||
|
return fillCount;
|
||||||
case "randomfill":
|
case "randomfill":
|
||||||
if(inputAsArray.length < 1) { //somehow?
|
if(inputAsArray.length < 1) { //somehow?
|
||||||
alert("Usage: randomfill <overwrite (should be a bool) (default: true)>\nDon't include framing characters []<>.\nArguments in <angle brackets> are optional.");
|
alertIfError(alertError,"Usage: randomfill <overwrite (should be a bool) (default: true)>\nDon't include framing characters []<>.\nArguments in <angle brackets> are optional.");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
var doOverwrite = null;
|
var doOverwrite = null;
|
||||||
|
|
@ -237,8 +260,8 @@ function funniPrompt(argument=null) {
|
||||||
} else if(falseSynonyms.includes(doOverwrite.toLowerCase())) {
|
} else if(falseSynonyms.includes(doOverwrite.toLowerCase())) {
|
||||||
doOverwrite = false;
|
doOverwrite = false;
|
||||||
} else {
|
} else {
|
||||||
alert("Unrecognized boolean value: " + value);
|
alertIfError(alertError,"Unrecognized boolean value: " + value);
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
doOverwrite = true;
|
doOverwrite = true;
|
||||||
|
|
@ -247,6 +270,7 @@ function funniPrompt(argument=null) {
|
||||||
var elementList = randomChoices;
|
var elementList = randomChoices;
|
||||||
|
|
||||||
//Fill code
|
//Fill code
|
||||||
|
var fillCount = 0;
|
||||||
for (var i = 1; i < width; i++) {
|
for (var i = 1; i < width; i++) {
|
||||||
for (var j = 1; j < height; j++) {
|
for (var j = 1; j < height; j++) {
|
||||||
var randomElement = elementList[Math.floor(Math.random() * elementList.length)];
|
var randomElement = elementList[Math.floor(Math.random() * elementList.length)];
|
||||||
|
|
@ -255,15 +279,16 @@ function funniPrompt(argument=null) {
|
||||||
};
|
};
|
||||||
if (isEmpty(i,j,false)) {
|
if (isEmpty(i,j,false)) {
|
||||||
createPixel(randomElement,i,j);
|
createPixel(randomElement,i,j);
|
||||||
|
fillCount++;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
break;
|
alertIfOutput(alertOutput,`Placed ${fillCount} random pixels`);
|
||||||
|
return fillCount;
|
||||||
case "count":
|
case "count":
|
||||||
//alert("To do");
|
|
||||||
if(inputAsArray.length < 2) {
|
if(inputAsArray.length < 2) {
|
||||||
alert("Usage: count [element]\nDon't include framing characters []<>.\nNote: The element name can't have a space in it because spaces are the separator used in the parsing split().\nArguments in [brackets] are required.");
|
alertIfError(alertError,"Usage: count [element]\nDon't include framing characters []<>.\nNote: The element name can't have a space in it because spaces are the separator used in the parsing split().\nArguments in [brackets] are required.");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
var inputElement = inputAsArray[1];
|
var inputElement = inputAsArray[1];
|
||||||
//console.log("Element gotten: " + inputElement);
|
//console.log("Element gotten: " + inputElement);
|
||||||
|
|
@ -272,8 +297,8 @@ function funniPrompt(argument=null) {
|
||||||
inputElement = mostSimilarElement(inputElement);
|
inputElement = mostSimilarElement(inputElement);
|
||||||
//console.log("Element gotten: " + inputElement);
|
//console.log("Element gotten: " + inputElement);
|
||||||
if(typeof(elements[inputElement]) === "undefined") {
|
if(typeof(elements[inputElement]) === "undefined") {
|
||||||
alert("Element " + originalInput + " does not exist!");
|
alertIfError(alertError,"Element " + originalInput + " does not exist!");
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Actual counting code;
|
//Actual counting code;
|
||||||
|
|
@ -289,8 +314,8 @@ function funniPrompt(argument=null) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
alert(`There are ${count} pixels of ${inputElement}`);
|
alertIfOutput(alertOutput,`There are ${count} pixels of ${inputElement}`);
|
||||||
break;
|
return count;
|
||||||
case "countall":
|
case "countall":
|
||||||
var listObject = {};
|
var listObject = {};
|
||||||
|
|
||||||
|
|
@ -317,28 +342,31 @@ function funniPrompt(argument=null) {
|
||||||
formattedList += `${elementName}: ${elementCount}\n`;
|
formattedList += `${elementName}: ${elementCount}\n`;
|
||||||
};
|
};
|
||||||
|
|
||||||
alert("Elements counts logged to console");
|
alertIfOutput(alertOutput,"Elements counts logged to console");
|
||||||
console.log(formattedList);
|
console.log(formattedList);
|
||||||
break;
|
return listObject;
|
||||||
case "help":
|
case "help":
|
||||||
if(inputAsArray.length < 1) { //somehow
|
if(inputAsArray.length < 1) { //somehow
|
||||||
alert("Usage: help <command>\nDon't include framing characters []<>.\nArguments in <angle brackets> are optional.");
|
alertIfError(alertError,"Usage: help <command>\nDon't include framing characters []<>.\nArguments in <angle brackets> are optional.");
|
||||||
break;
|
return false;
|
||||||
};
|
};
|
||||||
if(inputAsArray.length < 2) {
|
if(inputAsArray.length < 2) {
|
||||||
alert("Commands: \nset\ntest\nfill\nrandomfill\ncount\ncountall\nhelp")
|
alertOutput ? alertIfOutput(alertOutput,"Commands: \nset\ntest\nfill\nrandomfill\ncount\ncountall\nhelp") : console.log("Commands: set, test, fill, randomfill, count, countall, help");
|
||||||
} else {
|
} else {
|
||||||
var command = inputAsArray[1];
|
var command = inputAsArray[1];
|
||||||
|
|
||||||
if(typeof(commandHelpObject[command]) === "undefined") {
|
if(typeof(commandHelpObject[command]) === "undefined") {
|
||||||
alert("Cound not find help for " + command + ".");
|
alertIfError(alertError,"Cound not find help for " + command + ".");
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
alert(commandHelpObject[command]);
|
alertOutput ? alertIfOutput(alertOutput,commandHelpObject[command]) : console.log(commandHelpObject[command].replace(/\n/g, " "));
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
break;
|
return true;
|
||||||
default:
|
default:
|
||||||
alert(`Command ${firstItem} not found!`);
|
alertIfError(alertError,`Command ${firstItem} not found!`);
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue