tty_handoff: extract Midnight Commander workaround

A following commit wants to run the full initialize_tty_metadata()
only after querying XTVERSION.

But MC_TMPDIR needs to be checked before querying for XTVERSION.

Remove this cyclic dependency by extracting the MC_TMPDIR check.
This commit is contained in:
Johannes Altmanninger
2025-09-21 20:51:20 +02:00
parent a6a38b78d6
commit fb0e17d6ea
2 changed files with 18 additions and 27 deletions

View File

@@ -148,7 +148,7 @@
};
use crate::tty_handoff::{
get_kitty_keyboard_capability, get_tty_protocols_active, initialize_tty_metadata,
safe_deactivate_tty_protocols, set_kitty_keyboard_capability, tty_metadata, TtyHandoff,
safe_deactivate_tty_protocols, set_kitty_keyboard_capability, TtyHandoff,
};
use crate::wchar::prelude::*;
use crate::wcstringutil::string_prefixes_string_maybe_case_insensitive;
@@ -265,19 +265,20 @@ pub(crate) fn initial_query(
vars: Option<&dyn Environment>,
) {
blocking_query.get_or_init(|| {
let md = tty_metadata();
let query = if is_dumb() || md.in_midnight_commander || !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)
};
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)
};
RefCell::new(query)
});
}

View File

@@ -23,9 +23,6 @@
// Facts about our environment, which inform how we handle the tty.
#[derive(Debug, Copy, Clone)]
pub struct TtyMetadata {
// Whether we are running under Midnight Commander.
pub in_midnight_commander: bool,
// Whether we are running under tmux.
pub in_tmux: bool,
@@ -38,13 +35,11 @@ impl TtyMetadata {
fn detect() -> Self {
use std::env::var_os;
let in_midnight_commander = var_os("MC_TMPDIR").is_some();
let in_tmux = var_os("TMUX").is_some();
// Detect iTerm2 before 3.5.12.
let pre_kitty_iterm2 = get_iterm2_version().is_some_and(|v| v < (3, 5, 12));
Self {
in_midnight_commander,
in_tmux,
pre_kitty_iterm2,
}
@@ -113,7 +108,7 @@ impl TtyProtocolsSet {
// Get commands to enable or disable TTY protocols, based on the metadata
// and the KITTY_KEYBOARD_SUPPORTED global variable.
// THIS IS USED FROM A SIGNAL HANDLER.
pub fn safe_get_commands(&self, enable: bool) -> &[u8] {
fn safe_get_commands(&self, enable: bool) -> &[u8] {
let protocol = self.md.safe_get_supported_protocol();
let cmds = if enable {
&self.enablers
@@ -227,14 +222,9 @@ fn get_or_init_tty_protocols() -> &'static TtyProtocolsSet {
unsafe { &*p }
}
// Get the TTY metadata, initializing it if necessary.
pub fn tty_metadata() -> TtyMetadata {
get_or_init_tty_protocols().md
}
// Cover to merely initialize the TTY metadata, for clarity at call sites.
// Initialize TTY metadata.
pub fn initialize_tty_metadata() {
tty_metadata();
get_or_init_tty_protocols();
}
// A marker of the current state of the tty protocols.