diff --git a/src/main.rs b/src/main.rs index 8141959..f1ba9d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,8 @@ use clap::Parser; use tokio::time::{sleep_until, Instant, Duration}; use tokio::process::Command; use serde::{Serialize, Deserialize}; -use chrono::NaiveTime; +use chrono::prelude::*; + #[tokio::main] async fn main() -> Result<()> { @@ -20,11 +21,14 @@ async fn main() -> Result<()> { } }; - // Print the recording details - println!("Recording starts in {}min for {}min", args.start_minutes, args.duration_minutes); + let (begin, end) = calculate_begin_end(&args)?; + + + // Print the recording details + println!("Recording starts in {:?} for {:?}", begin, end); // Wait the given amount of time - sleep_until(Instant::now() + Duration::from_secs(args.start_minutes * 60)).await; + sleep_until(Instant::now() + begin).await; // Connect to the OBS instance through obs-websocket. let client = Client::connect("localhost", cfg.websocket_port, Some(cfg.websocket_password)).await?; @@ -41,7 +45,7 @@ async fn main() -> Result<()> { println!("Recording started."); // Wait for the recording to finish - sleep_until(Instant::now() + Duration::from_secs(args.duration_minutes * 60)).await; + sleep_until(Instant::now() + end).await; // Stop the recording stop_recording(&client).await?; @@ -78,11 +82,40 @@ async fn shutdown() -> Result<(), anyhow::Error> { Ok(()) } -fn parse_time(time: &str) -> Result { +fn parse_time(time: &str) -> Result { let parsed = NaiveTime::parse_from_str(time, "%H:%M")?; - Ok(parsed) + let local = Local::now(); + let datetime = local.date().and_time(parsed); + if let Some(dt) = datetime { + let duration = dt - local; + return Ok(duration); + } + anyhow::bail!("Can't convert to duration.") } +fn calculate_begin_end(args: &Args) -> Result<(Duration, Duration), anyhow::Error> { + let begin; + let end; + + if let Some(btime) = args.begin_time.clone() { + let pt = parse_time(&btime)?; + begin = pt.to_std()?; + } else { + begin = Duration::from_secs(args.start_minutes * 60); + } + + if let Some(etime) = args.end_time.clone() { + let pt = parse_time(&etime)?; + + // We wait for the begin duration and have to substract that from the end duration, + // so we get the recording duration. + end = pt.to_std()? - begin; + } else { + end = Duration::from_secs(args.duration_minutes * 60); + } + + Ok((begin, end)) +} /// obs-cli is a simple cli tool for planned OBS recordings #[derive(Parser, Debug)] @@ -93,16 +126,16 @@ struct Args { start_minutes: u64, /// Define the duration of the recording in minutes - #[arg(short, long, default_value_t = 1)] + #[arg(short, long, default_value_t = 0)] duration_minutes: u64, /// Define the start time in HH:MM format - #[arg(short, long, default_value_t = String::from("14:00"))] - begin_time: String, + #[arg(short, long, default_value = None)] + begin_time: Option, /// Define the end time in HH:MM format - #[arg(short, long, default_value_t = String::from("14:00"))] - end_time: String, + #[arg(short, long, default_value = None)] + end_time: Option, /// Flag to shutdown the machine after recording. #[arg(short, long, default_value_t = false)]