From 312b2f76e2791efff290c05a1d44e7e982aa6e16 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Tue, 2 Dec 2025 14:10:14 +0100 Subject: [PATCH] feat: handle Q for quitting --- src/main.rs | 6 +++--- src/modes/chrono.rs | 13 +++++++------ src/modes/clock.rs | 13 +++++++------ src/modes/timer.rs | 13 +++++++------ 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7e6931f..42550ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,7 @@ fn main() -> io::Result<()> { let _ = terminal::enable_raw_mode()?; // Start the appropriate mode - match &cli.command { + let quit_reason = match &cli.command { Some(Commands::Chrono {}) => modes::chrono::main_loop(&mut config)?, Some(Commands::Timer { duration }) => { let duration = duration.join(" "); @@ -128,7 +128,7 @@ fn main() -> io::Result<()> { } Some(Commands::Debug {}) => unreachable!(), None => modes::clock::main_loop(&mut config)?, - } + }; // Disale raw mode, leave the alternate screen and show the cursor back let _ = terminal::disable_raw_mode()?; @@ -136,7 +136,7 @@ fn main() -> io::Result<()> { // Be polite if config.be_polite { - println!("CTRL-C pressed, bye!\n"); + println!("{}, bye!\n", quit_reason); } Ok(()) diff --git a/src/modes/chrono.rs b/src/modes/chrono.rs index 31acb41..5a91be4 100644 --- a/src/modes/chrono.rs +++ b/src/modes/chrono.rs @@ -66,7 +66,7 @@ impl Chronometer { } } -pub fn main_loop(config: &mut Config) -> io::Result<()> { +pub fn main_loop(config: &mut Config) -> io::Result { let mut stdout = io::stdout(); let mut chronometer = Chronometer::new(); @@ -75,18 +75,19 @@ pub fn main_loop(config: &mut Config) -> io::Result<()> { let mut lapses: Vec = vec![]; let mut scroll_offset: usize = 0; - let mut quit = false; - while !quit { + let mut quit_reason = None; + while quit_reason.is_none() { // Handle events while event::poll(Duration::ZERO)? { match event::read()? { Event::Key(e) => match e.code { - // Handle CTRL-C + // Handle quit via CTRL-C or Q KeyCode::Char('c') => { if e.modifiers.contains(KeyModifiers::CONTROL) { - quit = true; + quit_reason = Some("CTRL-C pressed"); } } + KeyCode::Char('q') => quit_reason = Some("Q pressed"), // Handle pause KeyCode::Char(' ') => { chronometer.toggle_pause(); @@ -146,7 +147,7 @@ pub fn main_loop(config: &mut Config) -> io::Result<()> { thread::sleep(Duration::from_millis(1000 / config.fps)); } - Ok(()) + Ok(quit_reason.unwrap().to_string()) } fn render_frame( diff --git a/src/modes/clock.rs b/src/modes/clock.rs index 999ca32..fd827a7 100644 --- a/src/modes/clock.rs +++ b/src/modes/clock.rs @@ -16,21 +16,22 @@ use crate::{ rendering::{self, symbols}, }; -pub fn main_loop(config: &mut Config) -> io::Result<()> { +pub fn main_loop(config: &mut Config) -> io::Result { let mut stdout = io::stdout(); - let mut quit = false; - while !quit { + let mut quit_reason = None; + while quit_reason.is_none() { // Handle events while event::poll(Duration::ZERO)? { match event::read()? { Event::Key(e) => match e.code { - // Handle CTRL-C + // Handle quit via CTRL-C or Q KeyCode::Char('c') => { if e.modifiers.contains(KeyModifiers::CONTROL) { - quit = true; + quit_reason = Some("CTRL-C pressed"); } } + KeyCode::Char('q') => quit_reason = Some("Q pressed"), _ => {} }, _ => {} @@ -50,7 +51,7 @@ pub fn main_loop(config: &mut Config) -> io::Result<()> { thread::sleep(Duration::from_millis(1000 / config.fps)); } - Ok(()) + Ok(quit_reason.unwrap().to_string()) } fn render_frame(config: &Config) -> io::Result<()> { diff --git a/src/modes/timer.rs b/src/modes/timer.rs index 9c3ae74..0c30f13 100644 --- a/src/modes/timer.rs +++ b/src/modes/timer.rs @@ -71,25 +71,26 @@ impl Timer { } } -pub fn main_loop(config: &mut Config, duration: &str) -> io::Result<()> { +pub fn main_loop(config: &mut Config, duration: &str) -> io::Result { let mut stdout = io::stdout(); let duration = parse_duration::parse(duration) .unwrap_or_else(|_| eprintln_quit!("Invalid duration provided")); let mut timer = Timer::new(duration); - let mut quit = false; - while !quit { + let mut quit_reason = None; + while quit_reason.is_none() { // Handle events while event::poll(Duration::ZERO)? { match event::read()? { Event::Key(e) => match e.code { - // Handle CTRL-C + // Handle quit via CTRL-C or Q KeyCode::Char('c') => { if e.modifiers.contains(KeyModifiers::CONTROL) { - quit = true; + quit_reason = Some("CTRL-C pressed"); } } + KeyCode::Char('q') => quit_reason = Some("Q pressed"), // Handle pause KeyCode::Char(' ') => { timer.toggle_pause(); @@ -117,7 +118,7 @@ pub fn main_loop(config: &mut Config, duration: &str) -> io::Result<()> { thread::sleep(Duration::from_millis(1000 / config.fps)); } - Ok(()) + Ok(quit_reason.unwrap().to_string()) } fn render_frame(config: &Config, timer: &Timer) -> io::Result<()> {