This commit is contained in:
g4c authored and Wez Furlong committed 2022-01-17 13:08:14 -07:00
1 parent 007a0c1b27
commit b0ff7707aa
4 files changed
+114 -33

No files matched your search

+1
View File
@@ -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;
+23 -12
View File
@@ -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<String>,
@@ -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<Mutex<TmuxRemotePane>>;
pub(crate) struct TmuxTab {
tab_id: TabId,
tmux_window_id: TmuxWindowId,
panes: Vec<TmuxPaneId>,
}
pub(crate) struct TmuxDomainState {
@@ -37,9 +51,8 @@ pub(crate) struct TmuxDomainState {
state: RefCell<State>,
cmd_queue: RefCell<VecDeque<Box<dyn TmuxCommand>>>,
gui_window_id: RefCell<Option<usize>>,
remote_panes: RefCell<HashMap<TmuxPaneId, TmuxRemotePane>>,
// TODO: Currently, a tmux pane is a local tab.
// add info about window-pane mapping to support this feature.
gui_tabs: RefCell<Vec<TmuxTab>>,
remote_panes: RefCell<HashMap<TmuxPaneId, RefTmuxRemotePane>>,
}
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 }
+16 -21
View File
@@ -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<TmuxCommandResult>;
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<PaneItem>),
}
@@ -40,11 +42,7 @@ impl TmuxCommand for ListAllPanes {
.to_owned()
}
fn process_result(
&self,
domain_id: DomainId,
result: &Guarded,
) -> anyhow::Result<TmuxCommandResult> {
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::<TmuxDomain>() {
// 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::<TmuxDomain>() {
return tmux_domain.inner.sync_pane_state(&items);
}
}
anyhow::bail!("Tmux domain lost");
}
}
+74
View File
@@ -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<String>,
}
impl Read for TmuxReader {
fn read(&mut self, mut buf: &mut [u8]) -> std::io::Result<usize> {
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<String>,
// TODO: wx
}
impl Write for TmuxPty {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
// 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<portable_pty::PtySize, anyhow::Error> {
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<Box<dyn std::io::Read + Send>, anyhow::Error> {
Ok(Box::new(TmuxReader {
rx: self.rx.clone(),
}))
}
fn try_clone_writer(&self) -> Result<Box<dyn std::io::Write + Send>, anyhow::Error> {
Ok(Box::new(TmuxPty {
master_pane: self.master_pane.clone(),
rx: self.rx.clone(),
}))
}
fn process_group_leader(&self) -> Option<libc::pid_t> {
return None;
}
}