| 1 | use super::Block; |
| 2 | use anyhow::Result; |
| 3 | use chrono::Local; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | pub struct Clock { |
| 7 | format: &'static str, |
| 8 | color: u32, |
| 9 | } |
| 10 | |
| 11 | impl Clock { |
| 12 | pub fn new(format: &'static str, color: u32) -> Self { |
| 13 | Self { format, color } |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | impl Block for Clock { |
| 18 | fn content(&mut self) -> Result<String> { |
| 19 | let now = Local::now(); |
| 20 | Ok(now.format(self.format).to_string()) |
| 21 | } |
| 22 | |
| 23 | fn interval(&self) -> Duration { |
| 24 | Duration::from_secs(1) |
| 25 | } |
| 26 | |
| 27 | fn color(&self) -> u32 { |
| 28 | self.color |
| 29 | } |
| 30 | } |