Files
LLeMbas-TTY/wezterm/src/cli/spawn_command.rs
T
Wez Furlong 4f123a461b macos (mostly): try harder to get initial dpi
There are a number of open issues that relate to getting the dpi
wrong when spawning a window. In theory it shouldn't matter because
we will immediately realize the difference and synthesize the correct
information, but evidence shows this isn't quite true.

What this commit does is:

* Override Connection::default_dpi() on macOS to return the
  effective_dpi from the active screen instead of the default
  non-retina dpi
* Adjust the Config::initial_size() method to accept an optional
  cell pixel dimension
* Add a helper function to wezterm-gui to compute the cell pixel
  dimensions given the config and the (usually default) dpi, and
  feed that through to Config::initial_size
* in the macos window impl, scale the computed geometry based on
  the ratio of the Connection::default_dpi and the default non-retina
  dpi.

This helps to avoid needing to do a fixup in the
https://github.com/wez/wezterm/issues/4966 case, and may help with
the various other macos quirky issues.

refs: https://github.com/wez/wezterm/issues/2958
refs: https://github.com/wez/wezterm/issues/3575
refs: https://github.com/wez/wezterm/issues/3900
refs: https://github.com/wez/wezterm/issues/4250
refs: https://github.com/wez/wezterm/issues/4285
refs: https://github.com/wez/wezterm/issues/4447
refs: https://github.com/wez/wezterm/issues/4851
refs: https://github.com/wez/wezterm/issues/4966
2024-02-06 22:57:05 -07:00

124 lines
4.0 KiB
Rust

use crate::cli::resolve_relative_cwd;
use clap::{Parser, ValueHint};
use config::keyassignment::SpawnTabDomain;
use config::ConfigHandle;
use mux::pane::PaneId;
use mux::window::WindowId;
use portable_pty::cmdbuilder::CommandBuilder;
use std::ffi::OsString;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SpawnCommand {
/// Specify the current pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
/// The pane is used to determine the current domain
/// and window.
#[arg(long)]
pane_id: Option<PaneId>,
#[arg(long)]
domain_name: Option<String>,
/// Specify the window into which to spawn a tab.
/// If omitted, the window associated with the current
/// pane is used.
/// Cannot be used with `--workspace` or `--new-window`.
#[arg(long, conflicts_with_all=&["workspace", "new_window"])]
window_id: Option<WindowId>,
/// Spawn into a new window, rather than a new tab.
#[arg(long)]
new_window: bool,
/// Specify the current working directory for the initially
/// spawned program
#[arg(long, value_parser, value_hint=ValueHint::DirPath)]
cwd: Option<OsString>,
/// When creating a new window, override the default workspace name
/// with the provided name. The default name is "default".
/// Requires `--new-window`.
#[arg(long, requires = "new_window")]
workspace: Option<String>,
/// Instead of executing your shell, run PROG.
/// For example: `wezterm cli spawn -- bash -l` will spawn bash
/// as if it were a login shell.
#[arg(value_parser, value_hint=ValueHint::CommandWithArguments, num_args=1..)]
prog: Vec<OsString>,
}
impl SpawnCommand {
pub async fn run(self, client: Client, config: &ConfigHandle) -> anyhow::Result<()> {
let window_id = if self.new_window {
None
} else {
match self.window_id {
Some(w) => Some(w),
None => {
let pane_id = client.resolve_pane_id(self.pane_id).await?;
let panes = client.list_panes().await?;
let mut window_id = None;
'outer: for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
if entry.pane_id == pane_id {
window_id.replace(entry.window_id);
break 'outer;
}
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
window_id
}
}
};
let workspace = self
.workspace
.as_deref()
.unwrap_or(
config
.default_workspace
.as_deref()
.unwrap_or(mux::DEFAULT_WORKSPACE),
)
.to_string();
let size = config.initial_size(0, None);
let spawned = client
.spawn_v2(codec::SpawnV2 {
domain: self
.domain_name
.map_or(SpawnTabDomain::DefaultDomain, |name| {
SpawnTabDomain::DomainName(name)
}),
window_id,
command: if self.prog.is_empty() {
None
} else {
let builder = CommandBuilder::from_argv(self.prog);
Some(builder)
},
command_dir: resolve_relative_cwd(self.cwd)?,
size,
workspace,
})
.await?;
log::debug!("{:?}", spawned);
println!("{}", spawned.pane_id);
Ok(())
}
}