Tmux control: Address the comments, second round

refs: #336
This commit is contained in:
Joe Xue authored and Wez Furlong committed 2025-03-07 09:00:48 -08:00
1 parent 36007dca65
commit 238896fc41
3 files changed
+109 -126

No files matched your search

+17 -10
View File
@@ -11,7 +11,6 @@ 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;
@@ -75,7 +74,7 @@ pub(crate) struct TmuxDomainState {
pub tmux_session: Mutex<Option<TmuxSessionId>>,
pub support_commands: Mutex<HashMap<String, String>>,
pub attach_state: Mutex<AttachState>,
pub channel: (Sender<TmuxPaneId>, Receiver<TmuxPaneId>),
pending_splits: Mutex<VecDeque<promise::Promise<TmuxPaneId>>>,
pub backlog: Mutex<HashMap<TmuxPaneId, String>>,
}
@@ -178,9 +177,10 @@ impl TmuxDomainState {
// Split pane
if !self.check_pane_attached(*window, *pane) {
smol::block_on(async {
let _ = self.channel.0.send(*pane).await;
});
let mut pending_splits = self.pending_splits.lock();
if let Some(mut promise) = pending_splits.pop_front() {
promise.ok(*pane);
}
}
log::info!("tmux window pane changed: {}:{}", window, pane);
}
@@ -332,7 +332,7 @@ impl TmuxDomain {
tmux_session: Mutex::new(None),
support_commands: Mutex::new(HashMap::default()),
attach_state: Mutex::new(AttachState::Init),
channel: bounded(1),
pending_splits: Mutex::new(VecDeque::default()),
backlog: Mutex::new(HashMap::default()),
});
@@ -367,11 +367,18 @@ impl Domain for TmuxDomain {
pane_id: PaneId,
split_request: SplitRequest,
) -> anyhow::Result<Arc<dyn Pane>> {
self.inner.split_tmux_pane(tab, pane_id, split_request);
let mut promise = promise::Promise::new();
if let Some(future) = promise.get_future() {
{
let mut pending_splits = self.inner.pending_splits.lock();
pending_splits.push_back(promise);
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;
if let Ok(id) = future.await {
let pane = self.inner.split_pane(tab, pane_id, id, split_request);
return pane;
}
}
anyhow::bail!("Split_pane failed");
+37 -80
View File
@@ -311,7 +311,7 @@ impl TmuxDomainState {
if let Some(local_pane) = local_pane {
let c = local_pane.get_cursor_position();
// no caupture, output case
// no capture, output case
if (c.x + c.y as usize) == 0 {
if let Some(text) = self.backlog.lock().remove(&pane.pane_id) {
if let Some(ref_pane) = pane_map.get(&pane.pane_id) {
@@ -472,7 +472,7 @@ impl TmuxDomainState {
let pane_map = self.remote_panes.lock();
let local_pane_id = match pane_map.get(&p.pane_id) {
Some(x) => x.lock().local_pane_id,
None => anyhow::bail!("cannot find the local pane for {}", &p.pane_id),
None => anyhow::bail!("cannot find the local pane for {}", p.pane_id),
};
split_pane_index = match tab
@@ -482,7 +482,7 @@ impl TmuxDomainState {
{
Some(x) => x.index,
None => {
log::info!("invalid pane id {}", local_pane_id);
log::info!("invalid pane id {local_pane_id}");
continue;
}
};
@@ -500,7 +500,7 @@ impl TmuxDomainState {
None => {
log::info!(
"cannot find the local tab for tmux window {}",
&window.window_id
window.window_id
);
continue;
}
@@ -665,13 +665,9 @@ impl TmuxCommand for ListAllPanes {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("list-panes failed");
let error = format!("list-pane in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
let mut items = vec![];
let mut pane_set = HashSet::new();
@@ -779,13 +775,9 @@ impl TmuxCommand for ListAllWindows {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("list-windows failed");
let error = format!("list-window in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
let mut items = vec![];
@@ -956,12 +948,9 @@ impl TmuxCommand for Resize {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("resize-window failed");
let error = format!("resize-pane in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
Ok(())
}
@@ -984,13 +973,9 @@ impl TmuxCommand for CapturePane {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("capture-pane failed");
let error = format!("capture-pane in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
let mux = Mux::get();
let domain = match mux.get_domain(domain_id) {
@@ -1038,13 +1023,9 @@ impl TmuxCommand for SendKeys {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("send-key failed");
let error = format!("send-key in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
Ok(())
}
@@ -1059,13 +1040,9 @@ impl TmuxCommand for NewWindow {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("new-windows failed");
let error = format!("new-window in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
Ok(())
}
@@ -1080,13 +1057,9 @@ impl TmuxCommand for ListCommands {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("list-commands failed");
let error = format!("list-command in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
let mux = Mux::get();
let domain = match mux.get_domain(domain_id) {
@@ -1138,13 +1111,9 @@ impl TmuxCommand for SplitPane {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("split-windows failed");
let error = format!("split-window in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
Ok(())
}
@@ -1162,13 +1131,9 @@ impl TmuxCommand for SelectWindow {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("select-windows failed");
let error = format!("select-window in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
Ok(())
}
@@ -1186,13 +1151,9 @@ impl TmuxCommand for SelectPane {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("select-pane failed");
let error = format!("select-pane in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
Ok(())
}
@@ -1210,13 +1171,9 @@ impl TmuxCommand for AttachDone {
fn process_result(&self, domain_id: DomainId, result: &Guarded) -> anyhow::Result<()> {
if result.error {
log::error!(
"Error resizing: domain_id={} result={:?}",
domain_id,
result
);
anyhow::bail!("list-session failed");
let error = format!("list-session in domain={domain_id} failed: {result:#?}");
log::error!("{error}");
anyhow::bail!("{error}");
}
let mux = Mux::get();
let domain = match mux.get_domain(domain_id) {
+55 -36
View File
@@ -281,7 +281,22 @@ fn parse_line(line: &str) -> anyhow::Result<Event> {
raw_flags,
})
}
_ => unreachable!(),
Rule::pane_id
| Rule::word
| Rule::client_name
| Rule::window_id
| Rule::session_id
| Rule::window_layout
| Rule::any_text
| Rule::line
| Rule::line_entire
| Rule::EOI
| Rule::number
| Rule::layout_split_pane
| Rule::layout_pane
| Rule::layout_split_horizontal
| Rule::layout_split_vertical
| Rule::layout_window => anyhow::bail!("Should not reach here"),
}
}
@@ -540,7 +555,36 @@ fn parse_layout_inner(
stack.push(pane);
}
_ => unreachable!(),
Rule::EOI
| Rule::number
| Rule::client_session_changed
| Rule::any_text
| Rule::pane_id
| Rule::window_id
| Rule::word
| Rule::window_layout
| Rule::line_entire
| Rule::line
| Rule::session_id
| Rule::client_name
| Rule::client_detached
| Rule::begin
| Rule::end
| Rule::error
| Rule::exit
| Rule::output
| Rule::pane_mode_changed
| Rule::session_changed
| Rule::session_renamed
| Rule::session_window_changed
| Rule::sessions_changed
| Rule::window_add
| Rule::window_close
| Rule::window_pane_changed
| Rule::window_renamed
| Rule::layout_split_pane
| Rule::layout_window
| Rule::layout_change => anyhow::bail!("Should not reach here"),
}
}
@@ -836,7 +880,7 @@ here
let layout_case2 = "158x40,0,0[158x20,0,0,69,158x19,0,21{79x19,0,21,70,78x19,80,21[78x9,80,21,71,78x9,80,31,73]}]".to_string();
let layout_case3 = "158x40,0,0{79x40,0,0[79x20,0,0,74,79x19,0,21{39x19,0,21,76,39x19,40,21,77}],78x40,80,0,75}".to_string();
let mut layout = parse_layout(layout_case1.get(0..).unwrap()).unwrap();
let mut layout = parse_layout(&layout_case1).unwrap();
let l = layout.pop().unwrap();
assert!(if let WindowLayout::SinglePane(p) = l {
assert_eq!(p.pane_width, 158);
@@ -849,38 +893,13 @@ here
false
});
layout = parse_layout(layout_case2.get(0..).unwrap()).unwrap();
assert!(if let WindowLayout::SplitVertical(_x) = &layout[0] {
true
} else {
false
});
assert!(if let WindowLayout::SplitHorizontal(_x) = &layout[1] {
true
} else {
false
});
assert!(if let WindowLayout::SplitVertical(_x) = &layout[2] {
true
} else {
false
});
layout = parse_layout(layout_case3.get(0..).unwrap()).unwrap();
assert!(if let WindowLayout::SplitHorizontal(_x) = &layout[0] {
true
} else {
false
});
assert!(if let WindowLayout::SplitVertical(_x) = &layout[1] {
true
} else {
false
});
assert!(if let WindowLayout::SplitHorizontal(_x) = &layout[2] {
true
} else {
false
});
layout = parse_layout(&layout_case2).unwrap();
assert!(matches!(&layout[0], WindowLayout::SplitVertical(_x)));
assert!(matches!(&layout[1], WindowLayout::SplitHorizontal(_x)));
assert!(matches!(&layout[2], WindowLayout::SplitVertical(_x)));
layout = parse_layout(&layout_case3).unwrap();
assert!(matches!(&layout[0], WindowLayout::SplitHorizontal(_x)));
assert!(matches!(&layout[1], WindowLayout::SplitVertical(_x)));
assert!(matches!(&layout[2], WindowLayout::SplitHorizontal(_x)));
}
}