Add a command palette picker for local and SSH sessions
Discover attached and detached sessions over existing SSH connections. Focus an open view or attach to the selected session using its daemon identity, preserving the original shells.
This commit is contained in:
+149
-13
@@ -14,15 +14,16 @@ pub(super) enum RemoteEvent {
|
||||
}
|
||||
|
||||
pub(super) struct RemoteSession {
|
||||
target: RemoteWindow,
|
||||
pub(super) target: RemoteWindow,
|
||||
anchor: TabId,
|
||||
slot: TabId,
|
||||
cancelled: Arc<AtomicBool>,
|
||||
connection: Option<Transport>,
|
||||
pub(super) connection: Option<Transport>,
|
||||
// Remote IDs map to stable IDs allocated from the parent session's pool.
|
||||
tabs: HashMap<TabId, TabId>,
|
||||
panes: HashMap<PaneId, PaneId>,
|
||||
latest: Option<Arc<Snapshot>>,
|
||||
attaching: Option<SessionIdentity>,
|
||||
requested_tab: Option<TabId>,
|
||||
clipboard: VecDeque<(PaneId, Weak<Outbox>)>,
|
||||
}
|
||||
@@ -52,6 +53,43 @@ impl RemoteSession {
|
||||
(self.latest.is_none() && self.slot == local) || original(&self.tabs, local).is_some()
|
||||
}
|
||||
|
||||
pub(super) fn listed_session(
|
||||
&self,
|
||||
owner: SessionIdentity,
|
||||
attached: usize,
|
||||
) -> Option<ListedSession> {
|
||||
let snapshot = self.latest.as_ref()?;
|
||||
let identity = *snapshot.lineage.first()?;
|
||||
let active = self.requested_tab.unwrap_or(snapshot.active_tab);
|
||||
let tab = *self.tabs.get(&active)?;
|
||||
Some(ListedSession {
|
||||
route: SessionRoute::Mounted {
|
||||
owner,
|
||||
tab,
|
||||
identity,
|
||||
},
|
||||
host: Some(self.target.destination.clone()),
|
||||
summary: SessionSummary::from_tabs(&snapshot.tabs, active),
|
||||
attached,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn selection_tab(&self, identity: SessionIdentity, tab: TabId) -> Option<TabId> {
|
||||
let Some(snapshot) = &self.latest else {
|
||||
return (self.attaching == Some(identity)).then_some(self.slot);
|
||||
};
|
||||
if snapshot.lineage.first() != Some(&identity) {
|
||||
return None;
|
||||
}
|
||||
// If the tab closed while the picker was open, the same SSH session
|
||||
// may still have another tab with a live shell.
|
||||
if self.owns_tab(tab) {
|
||||
Some(tab)
|
||||
} else {
|
||||
self.tabs.get(&snapshot.active_tab).copied()
|
||||
}
|
||||
}
|
||||
|
||||
fn nested_tab(&self, local: TabId) -> Option<&TabInfo> {
|
||||
let id = original(&self.tabs, local)?;
|
||||
self.latest
|
||||
@@ -278,28 +316,47 @@ impl SessionState {
|
||||
target: SessionTarget,
|
||||
ssh: SshOptions,
|
||||
) -> Result<()> {
|
||||
crate::client::validate_destination(&destination)?;
|
||||
ensure!(
|
||||
self.panes.contains_key(&pane),
|
||||
"originating shell has ended"
|
||||
);
|
||||
let (index, _, _) = self.pane_position(pane)?;
|
||||
self.open_remote_at(
|
||||
self.tabs[index].id,
|
||||
RemoteWindow { destination, ssh },
|
||||
target,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn open_remote_at(
|
||||
&mut self,
|
||||
anchor: TabId,
|
||||
window_target: RemoteWindow,
|
||||
target: SessionTarget,
|
||||
) -> Result<()> {
|
||||
crate::client::validate_destination(&window_target.destination)?;
|
||||
let index = self
|
||||
.tabs
|
||||
.iter()
|
||||
.position(|tab| tab.id == anchor)
|
||||
.context("originating tab has closed")?;
|
||||
ensure!(
|
||||
self.remotes.len() < MAX_PANES && self.tabs.len() < 256,
|
||||
"too many SSH sessions or tabs"
|
||||
);
|
||||
let (index, _, _) = self.pane_position(pane)?;
|
||||
let group = self.next_id;
|
||||
self.next_id += 1;
|
||||
let socket = self.socket.clone();
|
||||
let session = self.id;
|
||||
let sender = self.sender.clone();
|
||||
let dest = destination.clone();
|
||||
let window_target = RemoteWindow {
|
||||
destination,
|
||||
ssh: ssh.clone(),
|
||||
};
|
||||
let dest = window_target.destination.clone();
|
||||
let ssh = window_target.ssh.clone();
|
||||
let cancelled = Arc::new(AtomicBool::new(false));
|
||||
let worker_cancelled = cancelled.clone();
|
||||
let attaching = match target {
|
||||
SessionTarget::Remembered(id) => Some(id),
|
||||
_ => None,
|
||||
};
|
||||
thread::Builder::new()
|
||||
.name("psyterm-remote-tab".into())
|
||||
.spawn(move || {
|
||||
@@ -334,7 +391,7 @@ impl SessionState {
|
||||
}
|
||||
}
|
||||
})?;
|
||||
self.insert_remote_slot(index, group, window_target, cancelled);
|
||||
self.insert_remote_slot(index, group, window_target, cancelled, attaching);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -344,8 +401,16 @@ impl SessionState {
|
||||
group: u64,
|
||||
target: RemoteWindow,
|
||||
cancelled: Arc<AtomicBool>,
|
||||
attaching: Option<SessionIdentity>,
|
||||
) {
|
||||
let anchor = self.tabs[index].id;
|
||||
// Keep the tabs mirrored by each SSH connection contiguous across layout updates.
|
||||
let index = self
|
||||
.remotes
|
||||
.values()
|
||||
.find(|mount| mount.owns_tab(anchor))
|
||||
.and_then(|mount| self.tabs.iter().rposition(|tab| mount.owns_tab(tab.id)))
|
||||
.unwrap_or(index);
|
||||
self.tabs.insert(
|
||||
index + 1,
|
||||
TabInfo {
|
||||
@@ -373,6 +438,7 @@ impl SessionState {
|
||||
tabs: HashMap::new(),
|
||||
panes: HashMap::new(),
|
||||
latest: None,
|
||||
attaching,
|
||||
requested_tab: None,
|
||||
clipboard: VecDeque::new(),
|
||||
},
|
||||
@@ -624,6 +690,7 @@ mod tests {
|
||||
ssh: SshOptions::default(),
|
||||
},
|
||||
Arc::new(AtomicBool::new(false)),
|
||||
None,
|
||||
);
|
||||
group
|
||||
}
|
||||
@@ -638,6 +705,77 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mounted_directory_entry_keeps_one_identity_and_focus_tab_for_the_whole_session() {
|
||||
let mut local = session(1);
|
||||
local.new_tab(None).unwrap();
|
||||
let group = reserve(&mut local, 0);
|
||||
let mut upstream = session(2);
|
||||
upstream.new_tab(None).unwrap();
|
||||
upstream.new_tab(None).unwrap();
|
||||
assert!(
|
||||
local.remotes[&group]
|
||||
.listed_session(local.identity, 1)
|
||||
.is_none()
|
||||
);
|
||||
let frame = upstream.snapshot();
|
||||
let expected = SessionSummary::from_tabs(&frame.tabs, frame.active_tab);
|
||||
apply(&mut local, group, frame).unwrap();
|
||||
|
||||
let mount = &local.remotes[&group];
|
||||
let listed = mount.listed_session(local.identity, 1).unwrap();
|
||||
let tab = local.active_tab;
|
||||
assert_eq!(
|
||||
listed.route,
|
||||
SessionRoute::Mounted {
|
||||
owner: local.identity,
|
||||
tab,
|
||||
identity: upstream.identity
|
||||
}
|
||||
);
|
||||
assert_eq!(listed.host.as_deref(), Some("user@work"));
|
||||
assert_eq!(listed.summary, expected);
|
||||
assert_eq!((listed.summary.tabs, listed.summary.panes), (2, 2));
|
||||
assert_eq!(mount.selection_tab(upstream.identity, tab), Some(tab));
|
||||
assert_eq!(mount.selection_tab(upstream.identity, u64::MAX), Some(tab));
|
||||
assert!(mount.selection_tab(local.identity, tab).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sibling_session_attaches_after_all_mirrored_tabs_and_restores_focus_on_failure() {
|
||||
let mut local = session(1);
|
||||
local.new_tab(None).unwrap();
|
||||
let first = reserve(&mut local, 0);
|
||||
let mut upstream = session(2);
|
||||
upstream.new_tab(None).unwrap();
|
||||
upstream.new_tab(None).unwrap();
|
||||
apply(&mut local, first, upstream.snapshot()).unwrap();
|
||||
let anchor = local.tabs[1].id;
|
||||
local.active_tab = anchor;
|
||||
let sibling = reserve(&mut local, 1);
|
||||
let target = SessionIdentity {
|
||||
session: 7,
|
||||
..upstream.identity
|
||||
};
|
||||
local.remotes.get_mut(&sibling).unwrap().attaching = Some(target);
|
||||
assert_eq!(
|
||||
local.remotes[&sibling].selection_tab(target, anchor),
|
||||
Some(sibling)
|
||||
);
|
||||
assert_eq!(local.active_tab, sibling);
|
||||
assert_eq!(local.tabs[3].id, sibling);
|
||||
upstream.new_tab(None).unwrap();
|
||||
apply(&mut local, first, upstream.snapshot()).unwrap();
|
||||
assert_eq!(local.tabs[4].id, sibling);
|
||||
assert!(
|
||||
local.tabs[1..4]
|
||||
.iter()
|
||||
.all(|tab| local.remotes[&first].owns_tab(tab.id))
|
||||
);
|
||||
local.remove_remote(sibling);
|
||||
assert_eq!(local.active_tab, anchor);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remote_window_uses_the_selected_mount_and_nested_destination() {
|
||||
let mut local = session(1);
|
||||
@@ -786,9 +924,7 @@ mod tests {
|
||||
.err()
|
||||
.expect("refuse a recycled session ID");
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("remembered remote session has ended"),
|
||||
error.to_string().contains("remembered session has ended"),
|
||||
"{error:#}"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user