From 8e04f49d03f8b2eb1e00cfb60355b8bdb47c0faa Mon Sep 17 00:00:00 2001 From: g4c Date: Mon, 23 Aug 2021 20:48:09 +0800 Subject: [PATCH] split out tmux commands --- Cargo.lock | 2 + mux/src/lib.rs | 1 + mux/src/tmux.rs | 98 ++-------------------------------------- mux/src/tmux_commands.rs | 94 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 95 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6bec78a8..ef4c879fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2297,6 +2297,7 @@ dependencies = [ "crossbeam", "downcast-rs", "filedescriptor", + "flume", "hostname", "k9", "lazy_static", @@ -4111,6 +4112,7 @@ dependencies = [ "terminfo", "termios 0.3.3", "thiserror", + "tmux-cc", "ucd-trie", "unicode-segmentation", "varbincode", diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 818942a3b..744dad914 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -38,6 +38,7 @@ pub mod ssh; pub mod tab; pub mod termwiztermtab; pub mod tmux; +pub mod tmux_commands; pub mod window; use crate::activity::Activity; diff --git a/mux/src/tmux.rs b/mux/src/tmux.rs index c841daa7c..0fcfd2133 100644 --- a/mux/src/tmux.rs +++ b/mux/src/tmux.rs @@ -1,7 +1,9 @@ use crate::domain::{alloc_domain_id, Domain, DomainId, DomainState}; use crate::pane::{Pane, PaneId}; +use crate::tab::{SplitDirection, Tab, TabId}; +use crate::tmux_commands::{ListAllPanes, TmuxCommand}; +use crate::window::WindowId; use crate::Mux; -use anyhow::anyhow; use async_trait::async_trait; use flume; use portable_pty::{CommandBuilder, PtySize}; @@ -18,100 +20,6 @@ enum State { WaitingForResponse, } -trait TmuxCommand { - fn get_command(&self) -> String; - fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()>; -} - -struct ListAllPanes; -impl TmuxCommand for ListAllPanes { - fn get_command(&self) -> String { - "list-panes -aF '#{session_id} #{window_id} #{pane_id} \ - #{pane_index} #{cursor_x} #{cursor_y} #{pane_width} #{pane_height} \ - #{pane_left} #{pane_top}'\n" - .to_owned() - } - - fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> { - #[derive(Debug)] - #[allow(dead_code)] - struct Item { - session_id: TmuxSessionId, - window_id: TmuxWindowId, - pane_id: TmuxPaneId, - pane_index: u64, - cursor_x: u64, - cursor_y: u64, - pane_width: u64, - pane_height: u64, - pane_left: u64, - pane_top: u64, - } - - let mut items = vec![]; - - for line in result.output.split('\n') { - if line.is_empty() { - continue; - } - let mut fields = line.split(' '); - let session_id = fields.next().ok_or_else(|| anyhow!("missing session_id"))?; - let window_id = fields.next().ok_or_else(|| anyhow!("missing window_id"))?; - let pane_id = fields.next().ok_or_else(|| anyhow!("missing pane_id"))?; - let pane_index = fields - .next() - .ok_or_else(|| anyhow!("missing pane_index"))? - .parse()?; - let cursor_x = fields - .next() - .ok_or_else(|| anyhow!("missing cursor_x"))? - .parse()?; - let cursor_y = fields - .next() - .ok_or_else(|| anyhow!("missing cursor_y"))? - .parse()?; - let pane_width = fields - .next() - .ok_or_else(|| anyhow!("missing pane_width"))? - .parse()?; - let pane_height = fields - .next() - .ok_or_else(|| anyhow!("missing pane_height"))? - .parse()?; - let pane_left = fields - .next() - .ok_or_else(|| anyhow!("missing pane_left"))? - .parse()?; - let pane_top = fields - .next() - .ok_or_else(|| anyhow!("missing pane_top"))? - .parse()?; - - // These ids all have various sigils such as `$`, `%`, `@`, - // so skip those prior to parsing them - let session_id = session_id[1..].parse()?; - let window_id = window_id[1..].parse()?; - let pane_id = pane_id[1..].parse()?; - - items.push(Item { - session_id, - window_id, - pane_id, - pane_index, - cursor_x, - cursor_y, - pane_width, - pane_height, - pane_left, - pane_top, - }); - } - - log::error!("panes in domain_id {}: {:?}", domain_id, items); - Ok(()) - } -} - struct TmuxRemotePane { // members for local local_pane_id: PaneId, diff --git a/mux/src/tmux_commands.rs b/mux/src/tmux_commands.rs index e69de29bb..1014090e2 100644 --- a/mux/src/tmux_commands.rs +++ b/mux/src/tmux_commands.rs @@ -0,0 +1,94 @@ +use anyhow::anyhow; + +pub(crate) trait TmuxCommand { + fn get_command(&self) -> String; + fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()>; +} + +pub(crate) struct ListAllPanes; +impl TmuxCommand for ListAllPanes { + fn get_command(&self) -> String { + "list-panes -aF '#{session_id} #{window_id} #{pane_id} \ + #{pane_index} #{cursor_x} #{cursor_y} #{pane_width} #{pane_height} \ + #{pane_left} #{pane_top}'\n" + .to_owned() + } + + fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> { + #[derive(Debug)] + struct Item { + session_id: TmuxSessionId, + window_id: TmuxWindowId, + pane_id: TmuxPaneId, + pane_index: u64, + cursor_x: u64, + cursor_y: u64, + pane_width: u64, + pane_height: u64, + pane_left: u64, + pane_top: u64, + } + + let mut items = vec![]; + + for line in result.output.split('\n') { + if line.is_empty() { + continue; + } + let mut fields = line.split(' '); + let session_id = fields.next().ok_or_else(|| anyhow!("missing session_id"))?; + let window_id = fields.next().ok_or_else(|| anyhow!("missing window_id"))?; + let pane_id = fields.next().ok_or_else(|| anyhow!("missing pane_id"))?; + let pane_index = fields + .next() + .ok_or_else(|| anyhow!("missing pane_index"))? + .parse()?; + let cursor_x = fields + .next() + .ok_or_else(|| anyhow!("missing cursor_x"))? + .parse()?; + let cursor_y = fields + .next() + .ok_or_else(|| anyhow!("missing cursor_y"))? + .parse()?; + let pane_width = fields + .next() + .ok_or_else(|| anyhow!("missing pane_width"))? + .parse()?; + let pane_height = fields + .next() + .ok_or_else(|| anyhow!("missing pane_height"))? + .parse()?; + let pane_left = fields + .next() + .ok_or_else(|| anyhow!("missing pane_left"))? + .parse()?; + let pane_top = fields + .next() + .ok_or_else(|| anyhow!("missing pane_top"))? + .parse()?; + + // These ids all have various sigils such as `$`, `%`, `@`, + // so skip those prior to parsing them + let session_id = session_id[1..].parse()?; + let window_id = window_id[1..].parse()?; + let pane_id = pane_id[1..].parse()?; + + items.push(Item { + session_id, + window_id, + pane_id, + pane_index, + cursor_x, + cursor_y, + pane_width, + pane_height, + pane_left, + pane_top, + }); + } + + log::error!("panes in domain_id {}: {:?}", domain_id, items); + Ok(()) + } +}