1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 05:12:40 +03:00

wezterm cli now prefers to talk to main GUI instance

When spawned with no WEZTERM_UNIX_SOCKET environment set,
we now prefer to resolve the gui instance, falling back to
the mux if it doesn't look like the gui is running.

`wezterm cli --prefer-mux` will use the original behavior of
trying to resolve the path from the unix domain in the config.
This commit is contained in:
Wez Furlong 2022-01-06 22:14:38 -07:00
parent eddf1c031a
commit 17a9d6159a
5 changed files with 67 additions and 22 deletions

View File

@ -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

View File

@ -1009,30 +1009,49 @@ impl Client {
self.local_domain_id
}
fn compute_unix_domain(
prefer_mux: bool,
class_name: &str,
) -> anyhow::Result<config::UnixDomain> {
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<Self> {
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)
}

View File

@ -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('=') {

View File

@ -66,7 +66,7 @@ use spawn::SpawnWhere;
const ATLAS_SIZE: usize = 128;
lazy_static::lazy_static! {
static ref WINDOW_CLASS: Mutex<String> = Mutex::new("org.wezfurlong.wezterm".to_owned());
static ref WINDOW_CLASS: Mutex<String> = Mutex::new(wezterm_gui_subcommands::DEFAULT_WINDOW_CLASS.to_owned());
}
pub const ICON_DATA: &'static [u8] = include_bytes!("../../../assets/icon/terminal.png");

View File

@ -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<String>,
#[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![