From 0c2e7635ed4f325cbae4618b0ddb38752f033f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sun, 31 Aug 2014 14:57:16 +0100 Subject: [PATCH] Fix Alt+[0-9] buffer access order WeeChat sends them in no particular order, we need to sort the buffers by their WeeChat number. To avoid copying the potentially very large buffer objects around needlessly, extract the relevant keys and sort, then access. This is based on https://github.com/ailin-nemui/glowing-bear/commit/ad50220bfdfcf029fc47093ce5cfea04bf90fe79 --- js/glowingbear.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 39e9f14..6ed213c 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -1529,9 +1529,19 @@ weechat.directive('inputBar', function() { } var bufferNumber = code - 48 - 1 ; - var activeBufferId = Object.keys(models.getBuffers())[bufferNumber]; + // Map the buffers to only their numbers and IDs so we don't have to + // copy the entire (possibly very large) buffer object, and then sort + // the buffers according to their WeeChat number + var sortedBuffers = _.map(models.getBuffers(), function(buffer) { + return [buffer.number, buffer.id]; + }).sort(function(left, right) { + // By default, Array.prototype.sort() sorts alphabetically. + // Pass an ordering function to sort by first element. + return left[0] - right[0]; + }); + var activeBufferId = sortedBuffers[bufferNumber]; if (activeBufferId) { - models.setActiveBuffer(activeBufferId); + models.setActiveBuffer(activeBufferId[1]); $event.preventDefault(); } }