From 69a3221d734fcf14f904aee30588acecacea2286 Mon Sep 17 00:00:00 2001 From: jamespatrick19656 <34253839+jamespatrick19656@users.noreply.github.com> Date: Thu, 1 Feb 2018 12:39:28 +0000 Subject: [PATCH] ability to modify previous messages added in the ability to access previous messages in the console. For example container.history.get(0); returns an instance of MessageHandle for the most recent message in the console. I added a setTest(str) method to MessageHandle so you can change the text in that particular mesg. The message handle will continue to point to the message regardless of now many new messages are added. I needed this so i could make an interactive ..... which adds a few dots per second as my client waits for the server to reply. Also connected the history into clearScreen so it is cleared accordingly. --- jquery.console.js | 52 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/jquery.console.js b/jquery.console.js index 794fc23..16bd105 100644 --- a/jquery.console.js +++ b/jquery.console.js @@ -125,7 +125,7 @@ var continuedText = ''; var fadeOnReset = config.fadeOnReset !== undefined ? config.fadeOnReset : true; // Prompt history stack - var history = []; + var commandHistory = []; var ringn = 0; // For reasons unknown to The Sword of Michael himself, Opera // triggers and sends a key character when you hit various @@ -143,6 +143,42 @@ // External exports object var extern = {}; + //Object returned on history.get(nFromLast) to allow limited modification of a previous entry. + function MessageHandle(mesg){ + this.setText=function(msg){ + mesg.filledText(msg); + }; + } + //History of mesg added to the console. + var history = new (function(){ + var self = this; + var list = []; + this.add = function(mesg){ + list.push(mesg); + }; + this.clear=function(){ + list=[]; + }; + this.get=function(nFromLast)//nFromLast is indices from last mesg added. + { + if(list.length<=0) + return; + if(nFromLast==undefined) + var index=0; + else{ + if(nFromLast<0) + nFromLast=0; + var index=list.length-(1+nFromLast); + if(index<0) + index=0; + } + return new MessageHandle(list[index]); + }; + this.getExternMethods=function(){ + return {get:self.get}; + }; + })(); + //////////////////////////////////////////////////////////////////////// // Main entry point (function(){ @@ -167,6 +203,7 @@ extern.report = report; extern.showCompletion = showCompletion; extern.clearScreen = clearScreen; + extern.history = history.getExternMethods(); })(); //////////////////////////////////////////////////////////////////////// @@ -361,15 +398,15 @@ //////////////////////////////////////////////////////////////////////// // Rotate through the command history function rotateHistory(n){ - if (history.length == 0) return; + if (commandHistory.length == 0) return; ringn += n; - if (ringn < 0) ringn = history.length; - else if (ringn > history.length) ringn = 0; + if (ringn < 0) ringn = commandHistory.length; + else if (ringn > commandHistory.length) ringn = 0; var prevText = promptText; if (ringn == 0) { promptText = restoreText; } else { - promptText = history[ringn - 1]; + promptText = commandHistory[ringn - 1]; } if (config.historyPreserveColumn) { if (promptText.length < column + 1) { @@ -393,7 +430,7 @@ // Add something to the history ring function addToHistory(line){ - history.push(line); + commandHistory.push(line); restoreText = ''; }; @@ -435,6 +472,7 @@ inner.children(".jquery-console-prompt-box, .jquery-console-message").slice(0, -1).remove(); extern.report(" "); extern.focus(); + history.clear(); }; function deleteNextWord() { @@ -590,6 +628,7 @@ if (className) mesg.addClass(className); mesg.filledText(msg).hide(); inner.append(mesg); + history.add(mesg); mesg.show(); }; @@ -832,4 +871,5 @@ $(this).focus(); window.scrollTo(x, y); }; + })(jQuery);