From b6b810e2325714c4e3d555b3a2eed6660f5a8658 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 29 Jul 2025 14:43:50 -0300 Subject: [PATCH] feat: use isatty::tty_path for TTY detection in meta plugin Co-authored-by: aider (openai/andrew.openrouter.qwen.qwen3-coder) --- Cargo.toml | 1 + src/meta_plugin/system.rs | 64 +++++---------------------------------- 2 files changed, 9 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 553b1d8..44a90d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ sha2 = "0.10.0" local-ip-address = "0.5.5" dns-lookup = "2.0.2" uzers = "0.11.3" +isatty = "0.1.9" [dev-dependencies] tempfile = "3.3.0" diff --git a/src/meta_plugin/system.rs b/src/meta_plugin/system.rs index a90cdff..689b564 100644 --- a/src/meta_plugin/system.rs +++ b/src/meta_plugin/system.rs @@ -378,30 +378,15 @@ impl MetaPlugin for TtyMetaPlugin { } fn finalize(&mut self) -> io::Result { - // Try to get TTY from /proc/self/stat - match std::fs::read_to_string("/proc/self/stat") { - Ok(stat_content) => { - // The 7th field (index 6) in /proc/self/stat contains the controlling terminal (TTY) - let fields: Vec<&str> = stat_content.split_whitespace().collect(); - if fields.len() > 6 { - if let Ok(tty_nr) = fields[6].parse::() { - if tty_nr != 0 { - // Convert tty_nr to device path - match self.tty_name(tty_nr) { - Some(tty_name) => return Ok(tty_name), - None => return Ok("unknown tty".to_string()), - } - } - } + // Use isatty::tty_path to get the current TTY path + match isatty::stdout_isatty() { + true => { + match isatty::tty_path() { + Ok(path) => Ok(path.to_string_lossy().to_string()), + Err(_) => Ok("unknown tty".to_string()), } - } - Err(_) => {} - } - - // Fallback: try to get TTY from /dev/tty - match std::fs::read_link("/dev/tty") { - Ok(path) => Ok(format!("/dev/{}", path.to_string_lossy())), - Err(_) => Ok("not a tty".to_string()), + }, + false => Ok("not a tty".to_string()), } } @@ -413,36 +398,3 @@ impl MetaPlugin for TtyMetaPlugin { self.meta_name.clone() } } - -impl TtyMetaPlugin { - // Convert tty device number to device name - fn tty_name(&self, tty_nr: u32) -> Option { - let major = (tty_nr >> 8) as u16; - let minor = (tty_nr & 0xff) as u16; - - // Handle common TTY device types - match major { - 4 => { - // TTY devices - if minor < 64 { - Some(format!("/dev/tty{}", minor)) - } else { - None - } - }, - 136..=143 => { - // PTS devices - Some(format!("/dev/pts/{}", minor)) - }, - 3 => { - // TTY devices - if minor < 64 { - Some(format!("/dev/tty{}", minor)) - } else { - None - } - }, - _ => None, - } - } -}