From 9fbf154029ca25d74c5757c52f85af9d6df216fd Mon Sep 17 00:00:00 2001 From: Joe Xue Date: Thu, 23 Jan 2025 21:41:41 -0500 Subject: [PATCH] Split panel refs: #336 --- mux/src/tmux.rs | 59 ++++++++++++++++++++++-- mux/src/tmux_commands.rs | 96 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 143 insertions(+), 12 deletions(-) diff --git a/mux/src/tmux.rs b/mux/src/tmux.rs index 4d088b2ef..58313b5ec 100644 --- a/mux/src/tmux.rs +++ b/mux/src/tmux.rs @@ -1,13 +1,14 @@ -use crate::domain::{alloc_domain_id, Domain, DomainId, DomainState}; +use crate::domain::{alloc_domain_id, Domain, DomainId, DomainState, SplitSource}; use crate::pane::{Pane, PaneId}; -use crate::tab::{Tab, TabId}; -use crate::tmux_commands::{ListAllWindows, NewWindow, TmuxCommand}; +use crate::tab::{SplitRequest, Tab, TabId}; +use crate::tmux_commands::{ListAllWindows, NewWindow, SplitPane, TmuxCommand}; use crate::window::WindowId; use crate::{Mux, MuxWindowBuilder}; use async_trait::async_trait; use filedescriptor::FileDescriptor; use parking_lot::{Condvar, Mutex}; use portable_pty::CommandBuilder; +use smol::channel::{bounded, Receiver, Sender}; use std::collections::{HashMap, HashSet, VecDeque}; use std::io::Write; use std::sync::Arc; @@ -61,6 +62,7 @@ pub(crate) struct TmuxDomainState { pub gui_tabs: Mutex>, pub remote_panes: Mutex>, pub tmux_session: Mutex>, + pub channel: (Sender, Receiver), } pub struct TmuxDomain { @@ -133,6 +135,15 @@ impl TmuxDomainState { })); log::info!("tmux window changed: {}:{}", session, window); } + Event::WindowPaneChanged { window, pane } => { + if !self.check_pane_attached(*window, *pane) { + // new split pane + smol::block_on(async { + let _ = self.channel.0.send(*pane).await; + }); + } + log::info!("tmux window pane changed: {}:{}", window, pane); + } _ => {} } } @@ -203,6 +214,31 @@ impl TmuxDomainState { })); TmuxDomainState::schedule_send_next_command(self.domain_id); } + + /// split the tmux pane + pub fn split_tmux_pane(&self, _tab: TabId, pane_id: PaneId, split_request: SplitRequest) { + let mut target_tmux_pane_id = None; + + let mut pane_map = self.remote_panes.lock(); + for (tmux_pane_id, v) in pane_map.iter_mut() { + let remote_pane = v.lock(); + if remote_pane.local_pane_id == pane_id { + target_tmux_pane_id = Some(tmux_pane_id); + break; + } + } + + if let Some(id) = target_tmux_pane_id { + let mut cmd_queue = self.cmd_queue.as_ref().lock(); + cmd_queue.push_back(Box::new(SplitPane { + pane_id: *id, + direction: split_request.direction, + })); + TmuxDomainState::schedule_send_next_command(self.domain_id); + } else { + log::debug!("Could not find the tmux pane peer for local pane: {pane_id}"); + } + } } impl TmuxDomain { @@ -219,6 +255,7 @@ impl TmuxDomain { gui_tabs: Mutex::new(Vec::default()), remote_panes: Mutex::new(HashMap::default()), tmux_session: Mutex::new(None), + channel: bounded(1), }); Self { inner } @@ -242,6 +279,22 @@ impl Domain for TmuxDomain { anyhow::bail!("Intention: we use tmux command to do so"); } + async fn split_pane( + &self, + _source: SplitSource, + tab: TabId, + pane_id: PaneId, + split_request: SplitRequest, + ) -> anyhow::Result> { + self.inner.split_tmux_pane(tab, pane_id, split_request); + if let Ok(id) = self.inner.channel.1.recv().await { + let pane = self.inner.split_pane(tab, pane_id, id, split_request); + return pane; + } + + anyhow::bail!("Split_pane failed"); + } + async fn spawn_pane( &self, _size: TerminalSize, diff --git a/mux/src/tmux_commands.rs b/mux/src/tmux_commands.rs index 5b9c11262..01b6faef1 100644 --- a/mux/src/tmux_commands.rs +++ b/mux/src/tmux_commands.rs @@ -1,6 +1,6 @@ use crate::domain::{DomainId, WriterWrapper}; use crate::localpane::LocalPane; -use crate::pane::alloc_pane_id; +use crate::pane::{alloc_pane_id, PaneId}; use crate::tab::{SplitDirection, SplitRequest, SplitSize, Tab, TabId}; use crate::tmux::{TmuxDomain, TmuxDomainState, TmuxRemotePane, TmuxTab}; use crate::tmux_pty::{TmuxChild, TmuxPty}; @@ -55,18 +55,15 @@ struct WindowItem { impl TmuxDomainState { /// check if a PaneItem received from ListAllPanes has been attached - fn check_pane_attached(&self, target: &PaneItem) -> bool { + pub fn check_pane_attached(&self, window_id: TmuxWindowId, pane_id: TmuxPaneId) -> bool { let pane_list = self.gui_tabs.lock(); - let local_tab = match pane_list - .iter() - .find(|&x| x.tmux_window_id == target.window_id) - { + let local_tab = match pane_list.iter().find(|&x| x.tmux_window_id == window_id) { Some(x) => x, None => { return false; } }; - match local_tab.panes.get(&target.pane_id) { + match local_tab.panes.get(&pane_id) { Some(_) => { return true; } @@ -169,12 +166,73 @@ impl TmuxDomainState { ))) } + pub fn split_pane( + &self, + tab_id: TabId, + pane_id: PaneId, + remote_id: TmuxPaneId, + split_request: SplitRequest, + ) -> anyhow::Result> { + let mux = Mux::get(); + let tab = match mux.get_tab(tab_id) { + Some(t) => t, + None => anyhow::bail!("Invalid tab id {}", tab_id), + }; + + let pane_index = match tab + .iter_panes_ignoring_zoom() + .iter() + .find(|p| p.pane.pane_id() == pane_id) + { + Some(p) => p.index, + None => anyhow::bail!("invalid pane id {}", pane_id), + }; + + let split_size = match tab.compute_split_size(pane_index, split_request) { + Some(s) => s, + None => anyhow::bail!("invalid pane index {}", pane_index), + }; + + let window_id; + { + let mut pane_list = self.gui_tabs.lock(); + window_id = match pane_list.iter_mut().find(|x| x.tab_id == tab_id) { + Some(x) => x.tmux_window_id, + None => 0, + }; + } + + let p = PaneItem { + session_id: 0, + window_id: window_id, + pane_id: remote_id, + _pane_index: 0, + cursor_x: 0, + cursor_y: 0, + pane_width: split_size.second.cols as u64, + pane_height: split_size.second.rows as u64, + pane_left: 0, + pane_top: 0, + _pane_active: false, + }; + + let pane = self.create_pane(&p).unwrap(); + tab.split_and_insert(pane_index, split_request, Arc::clone(&pane))?; + + self.add_attached_pane(&p, &tab_id)?; + let _ = mux.add_pane(&pane); + + return Ok(pane); + } + fn sync_pane_state(&self, panes: &[PaneItem]) -> anyhow::Result<()> { let current_session = self.tmux_session.lock().unwrap_or(0); let mux = Mux::get(); for pane in panes.iter() { - if pane.session_id != current_session || !self.check_pane_attached(&pane) { + if pane.session_id != current_session + || !self.check_pane_attached(pane.window_id, pane.pane_id) + { continue; } @@ -257,7 +315,7 @@ impl TmuxDomainState { for p in split_stack { let local_pane; - if !self.check_pane_attached(&p) { + if !self.check_pane_attached(p.window_id, p.pane_id) { local_pane = self.create_pane(&p).unwrap(); self.add_attached_pane(&p, &tab.tab_id())?; let _ = mux.add_pane(&local_pane); @@ -776,6 +834,26 @@ impl TmuxCommand for NewWindow { } } +#[derive(Debug)] +pub(crate) struct SplitPane { + pub pane_id: TmuxPaneId, + pub direction: SplitDirection, +} + +impl TmuxCommand for SplitPane { + fn get_command(&self) -> String { + if self.direction == SplitDirection::Horizontal { + format!("split-window -h -t %{}\n", self.pane_id) + } else { + format!("split-window -v -t %{}\n", self.pane_id) + } + } + + fn process_result(&self, _domain_id: DomainId, _result: &Guarded) -> anyhow::Result<()> { + Ok(()) + } +} + #[cfg(test)] mod tests { use super::*;