refactor(timer): rename coutdown -> timer

This commit is contained in:
Pihkaal
2024-01-21 22:03:24 +01:00
parent 35e847841c
commit eff3db47dc
3 changed files with 19 additions and 19 deletions

View File

@@ -37,9 +37,9 @@ struct Cli {
enum Commands { enum Commands {
Debug {}, Debug {},
Chrono {}, Chrono {},
Countdown { Timer {
#[arg(required = true)] #[arg(required = true)]
start: Vec<String>, duration: Vec<String>,
}, },
} }
@@ -104,9 +104,9 @@ fn main() -> io::Result<()> {
match &cli.command { match &cli.command {
Some(Commands::Chrono {}) => modes::chrono::main_loop(&mut config)?, Some(Commands::Chrono {}) => modes::chrono::main_loop(&mut config)?,
Some(Commands::Countdown { start }) => { Some(Commands::Timer { duration }) => {
let start = start.join(" "); let duration = duration.join(" ");
modes::countdown::main_loop(&mut config, &start)? modes::timer::main_loop(&mut config, &duration)?
} }
Some(Commands::Debug {}) => unreachable!(), Some(Commands::Debug {}) => unreachable!(),
None => modes::clock::main_loop(&mut config)?, None => modes::clock::main_loop(&mut config)?,

View File

@@ -1,4 +1,4 @@
pub mod chrono; pub mod chrono;
pub mod clock; pub mod clock;
pub mod countdown;
pub mod debug; pub mod debug;
pub mod timer;

View File

@@ -16,16 +16,16 @@ use crate::{
rendering::{self, symbols}, rendering::{self, symbols},
}; };
struct Countdown { struct Timer {
duration: Duration, duration: Duration,
end_time: Option<Instant>, end_time: Option<Instant>,
paused_duration: Duration, paused_duration: Duration,
} }
impl Countdown { impl Timer {
fn new(duration: Duration) -> Self { fn new(duration: Duration) -> Self {
let end_time = Some(Instant::now() + duration + Duration::from_secs(1)); let end_time = Some(Instant::now() + duration + Duration::from_secs(1));
Countdown { Timer {
duration, duration,
end_time, end_time,
paused_duration: Duration::ZERO, paused_duration: Duration::ZERO,
@@ -71,11 +71,11 @@ impl Countdown {
} }
} }
pub fn main_loop(config: &mut Config, start: &str) -> io::Result<()> { pub fn main_loop(config: &mut Config, duration: &str) -> io::Result<()> {
let mut stdout = io::stdout(); let mut stdout = io::stdout();
let duration = parse_duration::parse(start).unwrap(); let duration = parse_duration::parse(duration).unwrap();
let mut countdown = Countdown::new(duration); let mut timer = Timer::new(duration);
let mut quit = false; let mut quit = false;
while !quit { while !quit {
@@ -91,11 +91,11 @@ pub fn main_loop(config: &mut Config, start: &str) -> io::Result<()> {
} }
// Handle pause // Handle pause
KeyCode::Char(' ') => { KeyCode::Char(' ') => {
countdown.toggle_pause(); timer.toggle_pause();
} }
// Handle reset // Handle reset
KeyCode::Char('r') => { KeyCode::Char('r') => {
countdown.reset(); timer.reset();
} }
_ => {} _ => {}
}, },
@@ -107,7 +107,7 @@ pub fn main_loop(config: &mut Config, start: &str) -> io::Result<()> {
queue!(stdout, terminal::Clear(ClearType::All))?; queue!(stdout, terminal::Clear(ClearType::All))?;
// Render // Render
render_frame(&config, &countdown)?; render_frame(&config, &timer)?;
config.color.update(); config.color.update();
@@ -119,17 +119,17 @@ pub fn main_loop(config: &mut Config, start: &str) -> io::Result<()> {
return Ok(()); return Ok(());
} }
fn render_frame(config: &Config, countdown: &Countdown) -> io::Result<()> { fn render_frame(config: &Config, timer: &Timer) -> io::Result<()> {
let color = config.color.get_value(); let color = config.color.get_value();
// Display time // Display time
let remaining = utils::format_duration(countdown.time_left()); let remaining = utils::format_duration(timer.time_left());
rendering::draw_time(&remaining, color)?; rendering::draw_time(&remaining, color)?;
// Display pause state // Display pause state
let (width, height) = terminal::size()?; let (width, height) = terminal::size()?;
let y = height / 2 + symbols::SYMBOL_HEIGHT as u16 / 2 + 2; let y = height / 2 + symbols::SYMBOL_HEIGHT as u16 / 2 + 2;
if countdown.is_paused() { if timer.is_paused() {
let text = "[PAUSE]"; let text = "[PAUSE]";
let x = width / 2 - (text.len() as u16) / 2; let x = width / 2 - (text.len() as u16) / 2;
@@ -141,7 +141,7 @@ fn render_frame(config: &Config, countdown: &Countdown) -> io::Result<()> {
)?; )?;
} }
// Display finish state // Display finish state
else if countdown.is_finished() { else if timer.is_finished() {
let text = "[FINISHED]"; let text = "[FINISHED]";
let x = width / 2 - (text.len() as u16) / 2; let x = width / 2 - (text.len() as u16) / 2;