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>" + return "Commands list:<br>" +
" -about Information about this website<br>"+ " -about Information about this website<br>"+
" -clear Clear terminal screen<br>" + " -clear Clear terminal screen<br>" +
" -history Displays the command history of this session<br>" +
" -exec Execute arbitrary math and logic equations"; " -exec Execute arbitrary math and logic equations";
} }
@ -18,6 +19,10 @@ function cmd_clear() {
return ""; return "";
} }
function cmd_history() {
return "";
}
function cmd_exec(input) { function cmd_exec(input) {
// No input was given // No input was given
if (input === undefined) if (input === undefined)

View File

@ -5,6 +5,7 @@ const length = pretext.length+1;
let startPosition = length; let startPosition = length;
let cursorPosition = 0; let cursorPosition = 0;
let cursorYOffset = 7; let cursorYOffset = 7;
let history = {index: 0, list: []};
// Initiating constants // Initiating constants
const tbDiv = document.getElementById("textbox"); const tbDiv = document.getElementById("textbox");
@ -87,6 +88,14 @@ document.addEventListener("keydown", e => {
tbDiv.removeChild(textIn); tbDiv.removeChild(textIn);
tbDiv.removeChild(textCur); 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.appendChild(createText(text));
tbDiv.innerHTML += `<br>`; tbDiv.innerHTML += `<br>`;
@ -112,10 +121,25 @@ document.addEventListener("keydown", e => {
break; break;
case "ArrowUp": case "ArrowUp":
// Einen Befehl zurück in der History // 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; break;
case "ArrowDown": case "ArrowDown":
// Einen Befehl nach vorne in der History // 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; break;
case "Tab": case "Tab":
// Autocomplete // Autocomplete