Editable Countdown (#12)

- Editable `Countdown`
- ASCII Symbols for `Clock` states
- Update labels
This commit is contained in:
Jens K. 2024-12-12 14:16:05 +01:00 committed by GitHub
parent 4c38ac368e
commit 64300631c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 46 deletions

View File

@ -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::<clock::Countdown>::new(
Duration::from_secs(10 * 60 /* 10min */),
Duration::from_millis(TICK_VALUE_MS),
),
),
timer: Timer::new(
"Timer".into(),
Clock::<clock::Timer>::new(Duration::ZERO, Duration::from_millis(TICK_VALUE_MS)),
),
countdown: Countdown::new(Clock::<clock::Countdown>::new(
Duration::from_secs(10 * 60 /* 10min */),
Duration::from_millis(TICK_VALUE_MS),
)),
timer: Timer::new(Clock::<clock::Timer>::new(
Duration::ZERO,
Duration::from_millis(TICK_VALUE_MS),
)),
pomodoro: Pomodoro::new(),
}
}

View File

@ -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<T> {
initial_value: Duration,

View File

@ -15,13 +15,12 @@ use crate::{
#[derive(Debug, Clone)]
pub struct Countdown {
headline: String,
clock: Clock<clock::Countdown>,
}
impl Countdown {
pub const fn new(headline: String, clock: Clock<clock::Countdown>) -> Self {
Self { headline, clock }
pub const fn new(clock: Clock<clock::Countdown>) -> 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);
}
}

View File

@ -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);
}
}

View File

@ -14,13 +14,12 @@ use std::cmp::max;
#[derive(Debug, Clone)]
pub struct Timer {
headline: String,
clock: Clock<clock::Timer>,
}
impl Timer {
pub const fn new(headline: String, clock: Clock<clock::Timer>) -> Self {
Self { headline, clock }
pub const fn new(clock: Clock<clock::Timer>) -> 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);
}
}