From 7221c1b3f3165f05995bf100b2f2e7181e514f11 Mon Sep 17 00:00:00 2001 From: Maxim Sokolov Date: Tue, 24 Jan 2023 21:29:18 -0800 Subject: [PATCH] add lua methods to access position and pane in the direction --- lua-api-crates/mux/src/tab.rs | 19 +++++++++++++++++ mux/src/tab.rs | 39 +++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/lua-api-crates/mux/src/tab.rs b/lua-api-crates/mux/src/tab.rs index 3b98a83f3..b0e34158c 100644 --- a/lua-api-crates/mux/src/tab.rs +++ b/lua-api-crates/mux/src/tab.rs @@ -1,3 +1,5 @@ +use config::keyassignment::PaneDirection; + use super::*; use luahelper::to_lua; use std::sync::Arc; @@ -53,6 +55,23 @@ impl UserData for MuxTab { .collect::>()) }); + methods.add_method("get_pane_direction", |_, this, direction: String| { + let mux = get_mux()?; + let tab = this.resolve(&mux)?; + let panes = tab.iter_panes_ignoring_zoom(); + + let dir = PaneDirection::direction_from_str(&direction).map_err(|err| { + mlua::Error::external(format!( + "Unable to convert direction '{}'. Error: {}", + direction, err + )) + })?; + let pane = tab + .get_pane_direction(dir, true) + .map(|pane_index| MuxPane(panes[pane_index].pane.pane_id())); + Ok(pane) + }); + methods.add_method("set_zoomed", |_, this, zoomed: bool| { let mux = get_mux()?; let tab = this.resolve(&mux)?; diff --git a/mux/src/tab.rs b/mux/src/tab.rs index 5902bcaf5..199039e98 100644 --- a/mux/src/tab.rs +++ b/mux/src/tab.rs @@ -645,6 +645,14 @@ impl Tab { self.inner.lock().activate_pane_direction(direction) } + /// Returns an adjacent pane in the specified direction. + /// In cases where there are multiple adjacent panes in the + /// intended direction, we take the pane that has the largest + /// edge intersection. + pub fn get_pane_direction(&self, direction: PaneDirection, ignore_zoom: bool) -> Option { + self.inner.lock().get_pane_direction(direction, ignore_zoom) + } + pub fn prune_dead_panes(&self) -> bool { self.inner.lock().prune_dead_panes() } @@ -1408,34 +1416,42 @@ impl TabInner { } self.toggle_zoom(); } - let panes = self.iter_panes(); + if let Some(panel_idx) = self.get_pane_direction(direction, false) { + self.set_active_idx(panel_idx); + } + } + + fn get_pane_direction(&mut self, direction: PaneDirection, ignore_zoom: bool) -> Option { + let panes = if ignore_zoom { + self.iter_panes_ignoring_zoom() + } else { + self.iter_panes() + }; let active = match panes.iter().find(|pane| pane.is_active) { Some(p) => p, None => { // No active pane somehow... - self.set_active_idx(0); - return; + return Some(0); } }; if matches!(direction, PaneDirection::Next | PaneDirection::Prev) { let max_pane_id = panes.iter().map(|p| p.index).max().unwrap_or(active.index); - if direction == PaneDirection::Next { - self.set_active_idx(if active.index == max_pane_id { + return Some(if direction == PaneDirection::Next { + if active.index == max_pane_id { 0 } else { active.index + 1 - }); + } } else { - self.set_active_idx(if active.index == 0 { + if active.index == 0 { max_pane_id } else { active.index - 1 - }); - } - return; + } + }); } let mut best = None; @@ -1506,8 +1522,9 @@ impl TabInner { drop(recency); if let Some((_, target)) = best.take() { - self.set_active_idx(target.index); + return Some(target.index); } + None } fn prune_dead_panes(&mut self) -> bool {