Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

feat(gui): save a saved host's choices as soon as they are made

RoversX37552220bda0
diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs
index c180e663..2eedad70 100644
--- a/wezterm-gui/src/main.rs
+++ b/wezterm-gui/src/main.rs
@@ -512,25 +512,36 @@ pub(crate) fn connect_domain_for_ssh_host_id(host_id: &str) -> anyhow::Result<Ar
/// client keeps the address, user and password it was built with, so one
/// registered earlier would go on with the old ones until a restart. Only a
/// domain doing nothing is replaced; one that is attached, attaching or
-/// retrying keeps going as it is.
+/// retrying keeps going as it is. So is one built the way the record has it
+/// already: the mux keeps every domain it is given, so another would only
+/// show up beside it, in the command palette for one.
pub(crate) fn refresh_connect_domain_for_ssh_host(spec: &crate::ssh_hosts::SshHostSpec) {
let name = crate::ssh_hosts::connect_domain_name(spec);
let Some(domain) = Mux::get().get_domain_by_name(&name) else {
return;
};
- let idle = domain.state() == mux::domain::DomainState::Detached
- && domain
- .downcast_ref::<ClientDomain>()
- .is_some_and(|client| !client.is_attaching() && !client.is_attach_retrying());
- if idle {
+ let Some(client) = domain.downcast_ref::<ClientDomain>() else {
+ return;
+ };
+ if domain.state() != mux::domain::DomainState::Detached
+ || client.is_attaching()
+ || client.is_attach_retrying()
+ {
+ return;
+ }
+ let current = client.ssh_domain_config().is_some_and(|built| {
+ crate::ssh_hosts::same_dialing(&built, &ssh_host_client_domain_config(spec, name.clone()))
+ });
+ if !current {
add_ssh_host_client_domain(spec, name);
}
}
-fn add_ssh_host_client_domain(
+/// What a saved host's Connect domain is built with.
+fn ssh_host_client_domain_config(
spec: &crate::ssh_hosts::SshHostSpec,
domain_name: String,
-) -> Arc<dyn Domain> {
+) -> SshDomain {
let mut dom = crate::ssh_hosts::build_ssh_domain(spec);
dom.name = domain_name;
dom.multiplexing = SshMultiplexing::WezTerm;
@@ -539,6 +550,14 @@ fn add_ssh_host_client_domain(
.as_deref()
.map(crate::secret::reveal)
.filter(|p| !p.is_empty());
+ dom
+}
+
+fn add_ssh_host_client_domain(
+ spec: &crate::ssh_hosts::SshHostSpec,
+ domain_name: String,
+) -> Arc<dyn Domain> {
+ let dom = ssh_host_client_domain_config(spec, domain_name);
let domain: Arc<dyn Domain> = Arc::new(ClientDomain::new(ClientDomainConfig::Ssh(dom)));
Mux::get().add_domain(&domain);
domain
diff --git a/wezterm-gui/src/ssh_hosts.rs b/wezterm-gui/src/ssh_hosts.rs
index def2ecab..415b53d6 100644
--- a/wezterm-gui/src/ssh_hosts.rs
+++ b/wezterm-gui/src/ssh_hosts.rs
@@ -259,18 +259,8 @@ pub fn try_update_host(host_id: &str, mut spec: SshHostSpec) -> Result<Option<Ss
let Some(record) = store.hosts.iter_mut().find(|record| record.id == host_id) else {
return Ok(None);
};
- // What connections found on the host is the record's to keep, not the
- // copy the form took when it opened -- unless the host moved to another
- // endpoint, which ThinkTerm has set nothing up on yet.
- if new_id == host_id {
- spec.shell_integration_applied = record.spec.shell_integration_applied;
- spec.shell_integration_unsupported = record.spec.shell_integration_unsupported.clone();
- spec.shell_integration_failure = record.spec.shell_integration_failure.clone();
- } else {
- spec.shell_integration_applied = false;
- spec.shell_integration_unsupported = None;
- spec.shell_integration_failure = None;
- }
+ keep_what_connections_found(&record.spec, &mut spec);
+ let reached_as_before = same_ssh_domain(&record.spec, &spec);
record.id = new_id.clone();
record.spec = spec;
save_ssh_host_store(&store)?;
@@ -281,11 +271,34 @@ pub fn try_update_host(host_id: &str, mut spec: SshHostSpec) -> Result<Option<Ss
.map(|record| record.spec.clone());
drop(store);
if let Some(spec) = spec {
- register_ssh_domain(&spec)?;
+ // Reached the way it was, the host has its domain already: the mux
+ // keeps every domain it is given, so another would only show up
+ // beside it, in the command palette for one.
+ if reached_as_before {
+ ensure_ssh_domain_registered(&spec)?;
+ } else {
+ register_ssh_domain(&spec)?;
+ }
}
Ok(Some(new_id))
}
+/// What connections found on the host is the record's to keep, not the copy
+/// the form took when it opened -- unless the host moved to another
+/// endpoint, which ThinkTerm has set nothing up on yet. Over the other
+/// network, it is the same host.
+fn keep_what_connections_found(record: &SshHostSpec, spec: &mut SshHostSpec) {
+ if endpoint(spec) == endpoint(record) {
+ spec.shell_integration_applied = record.shell_integration_applied;
+ spec.shell_integration_unsupported = record.shell_integration_unsupported.clone();
+ spec.shell_integration_failure = record.shell_integration_failure.clone();
+ } else {
+ spec.shell_integration_applied = false;
+ spec.shell_integration_unsupported = None;
+ spec.shell_integration_failure = None;
+ }
+}
+
pub fn try_remove_host(host_id: &str) -> Result<bool> {
if is_system_host_id(host_id) {
return Ok(false);
@@ -675,6 +688,24 @@ fn register_ssh_domain(spec: &SshHostSpec) -> Result<()> {
Ok(())
}
+/// Whether two domains made from a host dial it the same way: everything
+/// `build_ssh_domain` takes from the host's record, and its password.
+pub fn same_dialing(a: &SshDomain, b: &SshDomain) -> bool {
+ a.name == b.name
+ && a.remote_address == b.remote_address
+ && a.username == b.username
+ && a.multiplexing == b.multiplexing
+ && a.ssh_option == b.ssh_option
+ && a.stored_password == b.stored_password
+}
+
+/// Whether `a` and `b` make the same ssh domain. Most of a host's record --
+/// how its shell is set up, whether its OS is detected, Mosh -- goes into
+/// none of its domains.
+fn same_ssh_domain(a: &SshHostSpec, b: &SshHostSpec) -> bool {
+ same_dialing(&build_ssh_domain(a), &build_ssh_domain(b)) && a.password == b.password
+}
+
/// Ensure the host's SSH domain is registered with the mux, building and adding
/// it on first use. Returns the domain name to spawn into. The actual SSH
/// connection is established lazily by the domain's `spawn_pane`, so this is
@@ -789,6 +820,10 @@ fn domain_name_is_reachable(domain_name: &str) -> bool {
/// now: when it was renamed or moved to the other network, and no other
/// saved host goes by the old name as well.
pub fn connect_name_change(old: &SshHostSpec, new: &SshHostSpec) -> Option<(String, String)> {
+ // The same name: no need to read every host, `~/.ssh/config`'s too.
+ if connect_domain_name(old) == connect_domain_name(new) {
+ return None;
+ }
connect_name_change_among(old, new, &list_all_hosts())
}
@@ -1433,6 +1468,52 @@ Host prod *.internal
assert_eq!(pending_shell_integration(&spec), None);
}
+ #[test]
+ fn the_other_network_keeps_what_connections_found_on_the_host() {
+ let mut record = spec_with_options(HashMap::new());
+ record.shell_integration_applied = true;
+ record.shell_integration_unsupported = Some("nu".to_string());
+ // The form's copy, taken before the last connection.
+ let mut moved = spec_with_options(HashMap::new());
+ moved.network = SshNetwork::Tailnet;
+ keep_what_connections_found(&record, &mut moved);
+ assert!(moved.shell_integration_applied);
+ assert_eq!(moved.shell_integration_unsupported.as_deref(), Some("nu"));
+
+ // Another endpoint has nothing set up on it yet.
+ let mut elsewhere = record.clone();
+ elsewhere.port = Some(2222);
+ keep_what_connections_found(&record, &mut elsewhere);
+ assert!(!elsewhere.shell_integration_applied);
+ assert_eq!(elsewhere.shell_integration_unsupported, None);
+ }
+
+ #[test]
+ fn only_how_a_host_is_reached_makes_it_another_domain() {
+ let saved = spec_with_options(HashMap::new());
+ let mut settled = saved.clone();
+ settled.detect_os = true;
+ settled.shell_integration = true;
+ settled.shell_integration_applied = true;
+ settled.files_auto_connect = true;
+ settled.use_mosh = true;
+ assert!(same_ssh_domain(&saved, &settled));
+
+ let mut tailnet = saved.clone();
+ tailnet.network = SshNetwork::Tailnet;
+ let mut connect = saved.clone();
+ connect.multiplexing = true;
+ let mut port = saved.clone();
+ port.port = Some(2222);
+ let mut key = saved.clone();
+ key.identity_file = Some("/home/user/.ssh/id_example".to_string());
+ let mut password = saved.clone();
+ password.password = Some("sealed".to_string());
+ for changed in [tailnet, connect, port, key, password] {
+ assert!(!same_ssh_domain(&saved, &changed), "{changed:?}");
+ }
+ }
+
fn spec_with_options(ssh_options: HashMap<String, String>) -> SshHostSpec {
SshHostSpec {
label: "t".to_string(),
diff --git a/wezterm-gui/src/termwindow/ssh_hosts_view.rs b/wezterm-gui/src/termwindow/ssh_hosts_view.rs
index 6852d3a3..1d2a3b38 100644
--- a/wezterm-gui/src/termwindow/ssh_hosts_view.rs
+++ b/wezterm-gui/src/termwindow/ssh_hosts_view.rs
@@ -105,6 +105,26 @@ fn ssh_error(id: &'static str, error: impl Into<String>) -> String {
crate::i18n::tr_args(id, &args)
}
+/// Saves `spec` as the saved host `id` (see `ssh_hosts::try_update_host`),
+/// and what goes with it.
+fn update_saved_host(id: &str, spec: SshHostSpec) -> anyhow::Result<Option<String>> {
+ let before = ssh_hosts::host_spec(id);
+ let Some(saved_id) = ssh_hosts::try_update_host(id, spec)? else {
+ return Ok(None);
+ };
+ if let (Some(before), Some(after)) = (before, ssh_hosts::host_spec(&saved_id)) {
+ // Renamed or moved to the other network, the host is reached under
+ // another name from now on, and its Spaces go with it: left under
+ // the old name, they would reconnect the way it was reached before,
+ // or not at all.
+ if let Some((from, to)) = ssh_hosts::connect_name_change(&before, &after) {
+ ssh_hosts::move_spaces_after_rename(from, to);
+ }
+ crate::refresh_connect_domain_for_ssh_host(&after);
+ }
+ Ok(Some(saved_id))
+}
+
const PAD: f32 = 24.0;
const TOOLBAR_H: f32 = 54.0;
const TOOLBAR_GAP: f32 = 20.0;
@@ -253,6 +273,53 @@ impl HostMode {
}
}
+/// A choice in a saved host's form, saved as it is clicked (see
+/// `SshHostsView::save_choice`). What is typed waits for Save.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum HostChoice {
+ Network(SshNetwork),
+ Mode(HostMode),
+ DetectOs(bool),
+ ShellIntegration(bool),
+ FilesAutoConnect(bool),
+}
+
+impl HostChoice {
+ /// Makes the choice on `spec`, as Save makes it from the form.
+ fn apply_to(self, spec: &mut SshHostSpec) {
+ match self {
+ Self::Network(network) => spec.network = network,
+ Self::Mode(mode) => {
+ // Mosh and ThinkTerm Connect are alternative transports;
+ // only one can drive the connection.
+ spec.use_mosh = mode == HostMode::Mosh;
+ spec.multiplexing = mode == HostMode::Connect;
+ if spec.mosh_server_command.trim().is_empty() {
+ spec.mosh_server_command = ssh_hosts::DEFAULT_MOSH_SERVER_COMMAND.to_string();
+ }
+ }
+ Self::DetectOs(on) => spec.detect_os = on,
+ Self::ShellIntegration(on) => spec.shell_integration = on,
+ Self::FilesAutoConnect(on) => spec.files_auto_connect = on,
+ }
+ }
+
+ /// Puts back in `form` what `saved` has for the choice: what one that
+ /// could not be saved shows instead.
+ fn show_saved(self, saved: &SshHostSpec, form: &mut HostForm) {
+ match self {
+ Self::Network(_) => form.network = saved.network,
+ Self::Mode(_) => {
+ form.use_mosh = saved.use_mosh;
+ form.multiplexing = saved.multiplexing;
+ }
+ Self::DetectOs(_) => form.detect_os = saved.detect_os,
+ Self::ShellIntegration(_) => form.shell_integration = saved.shell_integration,
+ Self::FilesAutoConnect(_) => form.files_auto_connect = saved.files_auto_connect,
+ }
+ }
+}
+
/// The mux server of the README's architecture diagram, in the page's
/// theme, as (fill, edge, ink): how a host that keeps its sessions -- a
/// ThinkTerm Connect host -- is drawn.
@@ -831,38 +898,19 @@ impl SshHostsView {
}
let project_id = match &form.editing {
- Some(id) => {
- let before = ssh_hosts::host_spec(id);
- match ssh_hosts::try_update_host(id, spec) {
- // Not `id`: moving the host to another endpoint re-keys the
- // record, and the caller selects what comes back.
- Ok(Some(saved_id)) => {
- if let (Some(before), Some(after)) =
- (before, ssh_hosts::host_spec(&saved_id))
- {
- // Renamed or moved to the other network, the host
- // is reached under another name from now on, and
- // its Spaces go with it: left under the old name,
- // they would reconnect the way it was reached
- // before, or not at all.
- if let Some((from, to)) = ssh_hosts::connect_name_change(&before, &after)
- {
- ssh_hosts::move_spaces_after_rename(from, to);
- }
- crate::refresh_connect_domain_for_ssh_host(&after);
- }
- saved_id
- }
- Ok(None) => {
- form.error = Some(crate::i18n::tr("ssh-error-host-missing"));
- return None;
- }
- Err(err) => {
- form.error = Some(ssh_error("ssh-error-save", format!("{err:#}")));
- return None;
- }
+ // Not `id`: moving the host to another endpoint re-keys the
+ // record, and the caller selects what comes back.
+ Some(id) => match update_saved_host(id, spec) {
+ Ok(Some(saved_id)) => saved_id,
+ Ok(None) => {
+ form.error = Some(crate::i18n::tr("ssh-error-host-missing"));
+ return None;
}
- }
+ Err(err) => {
+ form.error = Some(ssh_error("ssh-error-save", format!("{err:#}")));
+ return None;
+ }
+ },
None => match ssh_hosts::try_create_host(spec) {
Ok(id) => id,
Err(err) => {
@@ -876,6 +924,80 @@ impl SshHostsView {
Some(project_id)
}
+ /// Saves `choice`, just made in the form, on the saved host the form
+ /// edits: that one choice, onto the host as saved, so that what was
+ /// typed into the form still waits for Save. A new host has nothing to
+ /// make it on yet; Save saves it with the rest.
+ fn save_choice(&mut self, choice: HostChoice) {
+ let Some(form) = self.form.as_mut() else {
+ return;
+ };
+ let Some(id) = form.editing.clone() else {
+ return;
+ };
+ let Some(saved) = ssh_hosts::host_spec(&id) else {
+ form.error = Some(crate::i18n::tr("ssh-error-host-missing"));
+ return;
+ };
+ let mut spec = saved.clone();
+ choice.apply_to(&mut spec);
+ // Made already: there is nothing to write.
+ if spec == saved {
+ return;
+ }
+ // One record per endpoint, as Save checks: on the other network,
+ // the host may be saved already.
+ let endpoint_id = ssh_hosts::host_id_for_host(&spec);
+ if endpoint_id != id && ssh_hosts::host_exists(&endpoint_id) {
+ let mut args = FluentArgs::new();
+ args.set("endpoint", ssh_hosts::endpoint(&spec));
+ form.error = Some(crate::i18n::tr_args("ssh-error-duplicate", &args));
+ choice.show_saved(&saved, form);
+ return;
+ }
+ match update_saved_host(&id, spec) {
+ Ok(Some(saved_id)) => {
+ let now = ssh_hosts::host_spec(&saved_id);
+ let on_tailnet = now
+ .as_ref()
+ .is_some_and(|spec| spec.network == SshNetwork::Tailnet);
+ if let Some(spec) = now {
+ form.original = Some(spec);
+ }
+ form.editing = Some(saved_id.clone());
+ self.keep_ring_on(&id, &saved_id, on_tailnet);
+ }
+ Ok(None) => {
+ form.error = Some(crate::i18n::tr("ssh-error-host-missing"));
+ choice.show_saved(&saved, form);
+ }
+ Err(err) => {
+ form.error = Some(ssh_error("ssh-error-save", format!("{err:#}")));
+ choice.show_saved(&saved, form);
+ }
+ }
+ }
+
+ /// The tailnet's machine of the host `id` keeps the ring once the host
+ /// is saved as `saved_id`: moved to the other network, the record is
+ /// keyed anew. Moved off the tailnet, no machine there stands for it any
+ /// more, so its card takes the ring -- where the list has it now, as the
+ /// record is keyed anew in place.
+ fn keep_ring_on(&mut self, id: &str, saved_id: &str, on_tailnet: bool) {
+ if self.tailnet.selected.as_deref() != Some(id) {
+ return;
+ }
+ if on_tailnet {
+ self.tailnet.selected = Some(saved_id.to_string());
+ return;
+ }
+ self.tailnet.selected = None;
+ if let Some(index) = self.filtered.iter().position(|entry| entry.id == id) {
+ self.selected = index;
+ self.selection_active = true;
+ }
+ }
+
// ---- input ------------------------------------------------------------
/// The inspector's edge, grabbed and dragged to make it wider or
@@ -1097,16 +1219,19 @@ impl SshHostsView {
}
if network == SshNetwork::Tailnet && form.use_mosh {
form.error = Some(crate::i18n::tr("ssh-error-tailnet-mosh"));
- } else {
- form.network = network;
- form.error = None;
+ return ContentViewResponse::Redraw;
}
+ form.network = network;
+ form.error = None;
}
+ self.save_choice(HostChoice::Network(network));
ContentViewResponse::Redraw
}
SshViewAction::ToggleDetect => {
if let Some(form) = self.form.as_mut() {
form.detect_os = !form.detect_os;
+ let choice = HostChoice::DetectOs(form.detect_os);
+ self.save_choice(choice);
}
ContentViewResponse::Redraw
}
@@ -1129,6 +1254,7 @@ impl SshHostsView {
self.focus = Focus::Field(FIELD_WORKSPACE);
}
}
+ self.save_choice(HostChoice::Mode(mode));
ContentViewResponse::Redraw
}
SshViewAction::SwitchToConnect(index) => {
@@ -1173,12 +1299,16 @@ impl SshHostsView {
SshViewAction::ToggleShellIntegration => {
if let Some(form) = self.form.as_mut() {
form.shell_integration = !form.shell_integration;
+ let choice = HostChoice::ShellIntegration(form.shell_integration);
+ self.save_choice(choice);
}
ContentViewResponse::Redraw
}
SshViewAction::ToggleFilesAutoConnect => {
if let Some(form) = self.form.as_mut() {
form.files_auto_connect = !form.files_auto_connect;
+ let choice = HostChoice::FilesAutoConnect(form.files_auto_connect);
+ self.save_choice(choice);
}
ContentViewResponse::Redraw
}
@@ -6338,6 +6468,95 @@ mod tests {
assert!(view.form.as_ref().unwrap().error.is_none());
}
+ #[test]
+ fn a_choice_is_made_on_the_saved_host_as_save_makes_it() {
+ use super::{HostChoice, HostMode};
+ let saved = spec("server-a", "192.0.2.10");
+
+ let mut moved = saved.clone();
+ HostChoice::Network(SshNetwork::Tailnet).apply_to(&mut moved);
+ assert_eq!(moved.network, SshNetwork::Tailnet);
+ // Nothing else of the host goes with it.
+ moved.network = saved.network;
+ assert_eq!(moved, saved);
+
+ let mut mosh = saved.clone();
+ mosh.multiplexing = true;
+ mosh.mosh_server_command = String::new();
+ HostChoice::Mode(HostMode::Mosh).apply_to(&mut mosh);
+ assert_eq!(
+ HostMode::of(mosh.use_mosh, mosh.multiplexing),
+ HostMode::Mosh
+ );
+ // Never on Mosh without a server command to start.
+ assert_eq!(
+ mosh.mosh_server_command,
+ super::ssh_hosts::DEFAULT_MOSH_SERVER_COMMAND
+ );
+ HostChoice::Mode(HostMode::Connect).apply_to(&mut mosh);
+ assert_eq!(
+ HostMode::of(mosh.use_mosh, mosh.multiplexing),
+ HostMode::Connect
+ );
+
+ let mut toggled = saved.clone();
+ HostChoice::DetectOs(false).apply_to(&mut toggled);
+ HostChoice::ShellIntegration(true).apply_to(&mut toggled);
+ HostChoice::FilesAutoConnect(true).apply_to(&mut toggled);
+ assert!(!toggled.detect_os && toggled.shell_integration && toggled.files_auto_connect);
+ }
+
+ #[test]
+ fn a_choice_that_could_not_be_saved_shows_what_is_saved() {
+ use super::{HostChoice, HostMode};
+ let saved = spec("server-a", "192.0.2.10");
+ let mut form = HostForm::default();
+ form.network = SshNetwork::Tailnet;
+ form.use_mosh = true;
+ form.detect_os = false;
+ HostChoice::Network(SshNetwork::Tailnet).show_saved(&saved, &mut form);
+ HostChoice::Mode(HostMode::Mosh).show_saved(&saved, &mut form);
+ HostChoice::DetectOs(false).show_saved(&saved, &mut form);
+ assert_eq!(form.network, SshNetwork::System);
+ assert_eq!(
+ HostMode::of(form.use_mosh, form.multiplexing),
+ HostMode::Ssh
+ );
+ assert!(form.detect_os);
+ }
+
+ #[test]
+ fn a_host_moved_off_the_tailnet_takes_the_ring_to_its_card() {
+ let mut view = SshHostsView::new();
+ let entry = |id: &str, label: &str| SshHostEntry {
+ id: id.to_string(),
+ source: SshHostSource::ThinkTerm,
+ spec: spec(label, "100.64.0.7"),
+ };
+ view.filtered = vec![
+ entry("ssh-other", "server-a"),
+ entry("ssh-saved", "server-b"),
+ ];
+ view.selection_active = false;
+ view.tailnet.selected = Some("ssh-saved".to_string());
+
+ // Still on the tailnet: its machine keeps the ring.
+ view.keep_ring_on("ssh-saved", "ssh-saved", true);
+ assert_eq!(view.tailnet.selected.as_deref(), Some("ssh-saved"));
+ assert!(!view.selection_active);
+
+ // Off it, keyed anew: its card takes the ring.
+ view.keep_ring_on("ssh-saved", "ssh-moved", false);
+ assert!(view.tailnet.selected.is_none());
+ assert!(view.selection_active);
+ assert_eq!(view.selected, 1);
+
+ // A host no machine had the ring for leaves the rings as they are.
+ view.selection_active = false;
+ view.keep_ring_on("ssh-other", "ssh-moved", false);
+ assert!(!view.selection_active);
+ }
+
#[test]
fn the_tailscale_panel_and_a_host_form_take_turns() {
use super::ContentView;