diff --git a/docs/changelog.md b/docs/changelog.md index 0fecb773b..9a17a5d55 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -20,6 +20,7 @@ As features stabilize some brief notes about them will accumulate here. #### Changed * **Key Assignments now use Physical Key locations by default!!** follow the work in progress in [#1483](https://github.com/wez/wezterm/issues/1483) +* `wezterm cli`, when run outside of a wezterm pane, now prefers to connect to the main GUI instance rather than background mux server. Use `wezterm cli --prefer-mux` to ignore the GUI instance and talk only to the mux server. See `wezterm cli --help` for additional information. #### Updated and Improved diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs index 32f939c99..567d73509 100644 --- a/wezterm-client/src/client.rs +++ b/wezterm-client/src/client.rs @@ -1009,30 +1009,49 @@ impl Client { self.local_domain_id } + fn compute_unix_domain( + prefer_mux: bool, + class_name: &str, + ) -> anyhow::Result { + match std::env::var_os("WEZTERM_UNIX_SOCKET") { + Some(path) if !path.is_empty() => Ok(config::UnixDomain { + socket_path: Some(path.into()), + ..Default::default() + }), + Some(_) | None => { + if !prefer_mux { + if let Ok(gui) = crate::discovery::resolve_gui_sock_path(class_name) { + return Ok(config::UnixDomain { + socket_path: Some(gui), + no_serve_automatically: true, + ..Default::default() + }); + } + } + + let config = configuration(); + Ok(config + .unix_domains + .first() + .ok_or_else(|| { + anyhow!( + "no default unix domain is configured and WEZTERM_UNIX_SOCKET \ + is not set in the environment" + ) + })? + .clone()) + } + } + } + pub fn new_default_unix_domain( initial: bool, ui: &mut ConnectionUI, no_auto_start: bool, + prefer_mux: bool, + class_name: &str, ) -> anyhow::Result { - let config = configuration(); - - let unix_dom = match std::env::var_os("WEZTERM_UNIX_SOCKET") { - Some(path) => config::UnixDomain { - socket_path: Some(path.into()), - ..Default::default() - }, - None => config - .unix_domains - .first() - .ok_or_else(|| { - anyhow!( - "no default unix domain is configured and WEZTERM_UNIX_SOCKET \ - is not set in the environment" - ) - })? - .clone(), - }; - + let unix_dom = Self::compute_unix_domain(prefer_mux, class_name)?; Self::new_unix_domain(alloc_domain_id(), &unix_dom, initial, ui, no_auto_start) } diff --git a/wezterm-gui-subcommands/src/lib.rs b/wezterm-gui-subcommands/src/lib.rs index c69537d73..9aabe4906 100644 --- a/wezterm-gui-subcommands/src/lib.rs +++ b/wezterm-gui-subcommands/src/lib.rs @@ -2,6 +2,8 @@ use config::SshParameters; use std::ffi::OsString; use structopt::StructOpt; +pub const DEFAULT_WINDOW_CLASS: &str = "org.wezfurlong.wezterm"; + /// Helper for parsing config overrides pub fn name_equals_value(arg: &str) -> Result<(String, String), String> { if let Some(eq) = arg.find('=') { diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index eb4ea1dd5..d5234d525 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -66,7 +66,7 @@ use spawn::SpawnWhere; const ATLAS_SIZE: usize = 128; lazy_static::lazy_static! { - static ref WINDOW_CLASS: Mutex = Mutex::new("org.wezfurlong.wezterm".to_owned()); + static ref WINDOW_CLASS: Mutex = Mutex::new(wezterm_gui_subcommands::DEFAULT_WINDOW_CLASS.to_owned()); } pub const ICON_DATA: &'static [u8] = include_bytes!("../../../assets/icon/terminal.png"); diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index c13ac0082..c182d16a0 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -90,6 +90,19 @@ struct CliCommand { #[structopt(long = "no-auto-start")] no_auto_start: bool, + /// Prefer connecting to a background mux server. + /// The default is to prefer connecting to a running + /// wezterm gui instance + #[structopt(long = "prefer-mux")] + prefer_mux: bool, + + /// When connecting to a gui instance, if you started the + /// gui with `--class SOMETHING`, you should also pass + /// that same value here in order for the client to find + /// the correct gui instance. + #[structopt(long = "class")] + class: Option, + #[structopt(subcommand)] sub: CliSubCommand, } @@ -373,9 +386,19 @@ fn delegate_to_gui(saver: UmaskSaver) -> anyhow::Result<()> { } async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow::Result<()> { - let initial = true; let mut ui = mux::connui::ConnectionUI::new_headless(); - let client = Client::new_default_unix_domain(initial, &mut ui, cli.no_auto_start)?; + let initial = true; + + let client = Client::new_default_unix_domain( + initial, + &mut ui, + cli.no_auto_start, + cli.prefer_mux, + cli.class + .as_deref() + .unwrap_or(wezterm_gui_subcommands::DEFAULT_WINDOW_CLASS), + )?; + match cli.sub { CliSubCommand::List => { let cols = vec![