feat: handle Q for quitting

This commit is contained in:
Pihkaal
2025-12-02 14:10:14 +01:00
parent a731360ea4
commit 312b2f76e2
4 changed files with 24 additions and 21 deletions

View File

@@ -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(())

View File

@@ -66,7 +66,7 @@ impl Chronometer {
}
}
pub fn main_loop(config: &mut Config) -> io::Result<()> {
pub fn main_loop(config: &mut Config) -> io::Result<String> {
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<Lapse> = 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(

View File

@@ -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<String> {
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<()> {

View File

@@ -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<String> {
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<()> {