From 0fed030dacfa061428ea427ee18dec7d0546a35c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 5 Aug 2019 11:39:05 -0700 Subject: [PATCH] add `wezterm connect DOMAIN` subcommand for connecting to muxer This makes it a bit more convenient to control when and which multiplexer(s) you want to connect to. Now that each domain must have a name in the configuration file, we can address them more conveniently from the command line. --- src/main.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++ src/server/domain.rs | 2 +- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 9d09868f1..06c6145ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,6 +125,9 @@ enum SubCommand { #[structopt(name = "ssh", about = "Establish an ssh session")] Ssh(SshCommand), + #[structopt(name = "connect", about = "Connect to wezterm multiplexer")] + Connect(ConnectCommand), + #[structopt(name = "cli", about = "Interact with experimental mux server")] Cli(CliCommand), } @@ -179,6 +182,37 @@ struct SshCommand { prog: Vec, } +#[derive(Debug, StructOpt, Clone)] +struct ConnectCommand { + #[structopt( + long = "front-end", + raw( + possible_values = "&FrontEndSelection::variants()", + case_insensitive = "true" + ) + )] + front_end: Option, + + #[structopt( + long = "font-system", + raw( + possible_values = "&FontSystemSelection::variants()", + case_insensitive = "true" + ) + )] + font_system: Option, + + /// Name of the multiplexer domain section from the configuration + /// to which you'd like to connect + domain_name: String, + + /// Instead of executing your shell, run PROG. + /// For example: `wezterm start -- bash -l` will spawn bash + /// as if it were a login shell. + #[structopt(parse(from_os_str))] + prog: Vec, +} + pub fn create_user_owned_dirs(p: &Path) -> Fallible<()> { let mut builder = DirBuilder::new(); builder.recursive(true); @@ -279,6 +313,69 @@ fn run_ssh(config: Arc, opts: &SshCommand) -> Fallible<()> { gui.run_forever() } +fn client_domains(config: &Arc) -> Vec { + let mut domains = vec![]; + for unix_dom in &config.unix_domains { + domains.push(ClientDomainConfig::Unix(unix_dom.clone())); + } + + for ssh_dom in &config.ssh_domains { + domains.push(ClientDomainConfig::Ssh(ssh_dom.clone())); + } + + for tls_client in &config.tls_clients { + domains.push(ClientDomainConfig::Tls(tls_client.clone())); + } + domains +} + +fn run_mux_client(config: Arc, opts: &ConnectCommand) -> Fallible<()> { + let client_config = client_domains(&config) + .into_iter() + .find(|c| c.name() == opts.domain_name) + .ok_or_else(|| { + format_err!( + "no multiplexer domain with name `{}` was found in the configuration", + opts.domain_name + ) + })?; + + let font_system = opts.font_system.unwrap_or(config.font_system); + font_system.set_default(); + + let fontconfig = Rc::new(FontConfiguration::new(Arc::clone(&config), font_system)); + + let domain: Arc = Arc::new(ClientDomain::new(client_config)); + let mux = Rc::new(mux::Mux::new(&config, &domain)); + Mux::set_mux(&mux); + + let front_end = opts.front_end.unwrap_or(config.front_end); + let gui = front_end.try_new(&mux)?; + domain.attach()?; + + if mux.is_empty() { + let cmd = if !opts.prog.is_empty() { + let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect(); + let mut builder = CommandBuilder::new(&argv[0]); + builder.args(&argv[1..]); + Some(builder) + } else { + None + }; + let window_id = mux.new_empty_window(); + let tab = mux + .default_domain() + .spawn(PtySize::default(), cmd, window_id)?; + gui.spawn_new_window(mux.config(), &fontconfig, &tab, window_id)?; + } + + for dom in mux.iter_domains() { + log::error!("domain {} state {:?}", dom.domain_id(), dom.state()); + } + + gui.run_forever() +} + fn run_terminal_gui(config: Arc, opts: &StartCommand) -> Fallible<()> { #[cfg(unix)] { @@ -445,6 +542,7 @@ fn run() -> Result<(), Error> { run_terminal_gui(config, &start) } SubCommand::Ssh(ssh) => run_ssh(config, &ssh), + SubCommand::Connect(connect) => run_mux_client(config, &connect), SubCommand::Cli(cli) => { let client = Client::new_default_unix_domain(&config)?; match cli.sub { diff --git a/src/server/domain.rs b/src/server/domain.rs index cbdb8b9ca..86ec95fc3 100644 --- a/src/server/domain.rs +++ b/src/server/domain.rs @@ -62,7 +62,7 @@ pub enum ClientDomainConfig { } impl ClientDomainConfig { - fn name(&self) -> &str { + pub fn name(&self) -> &str { match self { ClientDomainConfig::Unix(unix) => &unix.name, ClientDomainConfig::Tls(tls) => &tls.name,