From 4e08a52f254f01e3896a11f89f5873e438db4296 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Fri, 25 Apr 2014 15:36:31 +0200 Subject: [PATCH 1/2] Use recent speaker instead of alphabetical nick tab complete Use _ for sorting. Fix jshint problems and naming things. Fix comments --- js/glowingbear.js | 26 +++++++++++------------ js/irc-utils.js | 24 ++++++++++++++++++--- js/models.js | 54 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 54b38db..243d249 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -666,6 +666,18 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', $scope.bufferlines = ab.lines; $scope.nicklist = ab.nicklist; + // Send a request for the nicklist if it hasn't been loaded yet + if (!ab.nicklistRequested()) { + connection.requestNicklist(ab.fullName, function() { + $scope.showNicklist = $scope.updateShowNicklist(); + // Scroll after nicklist has been loaded, as it may break long lines + $rootScope.scrollWithBuffer(true); + }); + } else { + // Check if we should show nicklist or not + $scope.showNicklist = $scope.updateShowNicklist(); + } + if (ab.requestedLines < $scope.lines) { // buffer has not been loaded, but some lines may already be present if they arrived after we connected // try to determine how many lines to fetch @@ -679,18 +691,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', } $rootScope.updateTitle(ab); - // Send a request for the nicklist if it hasn't been loaded yet - if (!ab.nicklistRequested()) { - connection.requestNicklist(ab.fullName, function() { - $scope.showNicklist = $scope.updateShowNicklist(); - // Scroll after nicklist has been loaded, as it may break long lines - $rootScope.scrollWithBuffer(true); - }); - } else { - // Check if we should show nicklist or not - $scope.showNicklist = $scope.updateShowNicklist(); - } - $rootScope.scrollWithBuffer(true); // If user wants to sync hotlist with weechat @@ -1235,7 +1235,7 @@ weechat.directive('inputBar', function() { // complete nick var nickComp = IrcUtils.completeNick(inputText, caretPos, - $scope.iterCandidate, activeBuffer.flatNicklist(), ':'); + $scope.iterCandidate, activeBuffer.getNicklistByTime(), ':'); // remember iteration candidate $scope.iterCandidate = nickComp.iterCandidate; diff --git a/js/irc-utils.js b/js/irc-utils.js index ff4c488..f608317 100644 --- a/js/irc-utils.js +++ b/js/irc-utils.js @@ -21,6 +21,21 @@ var IrcUtils = { return newList; }, + /** + * Get a new version of a nick list, sorted by last speaker + * + * @param nickList Original nick list + * @return Sorted nick list + */ + _ciNickList: function(nickList) { + + var newList = _(nickList).sortBy(function(nickObj) { + return -nickObj.spokeAt; + }); + newList = _(newList).pluck('name'); + + return newList; + }, /** * Completes a single nick. @@ -66,10 +81,13 @@ var IrcUtils = { if (lcCurrentNick === lcNick) { at = matchingNicks.length - 1; } - } else if (matchingNicks.length > 0) { + } + /* Since we aren't sorted any more torhve disabled this: + else if (matchingNicks.length > 0) { // end of group, no need to check after this - break; + //break; } + */ } if (at === null || matchingNicks.length === 0) { @@ -105,7 +123,7 @@ var IrcUtils = { } // new nick list to search in - var searchNickList = IrcUtils._ciSearchNickList(nickList); + var searchNickList = IrcUtils._ciNickList(nickList); // text before and after caret var beforeCaret = text.substring(0, caretPos); diff --git a/js/models.js b/js/models.js index 818695e..b5ab4e8 100644 --- a/js/models.js +++ b/js/models.js @@ -43,6 +43,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) */ var addLine = function(line) { lines.push(line); + updateNickSpeak(line); }; /* @@ -50,6 +51,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) */ var addNick = function(group, nick) { if (nicklistRequested()) { + nick.spokeAt = Date.now(); nicklist[group].nicks.push(nick); flatnicklist = getFlatNicklist(); } @@ -88,9 +90,55 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) }; /* - * Maintain a cached version of a flat sorted nicklist + * Update a nick with a fresh timestamp so tab completion + * can use time to complete recent speakers + */ + var updateNickSpeak = function(line) { + // Try to find nick from prefix + var prefix = line.prefix; + var nick = prefix[prefix.length - 1].text; + // Action / me, find the nick as the first word of the message + if (nick === " *") { + var match = line.text.match(/^(.+)\s/); + if (match) { + nick = match[1]; + } + } + else if (nick === "" || nick === "=!=") { + return; + } + _.each(nicklist, function(nickGroup) { + _.each(nickGroup.nicks, function(nickObj) { + if (nickObj.name === nick) { + // Use the order the line arrive in for simplicity + // instead of using weechat's own timestamp + nickObj.spokeAt = Date.now(); + } + }); + }); + flatnicklist = getFlatNicklist(); + }; + + /* + * Get a flat nicklist sorted by speaker time. This function is + * called for every tab key press by the user. * */ + var getNicklistByTime = function() { + var newlist = []; + _.each(nicklist, function(nickGroup) { + _.each(nickGroup.nicks, function(nickObj) { + newlist.push(nickObj); + }); + }); + + newlist.sort(function(a, b) { + return a.spokeAt < b.spokeAt; + }); + + return newlist; + }; + var getFlatNicklist = function() { var newlist = []; _.each(nicklist, function(nickGroup) { @@ -98,9 +146,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) newlist.push(nickObj.name); }); }); - newlist.sort(function(a, b) { - return a.toLowerCase() < b.toLowerCase() ? -1 : 1; - }); return newlist; }; @@ -190,6 +235,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) delNick: delNick, updateNick: updateNick, flatNicklist: flatNicklist, + getNicklistByTime: getNicklistByTime, serverSortKey: serverSortKey, indent: indent, history: history, From 921e28d0e2bf51fe87428bd99709e1f3ff2ed712 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Fri, 25 Apr 2014 21:05:20 +0200 Subject: [PATCH 2/2] Remove the old and now unused flat nicklist --- js/glowingbear.js | 4 ++-- js/irc-utils.js | 18 ------------------ js/models.js | 20 -------------------- 3 files changed, 2 insertions(+), 40 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 243d249..5a42194 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -868,7 +868,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', // get last word var lastSpace = trimmedValue.lastIndexOf(' ') + 1; var lastWord = trimmedValue.slice(lastSpace, trimmedValue.length - 1); - var nicklist = models.getActiveBuffer().flatNicklist(); + var nicklist = models.getActiveBuffer().getNicklistByTime(); // check against nicklist to see if it's a list of highlights if (nicklist.indexOf(lastWord) !== -1) { // It's another highlight! @@ -1230,7 +1230,7 @@ weechat.directive('inputBar', function() { // get current caret position var caretPos = inputNode.selectionStart; - // create flat array of nicks + // get current active buffer var activeBuffer = models.getActiveBuffer(); // complete nick diff --git a/js/irc-utils.js b/js/irc-utils.js index f608317..11e2465 100644 --- a/js/irc-utils.js +++ b/js/irc-utils.js @@ -3,24 +3,6 @@ */ var IrcUtils = { - /** - * Get a new version of a nick list, sorted alphabetically by lowercase nick. - * - * @param nickList Original nick list - * @return Nick list sorted alphabetically by lowercase nick - */ - _ciSearchNickList: function(nickList) { - var newList = []; - - nickList.forEach(function(nick) { - newList.push(nick); - }); - newList.sort(function(a, b) { - return a.toLowerCase() < b.toLowerCase() ? -1 : 1; - }); - - return newList; - }, /** * Get a new version of a nick list, sorted by last speaker * diff --git a/js/models.js b/js/models.js index b5ab4e8..08af87d 100644 --- a/js/models.js +++ b/js/models.js @@ -20,7 +20,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) var lines = []; var requestedLines = 0; var nicklist = {}; - var flatnicklist = []; var history = []; var historyPos = 0; var active = false; @@ -53,7 +52,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) if (nicklistRequested()) { nick.spokeAt = Date.now(); nicklist[group].nicks.push(nick); - flatnicklist = getFlatNicklist(); } }; /* @@ -65,7 +63,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) return; } group.nicks = _.filter(group.nicks, function(n) { return n.name !== nick.name;}); - flatnicklist = getFlatNicklist(); /* for (i in group.nicks) { if (group.nicks[i].name == nick.name) { @@ -86,7 +83,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) break; } } - flatnicklist = getFlatNicklist(); }; /* @@ -116,7 +112,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) } }); }); - flatnicklist = getFlatNicklist(); }; /* @@ -139,20 +134,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) return newlist; }; - var getFlatNicklist = function() { - var newlist = []; - _.each(nicklist, function(nickGroup) { - _.each(nickGroup.nicks, function(nickObj) { - newlist.push(nickObj.name); - }); - }); - return newlist; - }; - - var flatNicklist = function() { - return flatnicklist; - }; - var addToHistory = function(line) { var result = ""; if (historyPos !== history.length) { @@ -234,7 +215,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) addNick: addNick, delNick: delNick, updateNick: updateNick, - flatNicklist: flatNicklist, getNicklistByTime: getNicklistByTime, serverSortKey: serverSortKey, indent: indent,