From 9affdb894f3cff8286a5df84bbf4d9583510bbff Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 31 May 2021 19:21:54 +0200 Subject: [PATCH] Make Alt+ArrowUp and Alt+ArrowDown wrap around --- keybindings.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/keybindings.js b/keybindings.js index f46ebe5..b4d56e5 100644 --- a/keybindings.js +++ b/keybindings.js @@ -1,5 +1,15 @@ import { ReceiptType, Unread, BufferType, SERVER_BUFFER } from "./state.js"; +function getSiblingBuffer(buffers, bufID, delta) { + var bufList = Array.from(buffers.values()); + var i = bufList.findIndex((buf) => buf.id === bufID); + if (i < 0) { + return null; + } + i = (i + bufList.length + delta) % bufList.length; + return bufList[i]; +} + export const keybindings = [ { key: "h", @@ -56,15 +66,9 @@ export const keybindings = [ altKey: true, description: "Jump to the previous buffer", execute: (app) => { - var prev = null; - for (var buf of app.state.buffers.values()) { - if (app.state.activeBuffer == buf.id) { - if (prev) { - app.switchBuffer(prev); - } - break; - } - prev = buf; + var prev = getSiblingBuffer(app.state.buffers, app.state.activeBuffer, -1); + if (prev) { + app.switchBuffer(prev); } }, }, @@ -73,14 +77,9 @@ export const keybindings = [ altKey: true, description: "Jump to the next buffer", execute: (app) => { - var found = false; - for (var buf of app.state.buffers.values()) { - if (found) { - app.switchBuffer(buf); - break; - } else if (app.state.activeBuffer == buf.id) { - found = true; - } + var next = getSiblingBuffer(app.state.buffers, app.state.activeBuffer, 1); + if (next) { + app.switchBuffer(next); } }, },