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 const textIn = document.getElementById("input"); tbDiv.removeChild(textIn); tbDiv.removeChild(textCur); // Run command and return output??? let commandOutput = ""; const aTag = str => `${str}`; if (commandOutput !== "") { commandOutput = `${aTag(commandOutput)}
`; cursorYOffset++; } tbDiv.innerHTML += `${aTag(text)}
${commandOutput}${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 updateCursor() { let cursor = document.getElementById("cursor"); cursor.style.transform = `translate(${cursorPosition-1}ch, ${cursorYOffset}em)`; } setInterval(()=>{ cursor.style.opacity = !parseInt(cursor.style.opacity)+0; },1000);