Skip to content

Commit

Permalink
Allow a count prefix for closeTabsOnLeft and closeTabsOnRight
Browse files Browse the repository at this point in the history
  • Loading branch information
philc committed Aug 21, 2023
1 parent 67a7485 commit eaa687d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions background_scripts/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ const commandDescriptions = {
togglePinTab: ["Pin or unpin current tab", { background: true }],
toggleMuteTab: ["Mute or unmute current tab", { background: true, noRepeat: true }],

closeTabsOnLeft: ["Close tabs on the left", { background: true, noRepeat: true }],
closeTabsOnRight: ["Close tabs on the right", { background: true, noRepeat: true }],
closeTabsOnLeft: ["Close tabs on the left", { background: true }],
closeTabsOnRight: ["Close tabs on the right", { background: true }],
closeOtherTabs: ["Close all other tabs", { background: true, noRepeat: true }],

moveTabLeft: ["Move tab to the left", { background: true }],
Expand Down
14 changes: 11 additions & 3 deletions background_scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,24 @@ var forCountTabs = (count, currentTab, callback) =>

// Remove tabs before, after, or either side of the currently active tab
const removeTabsRelative = async (direction, { count, tab }) => {
// count is null if the user didn't type a count prefix before issuing this command and didn't
// specify a count=n option in their keymapping settings. Interpret this as closing all tabs on
// either side.
if (count == null) count = 99999;
const activeTab = tab;
const tabs = await chrome.tabs.query({ currentWindow: true });
const toRemove = tabs.filter((tab) => {
if (tab.pinned || tab.id == activeTab.id) return false;
if (tab.pinned || tab.id == activeTab.id) {
return false;
}
switch (direction) {
case "before":
return tab.index < activeTab.index;
return tab.index < activeTab.index &&
tab.index >= activeTab.index - count;
break;
case "after":
return tab.index > activeTab.index;
return tab.index > activeTab.index &&
tab.index <= activeTab.index + count;
break;
case "both":
return true;
Expand Down
13 changes: 11 additions & 2 deletions content_scripts/mode_normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ class NormalMode extends KeyHandlerMode {
}

commandHandler({ command: registryEntry, count }) {
count *= registryEntry.options.count != null ? registryEntry.options.count : 1;
if (registryEntry.options.count) {
count = (count ?? 1) * registryEntry.options.count;
}

// closeTabsOnLeft and closeTabsOnRight interpret a null count as "close all tabs in
// {direction}", so don't default the count to 1 for those commands. See #2971.
const allowNullCount = ["closeTabsOnLeft", "closeTabsOnRight"].includes(registryEntry.command);
if (!allowNullCount && count == null) {
count = 1;
}

if (registryEntry.noRepeat) {
if (registryEntry.noRepeat && count) {
count = 1;
}

Expand Down

0 comments on commit eaa687d

Please sign in to comment.