const pretext = "root@baipyr.us:~# "; let startPosition = 0; let cursorPosition = 0; let cursorYOffset = 0; const tbDiv = document.getElementById("textbox"); tbDiv.innerHTML = pretext+tbDiv.innerHTML; const length = tbDiv.innerText.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)`; 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); const aTag = str => { const link = document.createElement("a"); link.style.color = "lightgrey"; link.textContent = str; return link; }; tbDiv.appendChild(aTag(text)); tbDiv.innerHTML += `
`; if (commandOutput !== "") { tbDiv.append(aTag(commandOutput)) tbDiv.innerHTML += `
`; 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 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}ch)`; } setInterval(()=>{ cursor.style.opacity = !parseInt(cursor.style.opacity)+0; },1000);