feat: add tty meta plugin to get current terminal device

Co-authored-by: aider (openai/andrew.openrouter.qwen.qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-07-29 14:38:09 -03:00
parent 291b6d9587
commit 8a2468522a
2 changed files with 44 additions and 0 deletions

View File

@@ -33,6 +33,7 @@ pub enum MetaPluginType {
ReadRate,
Hostname,
FullHostname,
Tty,
}
pub trait MetaPlugin {
@@ -75,6 +76,7 @@ pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box<dyn MetaPlugin>
MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new()),
MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new()),
MetaPluginType::FullHostname => Box::new(FullHostnameMetaPlugin::new()),
MetaPluginType::Tty => Box::new(TtyMetaPlugin::new()),
}
}

View File

@@ -358,3 +358,45 @@ impl MetaPlugin for FullHostnameMetaPlugin {
self.meta_name.clone()
}
}
#[derive(Debug, Clone, Default)]
pub struct TtyMetaPlugin {
meta_name: String,
}
impl TtyMetaPlugin {
pub fn new() -> TtyMetaPlugin {
TtyMetaPlugin {
meta_name: "tty".to_string(),
}
}
}
impl MetaPlugin for TtyMetaPlugin {
fn create(&self) -> Result<Box<dyn Write>> {
Ok(Box::new(io::sink()))
}
fn finalize(&mut self) -> io::Result<String> {
// Try to get the TTY from the TTY environment variable first
if let Ok(tty) = env::var("TTY") {
if !tty.is_empty() {
return Ok(tty);
}
}
// Try to get TTY from /dev/tty (this will give us the current terminal)
match std::fs::read_link("/dev/tty") {
Ok(path) => Ok(format!("/dev/{}", path.to_string_lossy())),
Err(_) => Ok("not a tty".to_string()),
}
}
fn update(&mut self, _data: &[u8]) {
// No update needed for TTY
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
}