diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 744dad914..93eb84e55 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -39,6 +39,7 @@ pub mod tab; pub mod termwiztermtab; pub mod tmux; pub mod tmux_commands; +mod tmux_pty; pub mod window; use crate::activity::Activity; diff --git a/mux/src/tmux.rs b/mux/src/tmux.rs index 417237e97..36583420a 100644 --- a/mux/src/tmux.rs +++ b/mux/src/tmux.rs @@ -1,7 +1,7 @@ use crate::domain::{alloc_domain_id, Domain, DomainId, DomainState}; use crate::pane::{Pane, PaneId}; use crate::tab::{SplitDirection, Tab, TabId}; -use crate::tmux_commands::{process_command_result, ListAllPanes, TmuxCommand}; +use crate::tmux_commands::{ListAllPanes, TmuxCommand}; use crate::window::WindowId; use crate::Mux; use async_trait::async_trait; @@ -10,7 +10,7 @@ use portable_pty::{CommandBuilder, PtySize}; use std::cell::RefCell; use std::collections::{HashMap, VecDeque}; use std::rc::Rc; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use tmux_cc::*; #[derive(PartialEq, Eq, Debug, Copy, Clone)] @@ -20,7 +20,7 @@ enum State { WaitingForResponse, } -struct TmuxRemotePane { +pub(crate) struct TmuxRemotePane { // members for local local_pane_id: PaneId, tx: flume::Sender, @@ -28,6 +28,20 @@ struct TmuxRemotePane { session_id: TmuxSessionId, window_id: TmuxWindowId, pane_id: TmuxPaneId, + pub cursor_x: u64, + pub cursor_y: u64, + pub pane_width: u64, + pub pane_height: u64, + pub pane_left: u64, + pub pane_top: u64, +} + +pub(crate) type RefTmuxRemotePane = Arc>; + +pub(crate) struct TmuxTab { + tab_id: TabId, + tmux_window_id: TmuxWindowId, + panes: Vec, } pub(crate) struct TmuxDomainState { @@ -37,9 +51,8 @@ pub(crate) struct TmuxDomainState { state: RefCell, cmd_queue: RefCell>>, gui_window_id: RefCell>, - remote_panes: RefCell>, - // TODO: Currently, a tmux pane is a local tab. - // add info about window-pane mapping to support this feature. + gui_tabs: RefCell>, + remote_panes: RefCell>, } pub struct TmuxDomain { @@ -62,11 +75,8 @@ impl TmuxDomainState { *self.state.borrow_mut() = State::Idle; let resp = response.clone(); promise::spawn::spawn(async move { - match cmd.process_result(domain_id, &resp) { - Ok(result) => process_command_result(domain_id, result), - Err(err) => { - log::error!("error processing result: {}", err); - } + if let Err(err) = cmd.process_result(domain_id, &resp) { + log::error!("error processing result: {}", err); } }) .detach(); @@ -77,7 +87,7 @@ impl TmuxDomainState { // TODO: write to pane's tmux queue // TODO: decode string } - Event::WindowAdd { window } => { + Event::WindowAdd { window: _ } => { if self.gui_window_id.borrow().is_none() { if let Some(mux) = Mux::get() { let window_builder = mux.new_empty_window(); @@ -138,6 +148,7 @@ impl TmuxDomain { state: RefCell::new(State::WaitForInitialGuard), cmd_queue: RefCell::new(cmd_queue), gui_window_id: RefCell::new(None), + gui_tabs: RefCell::new(Vec::default()), remote_panes: RefCell::new(HashMap::default()), }); Self { inner } diff --git a/mux/src/tmux_commands.rs b/mux/src/tmux_commands.rs index 42195aa99..764ac970b 100644 --- a/mux/src/tmux_commands.rs +++ b/mux/src/tmux_commands.rs @@ -1,16 +1,12 @@ use crate::domain::DomainId; -use crate::tmux::TmuxDomain; +use crate::tmux::{TmuxDomain, TmuxDomainState}; use crate::Mux; use anyhow::anyhow; use tmux_cc::*; pub(crate) trait TmuxCommand { fn get_command(&self) -> String; - fn process_result( - &self, - domain_id: DomainId, - result: &Guarded, - ) -> anyhow::Result; + fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()>; } #[derive(Debug)] @@ -27,6 +23,12 @@ pub(crate) struct PaneItem { pane_top: u64, } +impl TmuxDomainState { + fn sync_pane_state(&self, panes: &[PaneItem]) -> anyhow::Result<()> { + Ok(()) + } +} + pub(crate) enum TmuxCommandResult { PaneList(Vec), } @@ -40,11 +42,7 @@ impl TmuxCommand for ListAllPanes { .to_owned() } - fn process_result( - &self, - domain_id: DomainId, - result: &Guarded, - ) -> anyhow::Result { + fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> { let mut items = vec![]; for line in result.output.split('\n') { @@ -104,16 +102,13 @@ impl TmuxCommand for ListAllPanes { }); } - log::error!("panes in domain_id {}: {:?}", domain_id, items); - Ok(TmuxCommandResult::PaneList(items)) - } -} - -pub(crate) fn process_command_result(domain_id: DomainId, result: TmuxCommandResult) { - let mux = Mux::get().expect("to be called on main thread"); - if let Some(domain) = mux.get_domain(domain_id) { - if let Some(tmux_domain) = domain.downcast_ref::() { - // TODO: + log::info!("panes in domain_id {}: {:?}", domain_id, items); + let mux = Mux::get().expect("to be called on main thread"); + if let Some(domain) = mux.get_domain(domain_id) { + if let Some(tmux_domain) = domain.downcast_ref::() { + return tmux_domain.inner.sync_pane_state(&items); + } } + anyhow::bail!("Tmux domain lost"); } } diff --git a/mux/src/tmux_pty.rs b/mux/src/tmux_pty.rs new file mode 100644 index 000000000..e165188e2 --- /dev/null +++ b/mux/src/tmux_pty.rs @@ -0,0 +1,74 @@ +use flume; +use portable_pty::MasterPty; +use std::io::{Read, Write}; + +use crate::tmux::RefTmuxRemotePane; + +pub(crate) struct TmuxReader { + rx: flume::Receiver, +} + +impl Read for TmuxReader { + fn read(&mut self, mut buf: &mut [u8]) -> std::io::Result { + match self.rx.try_recv() { + Ok(str) => { + return buf.write(str.as_bytes()); + } + Err(_) => { + return Ok(0); + } + } + } +} + +// A local tmux pane(tab) based on a tmux pty +pub(crate) struct TmuxPty { + master_pane: RefTmuxRemotePane, + rx: flume::Receiver, + // TODO: wx +} + +impl Write for TmuxPty { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + // TODO: write to wx of pty + Ok(0) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +impl MasterPty for TmuxPty { + fn resize(&self, size: portable_pty::PtySize) -> Result<(), anyhow::Error> { + // TODO: perform pane resize + Ok(()) + } + + fn get_size(&self) -> Result { + let pane = self.master_pane.lock().unwrap(); + Ok(portable_pty::PtySize { + rows: pane.pane_height as u16, + cols: pane.pane_width as u16, + pixel_width: 0, + pixel_height: 0, + }) + } + + fn try_clone_reader(&self) -> Result, anyhow::Error> { + Ok(Box::new(TmuxReader { + rx: self.rx.clone(), + })) + } + + fn try_clone_writer(&self) -> Result, anyhow::Error> { + Ok(Box::new(TmuxPty { + master_pane: self.master_pane.clone(), + rx: self.rx.clone(), + })) + } + + fn process_group_leader(&self) -> Option { + return None; + } +}