2022-05-27 16:13:33 +00:00
|
|
|
const pretext = "root@baipyr.us:~# ";
|
|
|
|
let startPosition = 0;
|
|
|
|
let cursorPosition = 0;
|
2023-01-12 14:10:13 +00:00
|
|
|
let cursorYOffset = 7;
|
2022-05-27 16:13:33 +00:00
|
|
|
|
2023-01-05 23:13:56 +00:00
|
|
|
const tbDiv = document.getElementById("textbox");
|
2023-01-12 14:10:13 +00:00
|
|
|
const length = pretext.length+1;
|
2023-01-05 23:13:56 +00:00
|
|
|
startPosition = length;
|
2023-01-13 06:27:13 +00:00
|
|
|
cursorPosition = 0;
|
2023-01-05 23:13:56 +00:00
|
|
|
const textIn = document.getElementById("input");
|
2023-01-12 06:25:46 +00:00
|
|
|
const textCur = document.getElementById("current");
|
|
|
|
const cursor = document.getElementById("cursor");
|
2023-01-12 14:10:13 +00:00
|
|
|
cursor.style.transform = `translate(${length-1}ch,${2.222*7-0.4}ch)`;
|
2023-01-05 23:13:56 +00:00
|
|
|
|
2023-01-12 06:25:46 +00:00
|
|
|
textIn.oninput = () => {
|
2023-01-13 06:27:13 +00:00
|
|
|
const text = textCur.textContent;
|
|
|
|
if (cursorPosition === text.length)
|
|
|
|
textCur.textContent += textIn.value;
|
|
|
|
else {
|
|
|
|
const left = text.substring(0, cursorPosition);
|
|
|
|
const right = text.substring(cursorPosition, text.length);
|
|
|
|
textCur.textContent = left + textIn.value + right;
|
|
|
|
}
|
|
|
|
cursorPosition += textIn.value.length;
|
2023-01-12 06:25:46 +00:00
|
|
|
textIn.value = "";
|
|
|
|
updateCursor();
|
2023-01-05 23:13:56 +00:00
|
|
|
};
|
2023-01-12 06:25:46 +00:00
|
|
|
textIn.focus();
|
2023-01-05 23:13:56 +00:00
|
|
|
|
|
|
|
document.addEventListener("selectionchange", e => {
|
2023-01-12 06:25:46 +00:00
|
|
|
if (e.target !== textIn) {
|
2023-01-05 23:13:56 +00:00
|
|
|
const selection = window.getSelection();
|
|
|
|
if (selection.type === "Caret")
|
2023-01-12 06:25:46 +00:00
|
|
|
textIn.focus();
|
2023-01-05 23:13:56 +00:00
|
|
|
}
|
2022-05-27 16:13:33 +00:00
|
|
|
});
|
|
|
|
|
2023-01-05 23:13:56 +00:00
|
|
|
document.addEventListener("keydown", e => {
|
2023-01-12 06:25:46 +00:00
|
|
|
const text = textCur.textContent;
|
2022-05-27 16:13:33 +00:00
|
|
|
switch (e.key) {
|
|
|
|
case "Backspace":
|
|
|
|
// Ein Zeichen nach links löschen
|
2023-01-13 06:27:13 +00:00
|
|
|
if (cursorPosition === 0)
|
2022-05-27 16:13:33 +00:00
|
|
|
return;
|
2023-01-13 06:27:13 +00:00
|
|
|
if (cursorPosition <= textCur.textContent.length-1) {
|
|
|
|
const a = text.substring(0, cursorPosition-1);
|
|
|
|
const b = text.substring(cursorPosition, text.length);
|
2023-01-12 06:25:46 +00:00
|
|
|
textCur.textContent=a+b;
|
2022-05-27 16:13:33 +00:00
|
|
|
} else
|
2023-01-12 06:25:46 +00:00
|
|
|
textCur.textContent = text.substring(0,text.length-1);
|
2022-05-27 16:13:33 +00:00
|
|
|
cursorPosition--;
|
2023-01-12 06:25:46 +00:00
|
|
|
updateCursor();
|
2022-05-27 16:13:33 +00:00
|
|
|
break;
|
|
|
|
case "ArrowLeft":
|
|
|
|
// Ein Zeichen nach links
|
2023-01-13 06:27:13 +00:00
|
|
|
if (cursorPosition > 0) {
|
2022-05-27 16:13:33 +00:00
|
|
|
cursorPosition--;
|
2023-01-12 06:25:46 +00:00
|
|
|
updateCursor();
|
|
|
|
}
|
2022-05-27 16:13:33 +00:00
|
|
|
break;
|
|
|
|
case "ArrowRight":
|
|
|
|
// Ein Zeichen nach reckts
|
2023-01-13 06:27:13 +00:00
|
|
|
if (cursorPosition < textCur.textContent.length) {
|
2022-05-27 16:13:33 +00:00
|
|
|
cursorPosition++;
|
2023-01-12 06:25:46 +00:00
|
|
|
updateCursor();
|
|
|
|
}
|
2022-05-27 16:13:33 +00:00
|
|
|
break;
|
|
|
|
case "Delete":
|
|
|
|
// Ein Zeichen nach rechts löschen
|
2023-01-13 06:27:13 +00:00
|
|
|
if (cursorPosition >= textCur.textContent.length)
|
|
|
|
return;
|
|
|
|
if (cursorPosition === 0 && textCur.textContent.length === 0)
|
2022-05-27 16:13:33 +00:00
|
|
|
return;
|
2023-01-13 06:27:13 +00:00
|
|
|
const a = text.substring(0, cursorPosition);
|
|
|
|
const b = text.substring(cursorPosition+1, text.length);
|
2023-01-12 06:25:46 +00:00
|
|
|
textCur.textContent=a+b;
|
2022-05-27 16:13:33 +00:00
|
|
|
break;
|
|
|
|
case "Enter":
|
|
|
|
// Befehl absenden / Zeilenumbruch
|
2023-01-05 23:13:56 +00:00
|
|
|
tbDiv.removeChild(textIn);
|
2023-01-12 06:25:46 +00:00
|
|
|
tbDiv.removeChild(textCur);
|
2022-05-27 16:13:33 +00:00
|
|
|
|
|
|
|
// Run command and return output???
|
2023-01-12 13:37:33 +00:00
|
|
|
let commandOutput = runCommand(text);
|
2023-01-05 23:13:56 +00:00
|
|
|
|
2023-01-12 13:40:53 +00:00
|
|
|
tbDiv.appendChild(createText(text));
|
2023-01-12 13:37:33 +00:00
|
|
|
tbDiv.innerHTML += `<br>`;
|
2023-01-05 23:13:56 +00:00
|
|
|
if (commandOutput !== "") {
|
2023-01-12 13:40:53 +00:00
|
|
|
tbDiv.append(createText(commandOutput))
|
2023-01-12 13:37:33 +00:00
|
|
|
tbDiv.innerHTML += `<br>`;
|
2023-01-05 23:13:56 +00:00
|
|
|
cursorYOffset++;
|
|
|
|
}
|
2023-01-12 13:37:33 +00:00
|
|
|
tbDiv.innerHTML += pretext;
|
2023-01-12 06:25:46 +00:00
|
|
|
textCur.textContent = "";
|
|
|
|
tbDiv.appendChild(textCur);
|
2023-01-05 23:13:56 +00:00
|
|
|
tbDiv.appendChild(textIn);
|
2023-01-13 06:27:13 +00:00
|
|
|
cursorPosition = 0;
|
2022-06-06 14:56:20 +00:00
|
|
|
cursorYOffset++;
|
2023-01-12 06:25:46 +00:00
|
|
|
updateCursor();
|
2022-05-27 16:13:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-12 13:40:53 +00:00
|
|
|
function createText(str) {
|
|
|
|
const link = document.createElement("a");
|
|
|
|
link.style.color = "lightgrey";
|
|
|
|
link.textContent = str;
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:37:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-12 06:25:46 +00:00
|
|
|
function updateCursor() {
|
|
|
|
let cursor = document.getElementById("cursor");
|
2023-01-13 06:27:13 +00:00
|
|
|
cursor.style.transform = `translate(${startPosition+cursorPosition-1}ch, ${2.222*cursorYOffset-0.4}ch)`;
|
2023-01-12 06:25:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 16:13:33 +00:00
|
|
|
setInterval(()=>{
|
2023-01-05 23:13:56 +00:00
|
|
|
cursor.style.opacity = !parseInt(cursor.style.opacity)+0;
|
2022-05-27 16:13:33 +00:00
|
|
|
},1000);
|