From f55a67b6264566d1e5632f5f9185fda5348d796f Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Wed, 9 Mar 2022 18:23:26 +0100 Subject: [PATCH] add `--timeout` arg --- src/cli/parse.rs | 14 ++++++++++++-- src/main.rs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/cli/parse.rs b/src/cli/parse.rs index eabab05..3343075 100644 --- a/src/cli/parse.rs +++ b/src/cli/parse.rs @@ -1,7 +1,7 @@ use clap::Parser; use clap_complete::Shell; -#[derive(Parser, Debug)] +#[derive(Debug, Parser, Clone)] #[clap(name = "discordrpc")] pub struct Cli { #[clap( @@ -120,11 +120,21 @@ pub struct Cli { )] pub enable_time: bool, + #[clap( + short = 'E', + long = "timeout", + help = "Exit after X seconds", + default_value="0", + required = false, + display_order = 13 + )] + pub timeout: u64, + #[clap( long = "print-completions", value_name = "shell", arg_enum, - display_order = 13 + display_order = 14 )] pub print_completions: Option, } diff --git a/src/main.rs b/src/main.rs index 78bdd80..a6139a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn main() { } // * start discord rpc - execute::run(args); + execute::run(args.clone()); println!( "{} {}", @@ -37,8 +37,16 @@ fn main() { "Press Ctrl+C to exit!".magenta() ); - loop { - // * empty `loop {}` wastes CPU cycles - sleep(Duration::new(9999999, 9999999)); + if args.timeout != 0 { + sleep(Duration::from_secs(args.timeout)); + + println!("{}", "Stopping due to timeout...".blue()); + + exit(0) + } else { + loop { + // * empty `loop {}` wastes CPU cycles + sleep(Duration::from_secs(9999999)); + } } }