From bdd478bbd0f8390626475359246218ff53be6304 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Thu, 18 Apr 2024 10:21:28 +0200 Subject: [PATCH] Disable focus reporting on non-tmux again for now We sometimes leak ^[[I and ^[[O focus reporting events when run from VSCode's "Run python file" button in the top right corner. To reproduce I installed the ms-python extension set the VSCode default shell to fish and repeatedly ran a script that does "time.sleep(1)". I believe VSCode synthesizes keys and triggers a race condition. We can probably fix this but I'm not sure when I'll get to it (given how relatively unimportant this feature is). So let's go back to the old behavior of only enabling focus reporting in tmux. I believe that tmux is affected by the same VSCode issue (also on 3.7.1 I think) but I haven't been able to get tmux to emit focus reporting sequences yet. Still, keep it to not regress cursor shape (#4788). So far this is the only motivation for focus reporting and I believe it is only relevant for terminals that can split windows (though there are a bunch that do). Closes #10448 --- CHANGELOG.rst | 4 +--- src/input_common.rs | 6 ++++++ src/reader.rs | 3 +++ tests/pexpects/read.py | 6 +++--- tests/pexpects/signals.py | 2 +- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f2c7a812c..4183199ec 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -150,9 +150,7 @@ Improved terminal support - Fish now sets the terminal window title (via OSC 0) unconditionally instead of only for some terminals (:issue:`10037`). - Fish now marks the prompt and command-output regions (via OSC 133) to enable terminal shell integration (:issue:`10352`). Shell integration shortcuts can scroll to the next/previous prompt or show the last command output in a pager. -- Focus reporting is enabled unconditionally, not just inside tmux. - To use it, define functions that handle events ``fish_focus_in`` and ``fish_focus_out``. -- Focus reporting is no longer disabled on the first prompt. +- Focus reporting in tmux is no longer disabled on the first prompt. Other improvements ------------------ diff --git a/src/input_common.rs b/src/input_common.rs index 465bf8301..e32576d73 100644 --- a/src/input_common.rs +++ b/src/input_common.rs @@ -7,6 +7,7 @@ use crate::common::{ use crate::env::{EnvStack, Environment}; use crate::fd_readable_set::FdReadableSet; use crate::flog::FLOG; +use crate::global_safety::RelaxedAtomicBool; use crate::input::KeyNameStyle; use crate::key::{ self, alt, canonicalize_control_char, canonicalize_keyed_control_char, function_key, shift, @@ -515,7 +516,12 @@ fn terminal_protocols_disable_impl() { let _ = write_to_fd(sequences.as_bytes(), STDOUT_FILENO); } +pub(crate) static IS_TMUX: RelaxedAtomicBool = RelaxedAtomicBool::new(false); + pub(crate) fn focus_events_enable_ifn() { + if !IS_TMUX.load() { + return; + } let mut term_protocols = TERMINAL_PROTOCOLS.get().borrow_mut(); let Some(term_protocols) = term_protocols.as_mut() else { panic!() diff --git a/src/reader.rs b/src/reader.rs index 4bce967db..2dd358ac4 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -71,6 +71,7 @@ use crate::history::{ }; use crate::input::init_input; use crate::input::Inputter; +use crate::input_common::IS_TMUX; use crate::input_common::{ focus_events_enable_ifn, terminal_protocols_enable_scoped, CharEvent, CharInputStyle, ReadlineCmd, @@ -3660,6 +3661,8 @@ fn reader_interactive_init(parser: &Parser) { parser .vars() .set_one(L!("_"), EnvMode::GLOBAL, L!("fish").to_owned()); + + IS_TMUX.store(parser.vars().get_unless_empty(L!("TMUX")).is_some()); } /// Destroy data for interactive use. diff --git a/tests/pexpects/read.py b/tests/pexpects/read.py index 952065ef6..6488df56c 100644 --- a/tests/pexpects/read.py +++ b/tests/pexpects/read.py @@ -13,7 +13,7 @@ send, sendline, sleep, expect_prompt, expect_re, expect_str = ( def expect_read_prompt(): - expect_re(r"\r\n?read> \x1b\[\?1004h$") + expect_re(r"\r\n?read> (\x1b\[\?1004h)?$") def expect_marker(text): @@ -56,12 +56,12 @@ print_var_contents("foo", "bar") # read -c (see #8633) sendline(r"read -c init_text somevar && echo $somevar") -expect_re(r"\r\n?read> init_text\x1b\[\?1004h$") +expect_re(r"\r\n?read> init_text(\x1b\[\?1004h)?$") sendline("someval") expect_prompt("someval\r\n") sendline(r"read --command='some other text' somevar && echo $somevar") -expect_re(r"\r\n?read> some other text\x1b\[\?1004h$") +expect_re(r"\r\n?read> some other text(\x1b\[\?1004h)?$") sendline("another value") expect_prompt("another value\r\n") diff --git a/tests/pexpects/signals.py b/tests/pexpects/signals.py index ef3ea1c69..7c03be3f9 100644 --- a/tests/pexpects/signals.py +++ b/tests/pexpects/signals.py @@ -47,7 +47,7 @@ expect_prompt() sendline("function postexec --on-event fish_postexec; echo fish_postexec spotted; end") expect_prompt() sendline("read") -expect_re(r"\r\n?read> \x1b\[\?1004h$") +expect_re(r"\r\n?read> (\x1b\[\?1004h)?$") sleep(0.200) os.kill(sp.spawn.pid, signal.SIGINT) expect_str("fish_postexec spotted")