diff --git a/src/app.rs b/src/app.rs index 57a606d..d09c08c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -48,19 +48,16 @@ impl Default for App { fn default() -> Self { Self { mode: Mode::Running, - content: Content::Pomodoro, + content: Content::Countdown, show_menu: false, - countdown: Countdown::new( - "Countdown".into(), - Clock::::new( - Duration::from_secs(10 * 60 /* 10min */), - Duration::from_millis(TICK_VALUE_MS), - ), - ), - timer: Timer::new( - "Timer".into(), - Clock::::new(Duration::ZERO, Duration::from_millis(TICK_VALUE_MS)), - ), + countdown: Countdown::new(Clock::::new( + Duration::from_secs(10 * 60 /* 10min */), + Duration::from_millis(TICK_VALUE_MS), + )), + timer: Timer::new(Clock::::new( + Duration::ZERO, + Duration::from_millis(TICK_VALUE_MS), + )), pomodoro: Pomodoro::new(), } } diff --git a/src/widgets/clock.rs b/src/widgets/clock.rs index f540baf..8587ef3 100644 --- a/src/widgets/clock.rs +++ b/src/widgets/clock.rs @@ -20,7 +20,7 @@ pub enum Time { // Hours, } -#[derive(Debug, Clone, Display, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Mode { Initial, Tick, @@ -32,6 +32,21 @@ pub enum Mode { Done, } +impl fmt::Display for Mode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Mode::Initial => write!(f, "[]"), + Mode::Tick => write!(f, ">"), + Mode::Pause => write!(f, "||"), + Mode::Editable(time, _) => match time { + Time::Seconds => write!(f, "[edit seconds]"), + Time::Minutes => write!(f, "[edit minutes]"), + }, + Mode::Done => write!(f, "done"), + } + } +} + #[derive(Debug, Clone)] pub struct Clock { initial_value: Duration, diff --git a/src/widgets/countdown.rs b/src/widgets/countdown.rs index 6f821e7..0359443 100644 --- a/src/widgets/countdown.rs +++ b/src/widgets/countdown.rs @@ -15,13 +15,12 @@ use crate::{ #[derive(Debug, Clone)] pub struct Countdown { - headline: String, clock: Clock, } impl Countdown { - pub const fn new(headline: String, clock: Clock) -> Self { - Self { headline, clock } + pub const fn new(clock: Clock) -> Self { + Self { clock } } pub fn is_edit_mode(&mut self) -> bool { @@ -35,12 +34,33 @@ impl EventHandler for Countdown { Event::Tick => { self.clock.tick(); } - Event::Key(key) if key.code == KeyCode::Char('s') => { - self.clock.toggle_pause(); - } Event::Key(key) if key.code == KeyCode::Char('r') => { self.clock.reset(); } + Event::Key(key) => match key.code { + KeyCode::Char('r') => { + self.clock.reset(); + } + KeyCode::Char('s') => { + self.clock.toggle_pause(); + } + KeyCode::Char('e') => { + self.clock.toggle_edit(); + } + KeyCode::Left => { + self.clock.edit_next(); + } + KeyCode::Right => { + self.clock.edit_prev(); + } + KeyCode::Up => { + self.clock.edit_up(); + } + KeyCode::Down => { + self.clock.edit_down(); + } + _ => {} + }, _ => {} } } @@ -52,17 +72,18 @@ impl StatefulWidget for CountdownWidget { type State = Countdown; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { let clock = ClockWidget::new(); - let headline = Line::raw(state.headline.to_uppercase()); + let headline = "Countdown".to_uppercase(); + let label = Line::raw((format!("{} {}", headline, state.clock.get_mode())).to_uppercase()); let area = center( area, - Constraint::Length(max(clock.get_width(), headline.width() as u16)), - Constraint::Length(clock.get_height() + 1 /* height of headline */), + Constraint::Length(max(clock.get_width(), label.width() as u16)), + Constraint::Length(clock.get_height() + 1 /* height of label */), ); let [v1, v2] = Layout::vertical(Constraint::from_lengths([clock.get_height(), 1])).areas(area); clock.render(v1, buf, &mut state.clock); - headline.render(v2, buf); + label.centered().render(v2, buf); } } diff --git a/src/widgets/pomodoro.rs b/src/widgets/pomodoro.rs index 6cdb56b..d6c6d9c 100644 --- a/src/widgets/pomodoro.rs +++ b/src/widgets/pomodoro.rs @@ -11,8 +11,8 @@ use ratatui::{ text::Line, widgets::{StatefulWidget, Widget}, }; -use std::cmp::max; -use std::time::Duration; +use std::{cmp::max, time::Duration}; + use strum::Display; static PAUSE_MS: u64 = 5 * 60 * 1000; /* 5min in milliseconds */ @@ -107,14 +107,10 @@ impl EventHandler for Pomodoro { } } KeyCode::Up => { - if self.get_clock().is_edit_mode() { - self.get_clock().edit_up(); - } + self.get_clock().edit_up(); } KeyCode::Down => { - if self.get_clock().is_edit_mode() { - self.get_clock().edit_down(); - } + self.get_clock().edit_down(); } KeyCode::Char('r') => { self.get_clock().reset(); @@ -133,18 +129,18 @@ impl StatefulWidget for PomodoroWidget { type State = Pomodoro; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { let clock = ClockWidget::new(); - let mode_str = Line::raw( - (if let Some(edit_mode) = state.get_clock().edit_mode() { - format!("{} > edit {}", state.mode, edit_mode) - } else { - format!("{} > {}", state.mode.clone(), state.get_clock().get_mode()) - }) + let label = Line::raw( + (format!( + "Pomodoro {} {}", + state.mode.clone(), + state.get_clock().get_mode() + )) .to_uppercase(), ); let area = center( area, - Constraint::Length(max(clock.get_width(), mode_str.width() as u16)), + Constraint::Length(max(clock.get_width(), label.width() as u16)), Constraint::Length(clock.get_height() + 1 /* height of mode_str */), ); @@ -152,6 +148,6 @@ impl StatefulWidget for PomodoroWidget { Layout::vertical(Constraint::from_lengths([clock.get_height(), 1])).areas(area); clock.render(v1, buf, state.get_clock()); - mode_str.render(v2, buf); + label.centered().render(v2, buf); } } diff --git a/src/widgets/timer.rs b/src/widgets/timer.rs index 773b6fd..7bd1a1c 100644 --- a/src/widgets/timer.rs +++ b/src/widgets/timer.rs @@ -14,13 +14,12 @@ use std::cmp::max; #[derive(Debug, Clone)] pub struct Timer { - headline: String, clock: Clock, } impl Timer { - pub const fn new(headline: String, clock: Clock) -> Self { - Self { headline, clock } + pub const fn new(clock: Clock) -> Self { + Self { clock } } } @@ -47,17 +46,18 @@ impl StatefulWidget for &TimerWidget { type State = Timer; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { let clock = ClockWidget::new(); - let headline = Line::raw(state.headline.to_uppercase()); + let headline = "Timer".to_uppercase(); + let label = Line::raw((format!("{} {}", headline, state.clock.get_mode())).to_uppercase()); let area = center( area, - Constraint::Length(max(clock.get_width(), headline.width() as u16)), - Constraint::Length(clock.get_height() + 1 /* height of headline */), + Constraint::Length(max(clock.get_width(), label.width() as u16)), + Constraint::Length(clock.get_height() + 1 /* height of label */), ); let [v1, v2] = Layout::vertical(Constraint::from_lengths([clock.get_height(), 1])).areas(area); clock.render(v1, buf, &mut state.clock); - headline.render(v2, buf); + label.centered().render(v2, buf); } }