feat: handle Q for quitting
This commit is contained in:
@@ -120,7 +120,7 @@ fn main() -> io::Result<()> {
|
|||||||
let _ = terminal::enable_raw_mode()?;
|
let _ = terminal::enable_raw_mode()?;
|
||||||
|
|
||||||
// Start the appropriate 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::Chrono {}) => modes::chrono::main_loop(&mut config)?,
|
||||||
Some(Commands::Timer { duration }) => {
|
Some(Commands::Timer { duration }) => {
|
||||||
let duration = duration.join(" ");
|
let duration = duration.join(" ");
|
||||||
@@ -128,7 +128,7 @@ fn main() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
Some(Commands::Debug {}) => unreachable!(),
|
Some(Commands::Debug {}) => unreachable!(),
|
||||||
None => modes::clock::main_loop(&mut config)?,
|
None => modes::clock::main_loop(&mut config)?,
|
||||||
}
|
};
|
||||||
|
|
||||||
// Disale raw mode, leave the alternate screen and show the cursor back
|
// Disale raw mode, leave the alternate screen and show the cursor back
|
||||||
let _ = terminal::disable_raw_mode()?;
|
let _ = terminal::disable_raw_mode()?;
|
||||||
@@ -136,7 +136,7 @@ fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
// Be polite
|
// Be polite
|
||||||
if config.be_polite {
|
if config.be_polite {
|
||||||
println!("CTRL-C pressed, bye!\n");
|
println!("{}, bye!\n", quit_reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -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 stdout = io::stdout();
|
||||||
|
|
||||||
let mut chronometer = Chronometer::new();
|
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 lapses: Vec<Lapse> = vec![];
|
||||||
let mut scroll_offset: usize = 0;
|
let mut scroll_offset: usize = 0;
|
||||||
|
|
||||||
let mut quit = false;
|
let mut quit_reason = None;
|
||||||
while !quit {
|
while quit_reason.is_none() {
|
||||||
// Handle events
|
// Handle events
|
||||||
while event::poll(Duration::ZERO)? {
|
while event::poll(Duration::ZERO)? {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(e) => match e.code {
|
Event::Key(e) => match e.code {
|
||||||
// Handle CTRL-C
|
// Handle quit via CTRL-C or Q
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
if e.modifiers.contains(KeyModifiers::CONTROL) {
|
if e.modifiers.contains(KeyModifiers::CONTROL) {
|
||||||
quit = true;
|
quit_reason = Some("CTRL-C pressed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('q') => quit_reason = Some("Q pressed"),
|
||||||
// Handle pause
|
// Handle pause
|
||||||
KeyCode::Char(' ') => {
|
KeyCode::Char(' ') => {
|
||||||
chronometer.toggle_pause();
|
chronometer.toggle_pause();
|
||||||
@@ -146,7 +147,7 @@ pub fn main_loop(config: &mut Config) -> io::Result<()> {
|
|||||||
thread::sleep(Duration::from_millis(1000 / config.fps));
|
thread::sleep(Duration::from_millis(1000 / config.fps));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(quit_reason.unwrap().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_frame(
|
fn render_frame(
|
||||||
|
|||||||
@@ -16,21 +16,22 @@ use crate::{
|
|||||||
rendering::{self, symbols},
|
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 stdout = io::stdout();
|
||||||
|
|
||||||
let mut quit = false;
|
let mut quit_reason = None;
|
||||||
while !quit {
|
while quit_reason.is_none() {
|
||||||
// Handle events
|
// Handle events
|
||||||
while event::poll(Duration::ZERO)? {
|
while event::poll(Duration::ZERO)? {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(e) => match e.code {
|
Event::Key(e) => match e.code {
|
||||||
// Handle CTRL-C
|
// Handle quit via CTRL-C or Q
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
if e.modifiers.contains(KeyModifiers::CONTROL) {
|
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));
|
thread::sleep(Duration::from_millis(1000 / config.fps));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(quit_reason.unwrap().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_frame(config: &Config) -> io::Result<()> {
|
fn render_frame(config: &Config) -> io::Result<()> {
|
||||||
|
|||||||
@@ -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 mut stdout = io::stdout();
|
||||||
|
|
||||||
let duration = parse_duration::parse(duration)
|
let duration = parse_duration::parse(duration)
|
||||||
.unwrap_or_else(|_| eprintln_quit!("Invalid duration provided"));
|
.unwrap_or_else(|_| eprintln_quit!("Invalid duration provided"));
|
||||||
let mut timer = Timer::new(duration);
|
let mut timer = Timer::new(duration);
|
||||||
|
|
||||||
let mut quit = false;
|
let mut quit_reason = None;
|
||||||
while !quit {
|
while quit_reason.is_none() {
|
||||||
// Handle events
|
// Handle events
|
||||||
while event::poll(Duration::ZERO)? {
|
while event::poll(Duration::ZERO)? {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(e) => match e.code {
|
Event::Key(e) => match e.code {
|
||||||
// Handle CTRL-C
|
// Handle quit via CTRL-C or Q
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
if e.modifiers.contains(KeyModifiers::CONTROL) {
|
if e.modifiers.contains(KeyModifiers::CONTROL) {
|
||||||
quit = true;
|
quit_reason = Some("CTRL-C pressed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('q') => quit_reason = Some("Q pressed"),
|
||||||
// Handle pause
|
// Handle pause
|
||||||
KeyCode::Char(' ') => {
|
KeyCode::Char(' ') => {
|
||||||
timer.toggle_pause();
|
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));
|
thread::sleep(Duration::from_millis(1000 / config.fps));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(quit_reason.unwrap().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_frame(config: &Config, timer: &Timer) -> io::Result<()> {
|
fn render_frame(config: &Config, timer: &Timer) -> io::Result<()> {
|
||||||
|
|||||||
Reference in New Issue
Block a user