Persist app state (#21)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use clap::ValueEnum;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
@@ -9,7 +11,7 @@ use ratatui::{
|
||||
widgets::StatefulWidget,
|
||||
};
|
||||
|
||||
use crate::{args::ClockStyle, utils::center_horizontal};
|
||||
use crate::utils::center_horizontal;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Display, PartialEq, Eq)]
|
||||
pub enum Time {
|
||||
@@ -74,22 +76,47 @@ pub enum Format {
|
||||
HhMmSs,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, ValueEnum, Default, Serialize, Deserialize)]
|
||||
pub enum Style {
|
||||
#[default]
|
||||
#[value(name = "bold", alias = "b")]
|
||||
Bold,
|
||||
#[value(name = "empty", alias = "e")]
|
||||
Empty,
|
||||
#[value(name = "thick", alias = "t")]
|
||||
Thick,
|
||||
#[value(name = "cross", alias = "c")]
|
||||
Cross,
|
||||
}
|
||||
|
||||
impl Style {
|
||||
pub fn next(&self) -> Self {
|
||||
match self {
|
||||
Style::Bold => Style::Empty,
|
||||
Style::Empty => Style::Thick,
|
||||
Style::Thick => Style::Cross,
|
||||
Style::Cross => Style::Bold,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Clock<T> {
|
||||
initial_value: Duration,
|
||||
pub initial_value: Duration,
|
||||
pub current_value: Duration,
|
||||
tick_value: Duration,
|
||||
current_value: Duration,
|
||||
mode: Mode,
|
||||
format: Format,
|
||||
pub style: ClockStyle,
|
||||
pub style: Style,
|
||||
pub with_decis: bool,
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
pub struct ClockArgs {
|
||||
pub initial_value: Duration,
|
||||
pub current_value: Duration,
|
||||
pub tick_value: Duration,
|
||||
pub style: ClockStyle,
|
||||
pub style: Style,
|
||||
pub with_decis: bool,
|
||||
}
|
||||
|
||||
@@ -315,15 +342,22 @@ impl Clock<Countdown> {
|
||||
pub fn new(args: ClockArgs) -> Self {
|
||||
let ClockArgs {
|
||||
initial_value,
|
||||
current_value,
|
||||
tick_value,
|
||||
style,
|
||||
with_decis,
|
||||
} = args;
|
||||
let mut instance = Self {
|
||||
initial_value,
|
||||
current_value,
|
||||
tick_value,
|
||||
current_value: initial_value,
|
||||
mode: Mode::Initial,
|
||||
mode: if current_value == Duration::ZERO {
|
||||
Mode::Done
|
||||
} else if current_value == initial_value {
|
||||
Mode::Initial
|
||||
} else {
|
||||
Mode::Pause
|
||||
},
|
||||
format: Format::S,
|
||||
style,
|
||||
with_decis,
|
||||
@@ -380,15 +414,22 @@ impl Clock<Timer> {
|
||||
pub fn new(args: ClockArgs) -> Self {
|
||||
let ClockArgs {
|
||||
initial_value,
|
||||
current_value,
|
||||
tick_value,
|
||||
style,
|
||||
with_decis,
|
||||
} = args;
|
||||
let mut instance = Self {
|
||||
initial_value,
|
||||
current_value,
|
||||
tick_value,
|
||||
current_value: Duration::ZERO,
|
||||
mode: Mode::Initial,
|
||||
mode: if current_value == initial_value {
|
||||
Mode::Initial
|
||||
} else if current_value >= MAX_DURATION {
|
||||
Mode::Done
|
||||
} else {
|
||||
Mode::Pause
|
||||
},
|
||||
format: Format::S,
|
||||
phantom: PhantomData,
|
||||
style,
|
||||
@@ -552,12 +593,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn get_digit_symbol(&self, style: &ClockStyle) -> &str {
|
||||
fn get_digit_symbol(&self, style: &Style) -> &str {
|
||||
match &style {
|
||||
ClockStyle::Bold => "█",
|
||||
ClockStyle::Empty => "░",
|
||||
ClockStyle::Cross => "╬",
|
||||
ClockStyle::Thick => "┃",
|
||||
Style::Bold => "█",
|
||||
Style::Empty => "░",
|
||||
Style::Cross => "╬",
|
||||
Style::Thick => "┃",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,9 @@ use ratatui::{
|
||||
use std::cmp::max;
|
||||
|
||||
use crate::{
|
||||
args::ClockStyle,
|
||||
events::{Event, EventHandler},
|
||||
utils::center,
|
||||
widgets::clock::{self, Clock, ClockWidget},
|
||||
widgets::clock::{self, Clock, ClockWidget, Style},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -24,13 +23,17 @@ impl Countdown {
|
||||
Self { clock }
|
||||
}
|
||||
|
||||
pub fn set_style(&mut self, style: ClockStyle) {
|
||||
pub fn set_style(&mut self, style: Style) {
|
||||
self.clock.style = style;
|
||||
}
|
||||
|
||||
pub fn set_with_decis(&mut self, with_decis: bool) {
|
||||
self.clock.with_decis = with_decis;
|
||||
}
|
||||
|
||||
pub fn get_clock(&self) -> &Clock<clock::Countdown> {
|
||||
&self.clock
|
||||
}
|
||||
}
|
||||
|
||||
impl EventHandler for Countdown {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::args::Content;
|
||||
use crate::app::Content;
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::{
|
||||
args::ClockStyle,
|
||||
constants::TICK_VALUE_MS,
|
||||
events::{Event, EventHandler},
|
||||
utils::center,
|
||||
widgets::clock::{Clock, ClockWidget, Countdown},
|
||||
widgets::clock::{Clock, ClockWidget, Countdown, Style},
|
||||
};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
@@ -16,10 +15,12 @@ use std::{cmp::max, time::Duration};
|
||||
|
||||
use strum::Display;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::clock::ClockArgs;
|
||||
|
||||
#[derive(Debug, Clone, Display, Hash, Eq, PartialEq)]
|
||||
enum Mode {
|
||||
#[derive(Debug, Clone, Display, Hash, Eq, PartialEq, Deserialize, Serialize)]
|
||||
pub enum Mode {
|
||||
Work,
|
||||
Pause,
|
||||
}
|
||||
@@ -46,31 +47,39 @@ pub struct Pomodoro {
|
||||
}
|
||||
|
||||
pub struct PomodoroArgs {
|
||||
pub work: Duration,
|
||||
pub pause: Duration,
|
||||
pub style: ClockStyle,
|
||||
pub mode: Mode,
|
||||
pub initial_value_work: Duration,
|
||||
pub current_value_work: Duration,
|
||||
pub initial_value_pause: Duration,
|
||||
pub current_value_pause: Duration,
|
||||
pub style: Style,
|
||||
pub with_decis: bool,
|
||||
}
|
||||
|
||||
impl Pomodoro {
|
||||
pub fn new(args: PomodoroArgs) -> Self {
|
||||
let PomodoroArgs {
|
||||
work,
|
||||
pause,
|
||||
mode,
|
||||
initial_value_work,
|
||||
current_value_work,
|
||||
initial_value_pause,
|
||||
current_value_pause,
|
||||
style,
|
||||
with_decis,
|
||||
} = args;
|
||||
Self {
|
||||
mode: Mode::Work,
|
||||
mode,
|
||||
clock_map: ClockMap {
|
||||
work: Clock::<Countdown>::new(ClockArgs {
|
||||
initial_value: work,
|
||||
initial_value: initial_value_work,
|
||||
current_value: current_value_work,
|
||||
tick_value: Duration::from_millis(TICK_VALUE_MS),
|
||||
style,
|
||||
with_decis,
|
||||
}),
|
||||
pause: Clock::<Countdown>::new(ClockArgs {
|
||||
initial_value: pause,
|
||||
initial_value: initial_value_pause,
|
||||
current_value: current_value_pause,
|
||||
tick_value: Duration::from_millis(TICK_VALUE_MS),
|
||||
style,
|
||||
with_decis,
|
||||
@@ -79,11 +88,23 @@ impl Pomodoro {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_clock(&mut self) -> &mut Clock<Countdown> {
|
||||
pub fn get_clock(&mut self) -> &mut Clock<Countdown> {
|
||||
self.clock_map.get(&self.mode)
|
||||
}
|
||||
|
||||
pub fn set_style(&mut self, style: crate::args::ClockStyle) {
|
||||
pub fn get_clock_work(&self) -> &Clock<Countdown> {
|
||||
&self.clock_map.work
|
||||
}
|
||||
|
||||
pub fn get_clock_pause(&self) -> &Clock<Countdown> {
|
||||
&self.clock_map.pause
|
||||
}
|
||||
|
||||
pub fn get_mode(&self) -> &Mode {
|
||||
&self.mode
|
||||
}
|
||||
|
||||
pub fn set_style(&mut self, style: Style) {
|
||||
self.clock_map.work.style = style;
|
||||
self.clock_map.pause.style = style;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use crate::{
|
||||
args::ClockStyle,
|
||||
events::{Event, EventHandler},
|
||||
utils::center,
|
||||
widgets::clock::{self, Clock, ClockWidget},
|
||||
widgets::clock::{self, Clock, ClockWidget, Style},
|
||||
};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
@@ -23,13 +22,17 @@ impl Timer {
|
||||
Self { clock }
|
||||
}
|
||||
|
||||
pub fn set_style(&mut self, style: ClockStyle) {
|
||||
pub fn set_style(&mut self, style: Style) {
|
||||
self.clock.style = style;
|
||||
}
|
||||
|
||||
pub fn set_with_decis(&mut self, with_decis: bool) {
|
||||
self.clock.with_decis = with_decis;
|
||||
}
|
||||
|
||||
pub fn get_clock(&self) -> &Clock<clock::Timer> {
|
||||
&self.clock
|
||||
}
|
||||
}
|
||||
|
||||
impl EventHandler for Timer {
|
||||
|
||||
Reference in New Issue
Block a user