show tips when paused

This commit is contained in:
race604 2022-07-20 12:37:13 +08:00
parent 7ce5a117f2
commit 104dab5b2c
5 changed files with 61 additions and 36 deletions

View file

@ -9,7 +9,13 @@ pub(crate) use clock::Clock;
use clock_tui::bricks_text::BricksText; use clock_tui::bricks_text::BricksText;
pub(crate) use stopwatch::Stopwatch; pub(crate) use stopwatch::Stopwatch;
pub(crate) use timer::Timer; pub(crate) use timer::Timer;
use tui::{buffer::Buffer, layout::Rect, widgets::Widget}; use tui::{
buffer::Buffer,
layout::Rect,
style::Style,
text::Span,
widgets::{Paragraph, Widget},
};
fn format_duration(duration: Duration) -> String { fn format_duration(duration: Duration) -> String {
let millis = duration.num_milliseconds(); let millis = duration.num_milliseconds();
@ -30,13 +36,44 @@ fn format_duration(duration: Duration) -> String {
result result
} }
fn render_centered(area: Rect, buf: &mut Buffer, text: &BricksText) { fn render_centered(
area: Rect,
buf: &mut Buffer,
text: &BricksText,
header: Option<String>,
footer: Option<String>,
) {
let text_size = text.size(); let text_size = text.size();
let text_area = Rect { let text_area = Rect {
x: area.x + (area.width.saturating_sub(text_size.0)) / 2, x: area.x + (area.width.saturating_sub(text_size.0)) / 2,
y: area.y + (area.height.saturating_sub(text_size.1)) / 2, y: area.y + (area.height.saturating_sub(text_size.1)) / 2,
width: min(text_size.0, area.width), width: min(text_size.0, area.width),
height: min(text_size.0, area.height), height: min(text_size.1, area.height),
}; };
text.render(text_area, buf); text.render(text_area, buf);
let render_text_center = |text: &str, top: u16, buf: &mut Buffer| {
let text_len = text.len() as u16;
let paragrahp = Paragraph::new(Span::from(text)).style(Style::default());
let para_area = Rect {
x: area.left() + (area.width.saturating_sub(text_len)) / 2,
y: top,
width: min(text_len, area.width),
height: min(1, area.height),
};
paragrahp.render(para_area, buf);
};
if let Some(text) = header {
if area.top() + 2 <= text_area.top() {
render_text_center(text.as_str(), text_area.top() - 2, buf);
}
}
if let Some(text) = footer {
if area.bottom() >= text_area.bottom() + 2 {
render_text_center(text.as_str(), text_area.bottom() + 1, buf);
}
}
} }

View file

@ -1,13 +1,8 @@
use std::cmp::min;
use chrono::Local; use chrono::Local;
use clock_tui::bricks_text::BricksText; use clock_tui::bricks_text::BricksText;
use tui::{ use tui::{layout::Rect, style::Style, widgets::Widget};
layout::Rect,
style::Style, use super::render_centered;
text::Span,
widgets::{Paragraph, Widget},
};
pub(crate) struct Clock { pub(crate) struct Clock {
pub size: u16, pub size: u16,
@ -28,27 +23,11 @@ impl Widget for &Clock {
}; };
let time_str = time_str.as_str(); let time_str = time_str.as_str();
let text = BricksText::new(time_str, self.size, self.size, self.style); let text = BricksText::new(time_str, self.size, self.size, self.style);
let text_size = text.size(); let header = if self.show_date {
let text_area = Rect { Some(now.format("%Y-%m-%d %Z").to_string())
x: area.x + (area.width.saturating_sub(text_size.0)) / 2, } else {
y: area.y + (area.height.saturating_sub(text_size.1)) / 2, None
width: min(text_size.0, area.width),
height: min(text_size.0, area.height),
}; };
text.render(text_area, buf); render_centered(area, buf, &text, header, None);
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);
}
} }
} }

View file

@ -53,6 +53,11 @@ impl Widget for &Stopwatch {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
let time_str = format_duration(self.total_time()); let time_str = format_duration(self.total_time());
let text = BricksText::new(time_str.as_str(), self.size, self.size, self.style); let text = BricksText::new(time_str.as_str(), self.size, self.size, self.style);
render_centered(area, buf, &text); let footer = if self.is_paused() {
Some("PAUSED (press <SPACE> to resume)".to_string())
} else {
None
};
render_centered(area, buf, &text, None, footer);
} }
} }

View file

@ -59,8 +59,12 @@ impl Timer {
impl Widget for &Timer { impl Widget for &Timer {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
let time_str = format_duration(self.remaining_time()); let time_str = format_duration(self.remaining_time());
// println!("{}", time_str);
let text = BricksText::new(time_str.as_str(), self.size, self.size, self.style); let text = BricksText::new(time_str.as_str(), self.size, self.size, self.style);
render_centered(area, buf, &text); let footer = if self.is_paused() {
Some("PAUSED (press <SPACE> to resume)".to_string())
} else {
None
};
render_centered(area, buf, &text, None, footer);
} }
} }

View file

@ -65,7 +65,7 @@ fn main() -> Result<(), Box<dyn Error>> {
} else { } else {
false false
}; };
let tick_rate = Duration::from_millis(if low_rate { 20 } else { 200 }); let tick_rate = Duration::from_millis(if low_rate { 200 } else { 20 });
let res = run_app(&mut terminal, &mut app, tick_rate); let res = run_app(&mut terminal, &mut app, tick_rate);
// restore terminal // restore terminal