added command history

This commit is contained in:
Baipyrus 2023-01-13 12:57:46 +01:00
parent d4f648fd53
commit 841f0f60d2
2 changed files with 30 additions and 1 deletions

View File

@ -3,6 +3,7 @@ function cmd_help() {
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";
}
@ -18,6 +19,10 @@ function cmd_clear() {
return "";
}
function cmd_history() {
return "";
}
function cmd_exec(input) {
// No input was given
if (input === undefined)

View File

@ -5,6 +5,7 @@ const length = pretext.length+1;
let startPosition = length;
let cursorPosition = 0;
let cursorYOffset = 7;
let history = {index: 0, list: []};
// Initiating constants
const tbDiv = document.getElementById("textbox");
@ -87,6 +88,14 @@ document.addEventListener("keydown", e => {
tbDiv.removeChild(textIn);
tbDiv.removeChild(textCur);
const inputSet = new Set(text.split(''));
if (text !== "" || inputSet.has(" ") && inputSet.size === 1) {
history.list.push(text);
history.index = history.list.length;
console.log(history);
}
// Fix current input to page
tbDiv.appendChild(createText(text));
tbDiv.innerHTML += `<br>`;
@ -112,10 +121,25 @@ document.addEventListener("keydown", e => {
break;
case "ArrowUp":
// Einen Befehl zurück in der History
if (history.index > 0) {
history.index--;
textCur.textContent = history.list[history.index];
cursorPosition = textCur.textContent.length;
}
updateCursor();
break;
case "ArrowDown":
// Einen Befehl nach vorne in der History
if (history.index < history.list.length-1) {
history.index++;
textCur.textContent = history.list[history.index];
cursorPosition = textCur.textContent.length;
} else {
history.index++;
textCur.textContent = "";
cursorPosition = 0;
}
updateCursor();
break;
case "Tab":
// Autocomplete