TerminalHomepage/js/main.js
2023-01-12 15:10:13 +01:00

139 lines
4.2 KiB
JavaScript

const pretext = "root@baipyr.us:~# ";
let startPosition = 0;
let cursorPosition = 0;
let cursorYOffset = 7;
const tbDiv = document.getElementById("textbox");
const length = pretext.length+1;
startPosition = length;
cursorPosition = length;
const textIn = document.getElementById("input");
const textCur = document.getElementById("current");
const cursor = document.getElementById("cursor");
cursor.style.transform = `translate(${length-1}ch,${2.222*7-0.4}ch)`;
textIn.oninput = () => {
textCur.textContent += textIn.value;
textIn.value = "";
cursorPosition++;
updateCursor();
};
textIn.focus();
document.addEventListener("selectionchange", e => {
if (e.target !== textIn) {
const selection = window.getSelection();
if (selection.type === "Caret")
textIn.focus();
}
});
document.addEventListener("keydown", e => {
const index = cursorPosition - startPosition;
const text = textCur.textContent;
switch (e.key) {
case "Backspace":
// Ein Zeichen nach links löschen
if (index === 0)
return;
if (index <= textCur.textContent.length-1) {
const a = text.substring(0, index-1);
const b = text.substring(index, text.length);
textCur.textContent=a+b;
} else
textCur.textContent = text.substring(0,text.length-1);
cursorPosition--;
updateCursor();
break;
case "ArrowLeft":
// Ein Zeichen nach links
if (cursorPosition > startPosition) {
cursorPosition--;
updateCursor();
}
break;
case "ArrowRight":
// Ein Zeichen nach reckts
if (index < textCur.textContent.length) {
cursorPosition++;
updateCursor();
}
break;
case "Delete":
// Ein Zeichen nach rechts löschen
if (index === 0 || index >= textCur.textContent.length)
return;
const a = text.substring(0, index);
const b = text.substring(index+1, text.length);
textCur.textContent=a+b;
break;
case "Enter":
// Befehl absenden / Zeilenumbruch
tbDiv.removeChild(textIn);
tbDiv.removeChild(textCur);
// Run command and return output???
let commandOutput = runCommand(text);
tbDiv.appendChild(createText(text));
tbDiv.innerHTML += `<br>`;
if (commandOutput !== "") {
tbDiv.append(createText(commandOutput))
tbDiv.innerHTML += `<br>`;
cursorYOffset++;
}
tbDiv.innerHTML += pretext;
textCur.textContent = "";
tbDiv.appendChild(textCur);
tbDiv.appendChild(textIn);
cursorPosition = startPosition;
cursorYOffset++;
updateCursor();
break;
case "ArrowUp":
// Einen Befehl zurück in der History
break;
case "ArrowDown":
// Einen Befehl nach vorne in der History
break;
case "Tab":
// Autocomplete
break;
}
});
function createText(str) {
const link = document.createElement("a");
link.style.color = "lightgrey";
link.textContent = str;
return link;
}
function runCommand(input) {
let output = "";
for (const func in window) {
const splits = func.split("cmd_");
if (splits.length === 2) {
const name = splits[1];
if (input.toLowerCase() === name.toLowerCase()) {
output = window[func]();
break;
} else if (input.startsWith(name + " ")) {
output = window[func](input.split(" ").filter((e,i)=>i!==0));
break;
}
}
}
return output;
}
function updateCursor() {
let cursor = document.getElementById("cursor");
cursor.style.transform = `translate(${cursorPosition-1}ch, ${2.222*cursorYOffset-0.4}ch)`;
}
setInterval(()=>{
cursor.style.opacity = !parseInt(cursor.style.opacity)+0;
},1000);