TerminalHomepage/js/commands.js
2023-01-16 07:21:58 +01:00

64 lines
1.9 KiB
JavaScript

function cmd_help() {
// Display 'help' message
return "Commands list:<br>" +
" -about Information about this website<br>"+
" -clear Clear terminal screen<br>" +
" -history Displays the command history of this session<br>" +
" -exec Execute arbitrary math and logic equations";
}
function cmd_about() {
// Display 'about' message
return "This website is based on the general idea and design of a terminal.<br>" +
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
}
function cmd_clear() {
// Clear terminal by reloading page
window.location.reload();
return "";
}
function cmd_history() {
// Display the command history line by line
let output = "";
const hl = history.list;
for (let i = 0; i < hl.length; i++) {
const lineBreak = (i !== hl.length - 1) ? "<br>" : "";
output += `${i+1} ${hl[i]}${lineBreak}`;
}
return output;
}
function cmd_exec(input) {
// No input was given
if (input === undefined)
return "You must enter parameters!";
// Input contains letters or invalid characters
const str = input.join(' ');
if (/[',:;a-zA-Z]/.test(str) || str === "")
return "Invalid input!";
// Input is inside of character range with exceptions
const chars = str.split('').filter(e => {
const code = e.charCodeAt(0);
const s = code === 32;
const a = code > 36;
const b = code < 63;
const c = code === 94;
const d = code === 124;
return !(s || a && b || c || d);
});
// If exceptions remain, invalid input was given
if (chars.length > 0)
return "Invalid input!";
// Execute input and return output
return (new Function(`return ${str}`))().toString();
}
function cmd_echo(input) {
if (input === undefined)
return " ";
return input.join(' ');
}