diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index fd44dd1..a7baef8 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -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 MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new()), MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new()), MetaPluginType::FullHostname => Box::new(FullHostnameMetaPlugin::new()), + MetaPluginType::Tty => Box::new(TtyMetaPlugin::new()), } } diff --git a/src/meta_plugin/system.rs b/src/meta_plugin/system.rs index 73d0b8e..b57ab22 100644 --- a/src/meta_plugin/system.rs +++ b/src/meta_plugin/system.rs @@ -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> { + Ok(Box::new(io::sink())) + } + + fn finalize(&mut self) -> io::Result { + // 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() + } +}