use crate::pane::PaneId;
use crate::tab::TabId;
use crate::tmux::{PrefixKey, PrefixState, TmuxPaneId, TmuxSession, TmuxSync};
#[cfg_attr(not(feature = "mux"), allow(dead_code))]
pub(crate) trait TmuxTransport {
fn drain(
&self,
) -> (
Vec<par_term_emu_core_rust::tmux_control::TmuxNotification>,
bool,
);
fn send_command(&self, command: &str) -> std::io::Result<Vec<String>>;
}
pub(crate) struct TmuxState {
pub(crate) tmux_session: Option<TmuxSession>,
pub(crate) transport: Option<Box<dyn TmuxTransport>>,
pub(crate) tmux_sync: TmuxSync,
pub(crate) tmux_session_name: Option<String>,
pub(crate) tmux_gateway_tab_id: Option<TabId>,
pub(crate) tmux_prefix_key: Option<PrefixKey>,
pub(crate) tmux_prefix_state: PrefixState,
#[cfg_attr(not(feature = "mux"), allow(dead_code))]
pub(crate) mux_focused_pane: Option<TmuxPaneId>,
#[cfg_attr(not(feature = "mux"), allow(dead_code))]
pub(crate) mux_screen_seeds: std::collections::HashMap<TmuxPaneId, Vec<u8>>,
#[cfg(feature = "mux")]
pub(crate) agent_roster: super::notifications::agent_roster::AgentRoster,
#[cfg(feature = "mux")]
pub(crate) mux_attach_pending: Option<super::notifications::mux::MuxAttachPending>,
pub(crate) tmux_pane_to_native_pane: std::collections::HashMap<TmuxPaneId, PaneId>,
pub(crate) native_pane_to_tmux_pane: std::collections::HashMap<PaneId, TmuxPaneId>,
}
impl TmuxState {
pub(crate) fn new(tmux_prefix_key: Option<PrefixKey>) -> Self {
Self {
tmux_session: None,
transport: None,
tmux_sync: TmuxSync::new(),
tmux_session_name: None,
tmux_gateway_tab_id: None,
tmux_prefix_key,
tmux_prefix_state: PrefixState::new(),
mux_focused_pane: None,
mux_screen_seeds: std::collections::HashMap::new(),
#[cfg(feature = "mux")]
agent_roster: super::notifications::agent_roster::AgentRoster::new(),
#[cfg(feature = "mux")]
mux_attach_pending: None,
tmux_pane_to_native_pane: std::collections::HashMap::new(),
native_pane_to_tmux_pane: std::collections::HashMap::new(),
}
}
pub(crate) fn persisted_session_names(&self) -> (Option<String>, Option<String>) {
match (&self.tmux_session_name, self.transport.is_some()) {
(Some(name), true) => (None, Some(name.clone())),
(name, false) => (name.clone(), None),
(None, true) => (None, None),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_gateway_session_name_persists_as_tmux() {
let mut state = TmuxState::new(None);
assert_eq!(state.persisted_session_names(), (None, None));
state.tmux_session_name = Some("work".to_string());
assert_eq!(
state.persisted_session_names(),
(Some("work".to_string()), None)
);
}
}