This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
crock/clock-tui/src/app/modes/clock.rs
Jimmy d2daa965eb
add xtasks to generate man page and autocomplete files (#22)
* add xtasks to generate manpage and autocomplete files

* add alias for main pkg

* git ingore assets/gen
2022-09-04 21:13:56 +08:00

46 lines
1.4 KiB
Rust

use chrono::{Local, Utc};
use chrono_tz::Tz;
use crate::bricks_text::BricksText;
use tui::{layout::Rect, style::Style, widgets::Widget};
use super::render_centered;
pub(crate) struct Clock {
pub size: u16,
pub style: Style,
pub show_date: bool,
pub show_millis: bool,
pub show_secs: bool,
pub timezone: Option<Tz>,
}
impl Widget for &Clock {
fn render(self, area: Rect, buf: &mut tui::buffer::Buffer) {
let now = if let Some(ref tz) = self.timezone {
Utc::now().with_timezone(tz).naive_local()
} else {
Local::now().naive_local()
};
let mut time_str = now.format("%H:%M:%S%.3f").to_string();
if self.show_millis {
time_str.truncate(time_str.len() - 2);
} else if self.show_secs {
time_str.truncate(time_str.len() - 4);
} else {
time_str.truncate(time_str.len() - 7);
};
let time_str = time_str.as_str();
let text = BricksText::new(time_str, self.size, self.size, self.style);
let header = if self.show_date {
let mut title = now.format("%Y-%m-%d").to_string();
if let Some(tz) = self.timezone {
title.push(' ');
title.push_str(tz.name());
}
Some(title)
} else {
None
};
render_centered(area, buf, &text, header, None);
}
}