errors
This commit is contained in:
parent
d8e445b5f7
commit
4a7817a239
4 changed files with 82 additions and 8 deletions
50
src/tui/widgets/last_error.rs
Normal file
50
src/tui/widgets/last_error.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use std::{cell::RefCell, rc::Rc, time::Instant};
|
||||
|
||||
use ratatui::widgets::{Paragraph, Widget};
|
||||
|
||||
use crate::tui::widgets::styled::Styled;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LastError {
|
||||
inner: Rc<RefCell<Option<(String, Instant)>>>,
|
||||
}
|
||||
|
||||
impl LastError {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Rc::new(RefCell::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&self, s: String) {
|
||||
*self.inner.borrow_mut() = Some((s, Instant::now()));
|
||||
}
|
||||
|
||||
pub fn check_expired(&self) {
|
||||
let inner = self.inner.borrow();
|
||||
if let Some((_, time)) = *inner {
|
||||
if Instant::now().duration_since(time).as_secs_f64() > 1.0 {
|
||||
drop(inner);
|
||||
*self.inner.borrow_mut() = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Styled<'_, LastError> {
|
||||
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Paragraph::new(
|
||||
self.inner
|
||||
.inner
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.map(|i| i.0.replace("\n", ""))
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.style(self.styles.error)
|
||||
.render(area, buf);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue