From 7a14dd0b7f949b2fed9f6790045bbf66bc2f2a9d Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Tue, 23 Sep 2025 18:50:58 +0200 Subject: [PATCH] Be more consistent about when to query the TTY We are more conservative with querying on startup than we are with querying for cursor position. Part of this is oversight (if startup querying is not done, we basically never get into a position where we query for cursor position, outside extreme edge cases). Also, some specific scenarios where we query for cursor position inside, say, Midnight Commander, are not actually broken, because that only happens when Midnight Commander gives us control. But let's favor consistency for now; the Midnight Commander issue should be fixed soon anyway. Use the same logic in both cases. --- src/reader.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 310589903..74ad80869 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -259,6 +259,10 @@ fn redirect_tty_after_sighup() { } } +fn querying_allowed() -> bool { + !is_dumb() && std::env::var_os("MC_TMPDIR").is_none() && isatty(STDOUT_FILENO) +} + pub(crate) fn initial_query( blocking_query: &OnceCell>>, out: &mut impl Output, @@ -266,19 +270,18 @@ pub(crate) fn initial_query( ) { blocking_query.get_or_init(|| { initialize_tty_metadata(); - let query = - if is_dumb() || std::env::var_os("MC_TMPDIR").is_some() || !isatty(STDOUT_FILENO) { - None - } else { - // Query for kitty keyboard protocol support. - out.write_command(QueryKittyKeyboardProgressiveEnhancements); - out.write_command(QueryXtversion); - if let Some(vars) = vars { - query_capabilities_via_dcs(out.by_ref(), vars); - } - out.write_command(QueryPrimaryDeviceAttribute); - Some(TerminalQuery::Initial) - }; + let query = if !querying_allowed() { + None + } else { + // Query for kitty keyboard protocol support. + out.write_command(QueryKittyKeyboardProgressiveEnhancements); + out.write_command(QueryXtversion); + if let Some(vars) = vars { + query_capabilities_via_dcs(out.by_ref(), vars); + } + out.write_command(QueryPrimaryDeviceAttribute); + Some(TerminalQuery::Initial) + }; RefCell::new(query) }); } @@ -1542,7 +1545,7 @@ pub(crate) fn blocking_query(&self) -> RefMut<'_, Option> { } pub fn request_cursor_position(&mut self, out: &mut Outputter, q: CursorPositionQuery) { - if !isatty(STDOUT_FILENO) { + if !querying_allowed() { return; } let mut query = self.blocking_query();