use std::{ borrow::Cow, ops::Deref, time::{Duration, Instant}, }; pub struct StatusApp { message: String, timestamp: Option<(Instant, Duration)>, } impl StatusApp { pub const fn new() -> Self { Self { message: String::new(), timestamp: None, } } pub fn submit_message(&mut self, message: String, duration: Option) { if message.is_empty() { self.message.clear(); self.timestamp = None; } else { self.message = message; self.timestamp = duration.map(|dur| (Instant::now(), dur)); } } pub fn get_message_update(&mut self) -> Option> { if let Some((time, dur)) = self.timestamp { if time.elapsed() > dur { self.timestamp = None; let message = std::mem::take(&mut self.message); return Some(Cow::Owned(message)); } } (!self.message.is_empty()).then(|| Cow::Borrowed(self.message.deref())) } }