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:
@@ -378,30 +378,15 @@ impl MetaPlugin for TtyMetaPlugin {
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> io::Result<String> {
|
||||
// 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::<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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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<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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user