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
This commit is contained in:
Lorenz H-S 2013-12-17 19:37:45 +00:00
parent dc0b4c1a73
commit da748f911b
2 changed files with 63 additions and 2 deletions

View file

@ -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
}
}

View file

@ -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;
}
};
}