feat: use isatty::tty_path for TTY detection in meta plugin

Co-authored-by: aider (openai/andrew.openrouter.qwen.qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-07-29 14:43:50 -03:00
parent f58a98ddae
commit b6b810e232
2 changed files with 9 additions and 56 deletions

View File

@@ -40,6 +40,7 @@ sha2 = "0.10.0"
local-ip-address = "0.5.5" local-ip-address = "0.5.5"
dns-lookup = "2.0.2" dns-lookup = "2.0.2"
uzers = "0.11.3" uzers = "0.11.3"
isatty = "0.1.9"
[dev-dependencies] [dev-dependencies]
tempfile = "3.3.0" tempfile = "3.3.0"

View File

@@ -378,30 +378,15 @@ impl MetaPlugin for TtyMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
// Try to get TTY from /proc/self/stat // Use isatty::tty_path to get the current TTY path
match std::fs::read_to_string("/proc/self/stat") { match isatty::stdout_isatty() {
Ok(stat_content) => { true => {
// The 7th field (index 6) in /proc/self/stat contains the controlling terminal (TTY) match isatty::tty_path() {
let fields: Vec<&str> = stat_content.split_whitespace().collect(); Ok(path) => Ok(path.to_string_lossy().to_string()),
if fields.len() > 6 { Err(_) => Ok("unknown tty".to_string()),
if let Ok(tty_nr) = fields[6].parse::<u32>() {
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()),
}
}
}
} }
} },
Err(_) => {} false => Ok("not a tty".to_string()),
}
// 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()),
} }
} }
@@ -413,36 +398,3 @@ impl MetaPlugin for TtyMetaPlugin {
self.meta_name.clone() self.meta_name.clone()
} }
} }
impl TtyMetaPlugin {
// Convert tty device number to device name
fn tty_name(&self, tty_nr: u32) -> Option<String> {
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,
}
}
}