From da748f911b80fb6092934639db03e1c16f007401 Mon Sep 17 00:00:00 2001 From: Lorenz H-S Date: Tue, 17 Dec 2013 19:37:45 +0000 Subject: [PATCH] Keep message/command history for each channel * Can be navigated with up/down arrow keys * Currently entered text is preserved when going into history * When choosing a history entry, previously entered but not submitted text will be restored to input buffer Closes #75 --- js/models.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++- js/websockets.js | 13 +++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/js/models.js b/js/models.js index 5aa8f17..380b414 100644 --- a/js/models.js +++ b/js/models.js @@ -20,6 +20,8 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) var lines = [] var nicklist = {} var flatnicklist = [] + var history = [] + var historyPos = 0; var active = false var notification = 0 var unread = 0 @@ -108,6 +110,50 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) return flatnicklist; } + var addToHistory = function(line) { + var result = ""; + if (historyPos != history.length) { + // Pop cached line from history. Occurs if we submit something from history + result = history.pop(); + } + history.push(line); + historyPos = history.length; // Go to end of history + return result; + } + + var getHistoryUp = function(currentLine) { + if (historyPos >= history.length) { + // cache current line in history + history.push(currentLine); + } + if (historyPos <= 0 || historyPos >= history.length) { + // Can't go up from first message or from out-of-bounds index + return currentLine; + } else { + // Go up in history + historyPos--; + var line = history[historyPos]; + return line; + } + } + + var getHistoryDown = function(currentLine) { + if (historyPos < 0 || historyPos >= history.length) { + // Can't go down from out of bounds or last message + return currentLine; + } else { + historyPos++; + + if (history.length > 0 && historyPos == (history.length-1)) { + // return cached line and remove from cache + return history.pop(); + } else { + // Go down in history + return history[historyPos]; + } + } + } + return { id: pointer, fullName: fullName, @@ -127,7 +173,11 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) updateNick: updateNick, flatNicklist: flatNicklist, serverSortKey: serverSortKey, - indent: indent + indent: indent, + history: history, + addToHistory: addToHistory, + getHistoryUp: getHistoryUp, + getHistoryDown: getHistoryDown } } diff --git a/js/websockets.js b/js/websockets.js index 2b306c4..f1205e9 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -782,7 +782,7 @@ weechat.directive('inputBar', function() { // Send the message to the websocket $scope.sendMessage = function() { connection.sendMessage($scope.command); - $scope.command = ""; + $scope.command = models.getActiveBuffer().addToHistory($scope.command); // log to buffer history }; // Handle key presses in the input bar @@ -858,6 +858,17 @@ weechat.directive('inputBar', function() { return true; } + // Arrow up -> go up in history + if (code == 38) { + $scope.command = models.getActiveBuffer().getHistoryUp($scope.command); + return true; + } + + // Arrow down -> go down in history + if (code == 40) { + $scope.command = models.getActiveBuffer().getHistoryDown($scope.command); + return true; + } }; }