generated from PlexSheep/rs-base
support non_date and millis options for Clock
This commit is contained in:
parent
b8d6e42c4f
commit
7ce5a117f2
3 changed files with 38 additions and 17 deletions
19
src/app.rs
19
src/app.rs
|
@ -16,7 +16,14 @@ pub(crate) mod modes;
|
|||
#[derive(Debug, Subcommand)]
|
||||
pub(crate) enum Mode {
|
||||
/// The clock mode displays the current time, the default mode.
|
||||
Clock,
|
||||
Clock {
|
||||
/// Do not show date
|
||||
#[clap(short = 'D', long, takes_value = false)]
|
||||
no_date: bool,
|
||||
/// Show milliseconds
|
||||
#[clap(short, long, takes_value = false)]
|
||||
millis: bool,
|
||||
},
|
||||
/// The timer mode displays the remaining time until the timer is finished.
|
||||
Timer {
|
||||
#[clap(short, long, value_parser = parse_duration, default_value = "5m")]
|
||||
|
@ -47,13 +54,17 @@ pub(crate) struct App {
|
|||
impl App {
|
||||
pub fn init_app(&mut self) {
|
||||
let style = Style::default().fg(self.color);
|
||||
let mode = self.mode.as_ref().unwrap_or(&Mode::Clock);
|
||||
let mode = self.mode.as_ref().unwrap_or(&Mode::Clock {
|
||||
no_date: false,
|
||||
millis: false,
|
||||
});
|
||||
match mode {
|
||||
Mode::Clock => {
|
||||
Mode::Clock { no_date, millis } => {
|
||||
self.clock = Some(Clock {
|
||||
size: self.size,
|
||||
style,
|
||||
long: false,
|
||||
show_date: !no_date.to_owned(),
|
||||
show_millis: millis.to_owned(),
|
||||
});
|
||||
}
|
||||
Mode::Timer { duration } => {
|
||||
|
|
|
@ -12,13 +12,14 @@ use tui::{
|
|||
pub(crate) struct Clock {
|
||||
pub size: u16,
|
||||
pub style: Style,
|
||||
pub long: bool,
|
||||
pub show_date: bool,
|
||||
pub show_millis: bool,
|
||||
}
|
||||
|
||||
impl Widget for &Clock {
|
||||
fn render(self, area: Rect, buf: &mut tui::buffer::Buffer) {
|
||||
let now = Local::now();
|
||||
let time_str = if self.long {
|
||||
let time_str = if self.show_millis {
|
||||
let mut str = now.format("%H:%M:%S%.3f").to_string();
|
||||
str.truncate(str.len() - 2);
|
||||
str
|
||||
|
@ -35,16 +36,19 @@ impl Widget for &Clock {
|
|||
height: min(text_size.0, area.height),
|
||||
};
|
||||
text.render(text_area, buf);
|
||||
let text = now.format("%Y-%m-%d %Z").to_string();
|
||||
let text_len = text.as_str().len() as u16;
|
||||
let paragrahp = Paragraph::new(Span::from(text)).style(Style::default());
|
||||
|
||||
let para_area = Rect {
|
||||
x: area.x + (area.width.saturating_sub(text_len)) / 2,
|
||||
y: text_area.y.saturating_sub(2),
|
||||
width: min(text_len, area.width),
|
||||
height: min(1, area.height),
|
||||
};
|
||||
paragrahp.render(para_area, buf);
|
||||
if self.show_date {
|
||||
let text = now.format("%Y-%m-%d %Z").to_string();
|
||||
let text_len = text.as_str().len() as u16;
|
||||
let paragrahp = Paragraph::new(Span::from(text)).style(Style::default());
|
||||
|
||||
let para_area = Rect {
|
||||
x: area.x + (area.width.saturating_sub(text_len)) / 2,
|
||||
y: text_area.y.saturating_sub(2),
|
||||
width: min(text_len, area.width),
|
||||
height: min(1, area.height),
|
||||
};
|
||||
paragrahp.render(para_area, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::{
|
|||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use app::Mode;
|
||||
use clap::Parser;
|
||||
use crossterm::{
|
||||
event::{self, Event, KeyCode},
|
||||
|
@ -59,7 +60,12 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
let mut terminal = Terminal::new(backend)?;
|
||||
|
||||
// create app and run it
|
||||
let tick_rate = Duration::from_millis(100);
|
||||
let low_rate = if let Some(Mode::Clock { millis, .. }) = app.mode {
|
||||
!millis
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let tick_rate = Duration::from_millis(if low_rate { 20 } else { 200 });
|
||||
let res = run_app(&mut terminal, &mut app, tick_rate);
|
||||
|
||||
// restore terminal
|
||||
|
|
Reference in a new issue