From a0cbea270357cd84b84a7972ff158ac8d4bd777a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 25 Dec 2021 00:58:25 -0700 Subject: [PATCH] term: ignore first OSC title change sequence on Windows ConPTY emits a sequence that sets the title to the name of the program that is initially launched into it. This commit tries to ignore that sequence in that circumstance, so that the logic in b5d156c282a01f2f1cb10aee98655a4a2c2c71e5 can more dynamically set the tab title. --- Cargo.lock | 1 + mux/src/domain.rs | 22 ++++++++++++++++++++-- pty/Cargo.toml | 1 + pty/src/lib.rs | 4 +++- term/src/terminalstate/mod.rs | 11 +++++++++++ term/src/terminalstate/performer.rs | 14 ++++++++++++++ 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41b5848ce..a682f460a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2945,6 +2945,7 @@ version = "0.7.0" dependencies = [ "anyhow", "bitflags", + "downcast-rs", "filedescriptor", "futures", "lazy_static", diff --git a/mux/src/domain.rs b/mux/src/domain.rs index 8c3653ce7..aa77a0d8e 100644 --- a/mux/src/domain.rs +++ b/mux/src/domain.rs @@ -106,6 +106,18 @@ impl LocalDomain { name: name.to_string(), } } + + #[cfg(unix)] + fn is_conpty(&self) -> bool { + false + } + + #[cfg(windows)] + fn is_conpty(&self) -> bool { + self.pty_system + .downcast_ref::() + .is_some() + } } #[async_trait(?Send)] @@ -148,13 +160,16 @@ impl Domain for LocalDomain { let writer = pair.master.try_clone_writer()?; let mux = Mux::get().unwrap(); - let terminal = wezterm_term::Terminal::new( + let mut terminal = wezterm_term::Terminal::new( crate::pty_size_to_terminal_size(size), std::sync::Arc::new(config::TermConfig::new()), "WezTerm", config::wezterm_version(), Box::new(writer), ); + if self.is_conpty() { + terminal.set_supress_initial_title_change(); + } let pane: Rc = Rc::new(LocalPane::new( pane_id, @@ -230,13 +245,16 @@ impl Domain for LocalDomain { let writer = pair.master.try_clone_writer()?; - let terminal = wezterm_term::Terminal::new( + let mut terminal = wezterm_term::Terminal::new( crate::pty_size_to_terminal_size(split_size.second), std::sync::Arc::new(config::TermConfig::new()), "WezTerm", config::wezterm_version(), Box::new(writer), ); + if self.is_conpty() { + terminal.set_supress_initial_title_change(); + } let pane: Rc = Rc::new(LocalPane::new( pane_id, diff --git a/pty/Cargo.toml b/pty/Cargo.toml index 8980d0bab..1e6360866 100644 --- a/pty/Cargo.toml +++ b/pty/Cargo.toml @@ -10,6 +10,7 @@ documentation = "https://docs.rs/portable-pty" [dependencies] anyhow = "1.0" +downcast-rs = "1.0" filedescriptor = { version="0.8", path = "../filedescriptor" } log = "0.4" libc = "0.2" diff --git a/pty/src/lib.rs b/pty/src/lib.rs index acd4ef008..08dca0572 100644 --- a/pty/src/lib.rs +++ b/pty/src/lib.rs @@ -44,6 +44,7 @@ //! session with an implementation of `PtySystem`, allowing //! you to use the same pty interface with remote ptys. use anyhow::Error; +use downcast_rs::{impl_downcast, Downcast}; #[cfg(unix)] use libc; #[cfg(feature = "serde_support")] @@ -191,12 +192,13 @@ pub struct PtyPair { /// The `PtySystem` trait allows an application to work with multiple /// possible Pty implementations at runtime. This is important on /// Windows systems which have a variety of implementations. -pub trait PtySystem { +pub trait PtySystem: Downcast { /// Create a new Pty instance with the window size set to the specified /// dimensions. Returns a (master, slave) Pty pair. The master side /// is used to drive the slave side. fn openpty(&self, size: PtySize) -> anyhow::Result; } +impl_downcast!(PtySystem); impl Child for std::process::Child { fn try_wait(&mut self) -> IoResult> { diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs index 7b40b8daa..b5bce2ca7 100644 --- a/term/src/terminalstate/mod.rs +++ b/term/src/terminalstate/mod.rs @@ -347,6 +347,12 @@ pub struct TerminalState { /// The unicode version that is in effect unicode_version: UnicodeVersion, unicode_version_stack: Vec, + + /// On Windows, the ConPTY layer emits an OSC sequence to + /// set the title shortly after it starts up. + /// We don't want that, so we use this flag to remember + /// whether we want to skip it or not. + suppress_initial_title_change: bool, } #[derive(Debug)] @@ -484,9 +490,14 @@ impl TerminalState { seqno: 0, unicode_version, unicode_version_stack: vec![], + suppress_initial_title_change: false, } } + pub fn set_supress_initial_title_change(&mut self) { + self.suppress_initial_title_change = true; + } + pub fn current_seqno(&self) -> SequenceNo { self.seqno } diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs index a3e834227..9ebd44509 100644 --- a/term/src/terminalstate/performer.rs +++ b/term/src/terminalstate/performer.rs @@ -178,6 +178,19 @@ impl<'a> Performer<'a> { pub fn perform(&mut self, action: Action) { debug!("perform {:?}", action); + if self.suppress_initial_title_change { + match &action { + Action::OperatingSystemCommand(osc) => match **osc { + OperatingSystemCommand::SetIconNameAndWindowTitle(_) => { + debug!("suppressed {:?}", osc); + self.suppress_initial_title_change = false; + return; + } + _ => {} + }, + _ => {} + } + } match action { Action::Print(c) => self.print(c), Action::Control(code) => self.control(code), @@ -516,6 +529,7 @@ impl<'a> Performer<'a> { self.left_and_right_margins = 0..self.screen().physical_cols; self.unicode_version = UnicodeVersion(self.config.unicode_version()); self.unicode_version_stack.clear(); + self.suppress_initial_title_change = false; self.screen.activate_primary_screen(seqno); self.erase_in_display(EraseInDisplay::EraseScrollback);