From 841f0f60d2d8ccf4409b6ed0c7249bc6c8535ac4 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 13 Jan 2023 12:57:46 +0100 Subject: [PATCH] added command history --- js/commands.js | 5 +++++ js/main.js | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/js/commands.js b/js/commands.js index 8420d38..917a9dc 100644 --- a/js/commands.js +++ b/js/commands.js @@ -3,6 +3,7 @@ function cmd_help() { return "Commands list:
" + " -about Information about this website
"+ " -clear Clear terminal screen
" + + " -history Displays the command history of this session
" + " -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) diff --git a/js/main.js b/js/main.js index 98aa1f9..4698dd7 100644 --- a/js/main.js +++ b/js/main.js @@ -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 += `
`; @@ -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