refactor(timer): rename coutdown -> timer
This commit is contained in:
10
src/main.rs
10
src/main.rs
@@ -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)?,
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
Reference in New Issue
Block a user