Render time via widget

This commit is contained in:
Joscha 2022-07-31 15:31:55 +02:00
parent 82fce0430d
commit 6f4d94afa5
2 changed files with 39 additions and 0 deletions

View file

@ -1,3 +1,5 @@
mod time;
use std::sync::Arc;
use async_trait::async_trait;

37
src/ui/chat/tree/time.rs Normal file
View file

@ -0,0 +1,37 @@
use crossterm::style::{ContentStyle, Stylize};
use time::format_description::FormatItem;
use time::macros::format_description;
use crate::euph::api::Time;
use crate::ui::widgets::background::Background;
use crate::ui::widgets::text::Text;
use crate::ui::widgets::BoxedWidget;
const TIME_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day] [hour]:[minute]");
const TIME_EMPTY: &str = " ";
fn style() -> ContentStyle {
ContentStyle::default().grey()
}
fn style_inverted() -> ContentStyle {
ContentStyle::default().black().on_white()
}
pub fn widget(time: Option<Time>, highlighted: bool) -> BoxedWidget {
let text = if let Some(time) = time {
time.0.format(TIME_FORMAT).expect("could not format time")
} else {
TIME_EMPTY.to_string()
};
let style = if highlighted {
style_inverted()
} else {
style()
};
Background::new(Text::new((text, style)))
.style(style)
.into()
}