handle hours (#15)

This commit is contained in:
Jens K. 2024-12-17 11:16:53 +01:00 committed by GitHub
parent b7d6a6c139
commit 8ddbc77baf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 266 additions and 87 deletions

View File

@ -10,14 +10,11 @@ use ratatui::{
widgets::{Block, Borders, StatefulWidget, Widget}, widgets::{Block, Borders, StatefulWidget, Widget},
}; };
use crate::utils::center_horizontal;
#[derive(Debug, Copy, Clone, Display, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Display, PartialEq, Eq)]
pub enum Time { pub enum Time {
Seconds, Seconds,
Minutes, Minutes,
// TODO: Handle hours Hours,
// Hours,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -41,24 +38,47 @@ impl fmt::Display for Mode {
Mode::Editable(time, _) => match time { Mode::Editable(time, _) => match time {
Time::Seconds => write!(f, "[edit seconds]"), Time::Seconds => write!(f, "[edit seconds]"),
Time::Minutes => write!(f, "[edit minutes]"), Time::Minutes => write!(f, "[edit minutes]"),
Time::Hours => write!(f, "[edit hours]"),
}, },
Mode::Done => write!(f, "done"), Mode::Done => write!(f, "done"),
} }
} }
} }
// unstable
// https://doc.rust-lang.org/src/core/time.rs.html#32
const SECS_PER_MINUTE: u64 = 60;
// unstable
// https://doc.rust-lang.org/src/core/time.rs.html#34
const MINS_PER_HOUR: u64 = 60;
// unstable
// https://doc.rust-lang.org/src/core/time.rs.html#36
const HOURS_PER_DAY: u64 = 24;
// max. 99:59:59
const MAX_EDITABLE_DURATION: Duration =
Duration::from_secs(100 * MINS_PER_HOUR * SECS_PER_MINUTE - 1);
const ONE_SECOND: Duration = Duration::from_secs(1);
const ONE_MINUTE: Duration = Duration::from_secs(SECS_PER_MINUTE);
const ONE_HOUR: Duration = Duration::from_secs(MINS_PER_HOUR * SECS_PER_MINUTE);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Display)]
pub enum Format {
MmSs,
HhMmSs,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Clock<T> { pub struct Clock<T> {
initial_value: Duration, initial_value: Duration,
tick_value: Duration, tick_value: Duration,
current_value: Duration, current_value: Duration,
mode: Mode, mode: Mode,
format: Format,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
// TODO: Change it to 23:59:59 after supporting `hours`
const MAX_EDITABLE_DURATION: Duration = Duration::from_secs(60 * 60); // 1 hour
impl<T> Clock<T> { impl<T> Clock<T> {
pub fn toggle_pause(&mut self) { pub fn toggle_pause(&mut self) {
self.mode = if self.mode == Mode::Tick { self.mode = if self.mode == Mode::Tick {
@ -90,16 +110,15 @@ impl<T> Clock<T> {
}; };
} }
pub fn edit_up(&mut self) { pub fn edit_current_up(&mut self) {
self.current_value = match self.mode { self.current_value = match self.mode {
Mode::Editable(Time::Seconds, _) => { Mode::Editable(Time::Seconds, _) => {
if self if self
.current_value .current_value
// TODO: Change it to 24:59:59 after supporting `hours` // < 99:59:58
// At the meantime: < 59:59 .lt(&MAX_EDITABLE_DURATION.saturating_sub(ONE_SECOND))
.lt(&MAX_EDITABLE_DURATION.saturating_sub(Duration::from_secs(1)))
{ {
self.current_value.saturating_add(Duration::from_secs(1)) self.current_value.saturating_add(ONE_SECOND)
} else { } else {
self.current_value self.current_value
} }
@ -107,11 +126,21 @@ impl<T> Clock<T> {
Mode::Editable(Time::Minutes, _) => { Mode::Editable(Time::Minutes, _) => {
if self if self
.current_value .current_value
// TODO: Change it to 24:59:00 after supporting `hours` // < 99:58:59
// At the meantime: < 59:00 .lt(&MAX_EDITABLE_DURATION.saturating_sub(ONE_MINUTE))
.lt(&MAX_EDITABLE_DURATION.saturating_sub(Duration::from_secs(60)))
{ {
self.current_value.saturating_add(Duration::new(60, 0)) self.current_value.saturating_add(ONE_MINUTE)
} else {
self.current_value
}
}
Mode::Editable(Time::Hours, _) => {
if self
.current_value
// < 98:59:59
.lt(&MAX_EDITABLE_DURATION.saturating_sub(ONE_HOUR))
{
self.current_value.saturating_add(ONE_HOUR)
} else { } else {
self.current_value self.current_value
} }
@ -119,20 +148,18 @@ impl<T> Clock<T> {
_ => self.current_value, _ => self.current_value,
}; };
} }
pub fn edit_down(&mut self) { pub fn edit_current_down(&mut self) {
self.current_value = match self.mode { self.current_value = match self.mode {
Mode::Editable(Time::Seconds, _) => { Mode::Editable(Time::Seconds, _) => self.current_value.saturating_sub(ONE_SECOND),
self.current_value.saturating_sub(Duration::new(1, 0)) Mode::Editable(Time::Minutes, _) => self.current_value.saturating_sub(ONE_MINUTE),
} Mode::Editable(Time::Hours, _) => self.current_value.saturating_sub(ONE_HOUR),
Mode::Editable(Time::Minutes, _) => {
self.current_value.saturating_sub(Duration::new(60, 0))
}
_ => self.current_value, _ => self.current_value,
}; };
self.update_mode();
} }
pub fn get_mode(&mut self) -> Mode { pub fn get_mode(&mut self) -> &Mode {
self.mode.clone() &self.mode
} }
pub fn is_edit_mode(&mut self) -> bool { pub fn is_edit_mode(&mut self) -> bool {
@ -146,18 +173,46 @@ impl<T> Clock<T> {
} }
} }
pub fn edit_next(&mut self) { fn edit_mode_next(&mut self) {
self.mode = match self.mode.clone() { let mode = self.mode.clone();
Mode::Editable(Time::Seconds, prev) => Mode::Editable(Time::Minutes, prev), self.mode = match mode {
Mode::Editable(Time::Minutes, prev) => Mode::Editable(Time::Seconds, prev), Mode::Editable(Time::Seconds, prev) if self.format == Format::MmSs => {
_ => self.mode.clone(), Mode::Editable(Time::Minutes, prev)
}
Mode::Editable(Time::Minutes, prev) if self.format == Format::MmSs => {
Mode::Editable(Time::Seconds, prev)
}
Mode::Editable(Time::Minutes, prev) if self.format == Format::HhMmSs => {
Mode::Editable(Time::Hours, prev)
}
Mode::Editable(Time::Hours, prev) => Mode::Editable(Time::Seconds, prev),
_ => mode,
} }
} }
pub fn edit_prev(&mut self) { fn edit_mode_prev(&mut self) {
// as same as editing `next` value let mode = self.mode.clone();
// TODO: Update it as soon as `hours` come into play self.mode = match mode {
self.edit_next() Mode::Editable(Time::Seconds, prev) if self.format == Format::HhMmSs => {
Mode::Editable(Time::Hours, prev)
}
Mode::Editable(Time::Seconds, prev) if self.format == Format::MmSs => {
Mode::Editable(Time::Minutes, prev)
}
Mode::Editable(Time::Minutes, prev) => Mode::Editable(Time::Seconds, prev),
Mode::Editable(Time::Hours, prev) => Mode::Editable(Time::Minutes, prev),
_ => mode,
}
}
fn update_mode(&mut self) {
let mode = self.mode.clone();
self.mode = match mode {
Mode::Editable(Time::Hours, prev) if self.format == Format::HhMmSs => {
Mode::Editable(Time::Minutes, prev)
}
_ => mode,
}
} }
pub fn reset(&mut self) { pub fn reset(&mut self) {
@ -165,24 +220,32 @@ impl<T> Clock<T> {
self.current_value = self.initial_value; self.current_value = self.initial_value;
} }
fn duration(&self) -> Duration { fn current_hours(&self) -> u64 {
self.current_value self.current_seconds() / (SECS_PER_MINUTE * MINS_PER_HOUR)
} }
fn hours(&self) -> u64 { fn current_hours_mod(&self) -> u64 {
(self.duration().as_secs() / 60 / 60) % 60 self.current_hours() % HOURS_PER_DAY
} }
fn minutes(&self) -> u64 { fn current_minutes(&self) -> u64 {
(self.duration().as_secs() / 60) % 60 self.current_seconds() / MINS_PER_HOUR
} }
fn seconds(&self) -> u64 { fn current_minutes_mod(&self) -> u64 {
self.duration().as_secs() % 60 self.current_minutes() % SECS_PER_MINUTE
} }
fn tenths(&self) -> u32 { fn current_seconds(&self) -> u64 {
self.duration().subsec_millis() / 100 self.current_value.as_secs()
}
fn current_seconds_mod(&self) -> u64 {
self.current_seconds() % SECS_PER_MINUTE
}
fn current_tenths(&self) -> u32 {
self.current_value.subsec_millis() / 100
} }
pub fn is_done(&mut self) -> bool { pub fn is_done(&mut self) -> bool {
@ -195,10 +258,10 @@ impl<T> fmt::Display for Clock<T> {
write!( write!(
f, f,
"{:02}:{:02}:{:02}.{}", "{:02}:{:02}:{:02}.{}",
self.hours(), self.current_hours_mod(),
self.minutes(), self.current_minutes_mod(),
self.seconds(), self.current_seconds_mod(),
self.tenths() self.current_tenths()
) )
} }
} }
@ -206,9 +269,6 @@ impl<T> fmt::Display for Clock<T> {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Countdown {} pub struct Countdown {}
#[derive(Debug, Clone)]
pub struct Timer {}
impl Clock<Countdown> { impl Clock<Countdown> {
pub fn new(initial_value: Duration, tick_value: Duration) -> Self { pub fn new(initial_value: Duration, tick_value: Duration) -> Self {
Self { Self {
@ -216,6 +276,7 @@ impl Clock<Countdown> {
tick_value, tick_value,
current_value: initial_value, current_value: initial_value,
mode: Mode::Initial, mode: Mode::Initial,
format: Format::MmSs,
phantom: PhantomData, phantom: PhantomData,
} }
} }
@ -224,6 +285,7 @@ impl Clock<Countdown> {
if self.mode == Mode::Tick { if self.mode == Mode::Tick {
self.current_value = self.current_value.saturating_sub(self.tick_value); self.current_value = self.current_value.saturating_sub(self.tick_value);
self.check_done(); self.check_done();
self.update_format();
} }
} }
@ -232,7 +294,52 @@ impl Clock<Countdown> {
self.mode = Mode::Done; self.mode = Mode::Done;
} }
} }
pub fn edit_next(&mut self) {
self.edit_mode_next();
self.update_format();
} }
pub fn edit_prev(&mut self) {
self.edit_mode_prev();
self.update_format();
}
pub fn edit_up(&mut self) {
self.edit_current_up();
self.update_format();
// update `initial_value` if needed
if self.initial_value.lt(&self.current_value) {
self.initial_value = self.current_value;
}
}
pub fn edit_down(&mut self) {
self.edit_current_down();
self.update_format();
// update `initial_value` if needed
if self.initial_value.gt(&self.current_value) {
self.initial_value = self.current_value;
}
}
fn update_format(&mut self) {
self.format = self.get_format();
}
pub fn get_format(&self) -> Format {
// show hours if initial available only
if self.current_value.ge(&ONE_HOUR) {
Format::HhMmSs
} else {
Format::MmSs
}
}
}
#[derive(Debug, Clone)]
pub struct Timer {}
impl Clock<Timer> { impl Clock<Timer> {
pub fn new(initial_value: Duration, tick_value: Duration) -> Self { pub fn new(initial_value: Duration, tick_value: Duration) -> Self {
Self { Self {
@ -240,6 +347,7 @@ impl Clock<Timer> {
tick_value, tick_value,
current_value: Duration::ZERO, current_value: Duration::ZERO,
mode: Mode::Initial, mode: Mode::Initial,
format: Format::MmSs,
phantom: PhantomData, phantom: PhantomData,
} }
} }
@ -248,6 +356,7 @@ impl Clock<Timer> {
if self.mode == Mode::Tick { if self.mode == Mode::Tick {
self.current_value = self.current_value.saturating_add(self.tick_value); self.current_value = self.current_value.saturating_add(self.tick_value);
self.check_done(); self.check_done();
self.update_format();
} }
} }
@ -256,6 +365,18 @@ impl Clock<Timer> {
self.mode = Mode::Done; self.mode = Mode::Done;
} }
} }
fn update_format(&mut self) {
self.format = self.get_format();
}
pub fn get_format(&self) -> Format {
if self.current_value.ge(&ONE_HOUR) {
Format::HhMmSs
} else {
Format::MmSs
}
}
} }
const DIGIT_SYMBOL: &str = ""; const DIGIT_SYMBOL: &str = "";
@ -379,12 +500,15 @@ where
} }
} }
fn get_horizontal_lengths(&self) -> [u16; 3] { fn get_horizontal_lengths(&self, format: &Format) -> Vec<u16> {
[11, 4, 11] match format {
Format::MmSs => vec![11, 4, 11],
Format::HhMmSs => vec![11, 4, 11, 4, 11],
}
} }
pub fn get_width(&self) -> u16 { pub fn get_width(&self, format: &Format) -> u16 {
self.get_horizontal_lengths().iter().sum() self.get_horizontal_lengths(format).iter().sum()
} }
pub fn get_digit_height(&self) -> u16 { pub fn get_digit_height(&self) -> u16 {
@ -467,30 +591,47 @@ where
} }
} }
fn render_edit_border(mode: Mode, width: u16, area: Rect, buf: &mut Buffer) { fn render_edit_border(mode: &Mode, format: &Format, width: u16, area: Rect, buf: &mut Buffer) {
match mode { let border_area = match mode {
Mode::Editable(Time::Seconds, _) => { Mode::Editable(Time::Seconds, _) => {
let [_, h2] = Layout::horizontal([Constraint::Fill(0), Constraint::Length(width)]) let [_, h] =
Layout::horizontal([Constraint::Percentage(100), Constraint::Length(width)])
.areas(area); .areas(area);
Block::new() h
.borders(Borders::TOP)
.border_set(symbols::border::THICK)
.render(h2, buf);
} }
Mode::Editable(Time::Minutes, _) => { Mode::Editable(Time::Minutes, _) if format == &Format::MmSs => {
let [h1, _] = Layout::horizontal([Constraint::Length(width), Constraint::Fill(0)]) let [h, _] =
Layout::horizontal([Constraint::Length(width), Constraint::Percentage(100)])
.areas(area); .areas(area);
h
}
Mode::Editable(Time::Minutes, _) if format == &Format::HhMmSs => {
let [_, h, _] = Layout::horizontal([
Constraint::Fill(0),
Constraint::Length(width),
Constraint::Fill(0),
])
.areas(area);
h
}
Mode::Editable(Time::Hours, _) => {
let [h, _] = Layout::horizontal([Constraint::Length(width), Constraint::Fill(0)])
.areas(area);
h
}
_ => area,
};
let border_type = match mode {
Mode::Editable(_, _) => symbols::border::THICK,
_ => symbols::border::EMPTY,
};
Block::new() Block::new()
.borders(Borders::TOP) .borders(Borders::TOP)
.border_set(symbols::border::THICK) .border_set(border_type)
.render(h1, buf) .render(border_area, buf);
}
_ => Block::new()
.borders(Borders::TOP)
.border_set(symbols::border::EMPTY)
.render(area, buf),
}
} }
} }
@ -501,22 +642,51 @@ where
type State = Clock<T>; type State = Clock<T>;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
// center
let h = center_horizontal(area, Constraint::Length(self.get_width()));
let [v1, v2] = Layout::vertical(Constraint::from_lengths([ let [v1, v2] = Layout::vertical(Constraint::from_lengths([
self.get_digit_height(), self.get_digit_height(),
EDIT_BORDER_HEIGHT as u16, EDIT_BORDER_HEIGHT as u16,
])) ]))
.areas(h); .areas(area);
let [h1, h2, h3] = match &state.format {
Layout::horizontal(Constraint::from_lengths(self.get_horizontal_lengths())).areas(v1); Format::MmSs => {
let [h1, h2, h3] = Layout::horizontal(Constraint::from_lengths(
self.get_horizontal_lengths(&state.format),
))
.areas(v1);
let size_digits = Self::render_digit_pair(state.minutes(), h1, buf); let size_digits = Self::render_digit_pair(state.current_minutes_mod(), h1, buf);
Self::render_colon(h2, buf); Self::render_colon(h2, buf);
Self::render_digit_pair(state.seconds(), h3, buf); Self::render_digit_pair(state.current_seconds_mod(), h3, buf);
Self::render_edit_border(state.mode.clone(), size_digits.width - 1, v2, buf); Self::render_edit_border(
&state.mode,
&state.format,
size_digits.width - 1,
v2,
buf,
);
}
Format::HhMmSs => {
let [h1, h2, h3, h4, h5] = Layout::horizontal(Constraint::from_lengths(
self.get_horizontal_lengths(&state.format),
))
.areas(v1);
let size_digits = Self::render_digit_pair(state.current_hours_mod(), h1, buf);
Self::render_colon(h2, buf);
Self::render_digit_pair(state.current_minutes_mod(), h3, buf);
Self::render_colon(h4, buf);
Self::render_digit_pair(state.current_seconds_mod(), h5, buf);
Self::render_edit_border(
&state.mode,
&state.format,
size_digits.width - 1,
v2,
buf,
);
}
}
} }
} }

View File

@ -75,7 +75,10 @@ impl StatefulWidget for CountdownWidget {
let area = center( let area = center(
area, area,
Constraint::Length(max(clock.get_width(), label.width() as u16)), Constraint::Length(max(
clock.get_width(&state.clock.get_format()),
label.width() as u16,
)),
Constraint::Length(clock.get_height() + 1 /* height of label */), Constraint::Length(clock.get_height() + 1 /* height of label */),
); );
let [v1, v2] = let [v1, v2] =

View File

@ -131,7 +131,10 @@ impl StatefulWidget for PomodoroWidget {
let area = center( let area = center(
area, area,
Constraint::Length(max(clock.get_width(), label.width() as u16)), Constraint::Length(max(
clock.get_width(&state.get_clock().get_format()),
label.width() as u16,
)),
Constraint::Length(clock.get_height() + 1 /* height of mode_str */), Constraint::Length(clock.get_height() + 1 /* height of mode_str */),
); );

View File

@ -52,7 +52,10 @@ impl StatefulWidget for &TimerWidget {
let area = center( let area = center(
area, area,
Constraint::Length(max(clock.get_width(), label.width() as u16)), Constraint::Length(max(
clock.get_width(&state.clock.get_format()),
label.width() as u16,
)),
Constraint::Length(clock.get_height() + 1 /* height of label */), Constraint::Length(clock.get_height() + 1 /* height of label */),
); );
let [v1, v2] = let [v1, v2] =