From 18dd3e90bca4d3cb449cb4e2ac10b40e609c5545 Mon Sep 17 00:00:00 2001 From: g4c Date: Mon, 23 Aug 2021 20:44:49 +0800 Subject: [PATCH] tmux domain handle events --- mux/Cargo.toml | 1 + mux/src/tmux.rs | 56 +++++++++++++++++++++++++++++++++------- mux/src/tmux_commands.rs | 0 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 mux/src/tmux_commands.rs diff --git a/mux/Cargo.toml b/mux/Cargo.toml index 27cadd372..aff652ddd 100644 --- a/mux/Cargo.toml +++ b/mux/Cargo.toml @@ -42,6 +42,7 @@ unicode-segmentation = "1.8" url = "2" wezterm-ssh = { path = "../wezterm-ssh" } wezterm-term = { path = "../term", features=["use_serde"] } +flume = "0.10" [target."cfg(windows)".dependencies] ntapi = "0.3" diff --git a/mux/src/tmux.rs b/mux/src/tmux.rs index 689649ca2..c841daa7c 100644 --- a/mux/src/tmux.rs +++ b/mux/src/tmux.rs @@ -3,9 +3,10 @@ use crate::pane::{Pane, PaneId}; use crate::Mux; use anyhow::anyhow; use async_trait::async_trait; +use flume; use portable_pty::{CommandBuilder, PtySize}; use std::cell::RefCell; -use std::collections::VecDeque; +use std::collections::{HashMap, VecDeque}; use std::rc::Rc; use std::sync::Arc; use tmux_cc::*; @@ -111,12 +112,26 @@ impl TmuxCommand for ListAllPanes { } } +struct TmuxRemotePane { + // members for local + local_pane_id: PaneId, + tx: flume::Sender, + // members sync with remote + session_id: TmuxSessionId, + window_id: TmuxWindowId, + pane_id: TmuxPaneId, +} + pub(crate) struct TmuxDomainState { pane_id: PaneId, pub domain_id: DomainId, - parser: RefCell, + // parser: RefCell, 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. } pub struct TmuxDomain { @@ -124,13 +139,12 @@ pub struct TmuxDomain { } impl TmuxDomainState { - pub fn advance(&self, b: u8) { - let mut parser = self.parser.borrow_mut(); - if let Some(event) = parser.advance_byte(b) { + pub fn advance(&self, events: Box>) { + for event in events.iter() { let state = *self.state.borrow(); log::error!("tmux: {:?} in state {:?}", event, state); - if let Event::Guarded(response) = event { - match state { + match event { + Event::Guarded(response) => match state { State::WaitForInitialGuard => { *self.state.borrow_mut() = State::Idle; } @@ -138,17 +152,37 @@ impl TmuxDomainState { let cmd = self.cmd_queue.borrow_mut().pop_front().unwrap(); let domain_id = self.domain_id; *self.state.borrow_mut() = State::Idle; + let resp = response.clone(); promise::spawn::spawn(async move { - if let Err(err) = cmd.process_result(domain_id, &response) { + if let Err(err) = cmd.process_result(domain_id, &resp) { log::error!("error processing result: {}", err); } }) .detach(); } State::Idle => {} + }, + Event::Output { pane, text } => { + // TODO: write to pane's tmux queue + // TODO: decode string } + Event::WindowAdd { window } => { + if self.gui_window_id.borrow().is_none() { + if let Some(mux) = Mux::get() { + let window_builder = mux.new_empty_window(); + log::info!("Tmux create window id {}", window_builder.window_id); + { + let mut window_id = self.gui_window_id.borrow_mut(); + *window_id = Some(window_builder.window_id); + } + } + } + } + _ => {} } } + + // send pending commands to tmux if *self.state.borrow() == State::Idle && !self.cmd_queue.borrow().is_empty() { let domain_id = self.domain_id; promise::spawn::spawn(async move { @@ -183,15 +217,17 @@ impl TmuxDomainState { impl TmuxDomain { pub fn new(pane_id: PaneId) -> Self { let domain_id = alloc_domain_id(); - let parser = RefCell::new(Parser::new()); + // let parser = RefCell::new(Parser::new()); let mut cmd_queue = VecDeque::>::new(); cmd_queue.push_back(Box::new(ListAllPanes)); let inner = Arc::new(TmuxDomainState { domain_id, pane_id, - parser, + // parser, state: RefCell::new(State::WaitForInitialGuard), cmd_queue: RefCell::new(cmd_queue), + gui_window_id: RefCell::new(None), + remote_panes: RefCell::new(HashMap::default()), }); Self { inner } } diff --git a/mux/src/tmux_commands.rs b/mux/src/tmux_commands.rs new file mode 100644 index 000000000..e69de29bb