From 13d374ec32216881d08fee23c67f0ecb3486d9ea Mon Sep 17 00:00:00 2001 From: Carlos <28845529+mesacarlos@users.noreply.github.com> Date: Fri, 23 Aug 2019 14:56:10 +0200 Subject: [PATCH] Added command history into client --- client/scripts/WebConsole.js | 5 +++++ client/scripts/WebConsoleConnector.js | 1 + client/scripts/WebConsoleJqueryHandler.js | 22 ++++++++++++++++--- client/scripts/WebConsoleManager.js | 1 + .../webconsole/websockets/WSServer.java | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/client/scripts/WebConsole.js b/client/scripts/WebConsole.js index 6ecb9bb..29d77e7 100644 --- a/client/scripts/WebConsole.js +++ b/client/scripts/WebConsole.js @@ -11,6 +11,7 @@ var persistenceManager = new WebConsolePersistenceManager(); var connectionManager = new WebConsoleManager(); var autoPasswordCompleted = false; //When true, saved password was used. If a 401 is received, then saved password is not correct var statusCommandsInterval = -1; +var commandHistoryIndex = -1; //Saves current command history index. -1 when not browsing history. /** * Prepare and show server to user @@ -26,6 +27,7 @@ function openServer(serverName){ //New server, new variables: autoPasswordCompleted = false; + commandHistoryIndex = -1; //Reset command history index //Create or retrieve connection connectionManager.loadConnection(serverName); @@ -206,6 +208,9 @@ function backToHomepage(){ //Stop gathering info from server clearInterval(statusCommandsInterval); statusCommandsInterval = -1; + + //Reset command history index + commandHistoryIndex = -1; //Clear all server indicators writePlayerInfo(0, 0); diff --git a/client/scripts/WebConsoleConnector.js b/client/scripts/WebConsoleConnector.js index e9a3b8f..b41e27a 100644 --- a/client/scripts/WebConsoleConnector.js +++ b/client/scripts/WebConsoleConnector.js @@ -11,6 +11,7 @@ class WebConsoleConnector { this.serverURI = serverURI; this.subscribers = []; //List of functions called when a new message arrive this.messages = []; //All messages retrieved since connection start + this.commands = []; //EXEC Commands sent by user to this server } /** diff --git a/client/scripts/WebConsoleJqueryHandler.js b/client/scripts/WebConsoleJqueryHandler.js index 1297993..8730513 100644 --- a/client/scripts/WebConsoleJqueryHandler.js +++ b/client/scripts/WebConsoleJqueryHandler.js @@ -88,13 +88,14 @@ $('#passwordModal').on('hidden.bs.modal', function (e) { $("#sendCommandButton").click(function() { connectionManager.sendConsoleCmd($("#commandInput").val()); $("#commandInput").val(''); + commandHistoryIndex = -1; //Reset command history index }); /** -* Enter key on command input +* Enter or arrow up key on command input */ -$("#commandInput").on('keypress', function (e) { - if(e.which === 13){ +$("#commandInput").on('keydown', function (e) { + if(e.which === 13){ //Detect enter key //Disable textbox to prevent multiple submit $(this).attr("disabled", "disabled"); @@ -106,6 +107,21 @@ $("#commandInput").on('keypress', function (e) { //Focus again $(this).focus(); + }else if(e.which === 38){ //Detect arrow up key + //Replace with older command + if(commandHistoryIndex == -1){ + //If not browsing history, start by latest command sent + commandHistoryIndex = connectionManager.activeConnection.commands.length; + } + $("#commandInput").val(connectionManager.activeConnection.commands[commandHistoryIndex - 1]); + commandHistoryIndex = commandHistoryIndex - 1; + }else if(e.which === 40){ //Detect arrow down key + //Replace with newer command + if(commandHistoryIndex !== -1){ + //If not browsing history, do nothing + $("#commandInput").val(connectionManager.activeConnection.commands[commandHistoryIndex + 1]); + commandHistoryIndex = commandHistoryIndex + 1; + } } }); diff --git a/client/scripts/WebConsoleManager.js b/client/scripts/WebConsoleManager.js index f02cdc2..bc6900d 100644 --- a/client/scripts/WebConsoleManager.js +++ b/client/scripts/WebConsoleManager.js @@ -68,6 +68,7 @@ class WebConsoleManager { */ sendConsoleCmd(cmd){ this.activeConnection.sendToServer("EXEC " + cmd); + this.activeConnection.commands.push(cmd); } /** diff --git a/src/com/mesacarlos/webconsole/websockets/WSServer.java b/src/com/mesacarlos/webconsole/websockets/WSServer.java index 18a3332..c276d4c 100644 --- a/src/com/mesacarlos/webconsole/websockets/WSServer.java +++ b/src/com/mesacarlos/webconsole/websockets/WSServer.java @@ -68,7 +68,7 @@ public class WSServer extends WebSocketServer { public void onClose(WebSocket conn, int code, String reason, boolean remote) { LoginManager.getInstance().logOut(conn.getRemoteSocketAddress()); Bukkit.getLogger() - .info("[WebConsole] Closed WS connection " + conn.getRemoteSocketAddress() + ". Reason: " + reason); + .info("[WebConsole] Closed WS connection " + conn.getRemoteSocketAddress()); } @Override