Skip to content

Commit

Permalink
feat: make segment option required
Browse files Browse the repository at this point in the history
* make errors use a macro for shorter code
  • Loading branch information
flazepe committed Oct 21, 2024
1 parent 59290f4 commit cc6e21c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Args {
pub input: String,

/// The segment duration range to add (e.g. "--segment 2:00-2:30"). This option can be repeated to add more segments
#[arg(short, long = "segment", id = "DURATION RANGE")]
#[arg(short, long = "segment", id = "DURATION RANGE", required = true)]
pub segments: Vec<String>,

/// The CQ, if using NVENC
Expand Down
24 changes: 12 additions & 12 deletions src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::args::Args;
use crate::{args::Args, error};
use std::{fmt::Display, process::exit, process::Command};

pub fn spawn_ffmpeg(args: Args) {
Expand All @@ -11,10 +11,7 @@ pub fn spawn_ffmpeg(args: Args) {
let (from, to) = segment
.split_once('-')
.map(|(from, to)| (duration_to_secs(from), duration_to_secs(to)))
.unwrap_or_else(|| {
print_error("A non-range value found inside a segment!");
exit(1);
});
.unwrap_or_else(|| error!("A non-range value found inside a segment!"));

let fade_to = to - (args.fade.unwrap_or(0.) + 0.5);

Expand Down Expand Up @@ -96,7 +93,7 @@ pub fn spawn_ffmpeg(args: Args) {
arg.to_string()
})
.collect::<Vec<String>>()
.join(" ")
.join(" "),
);
}

Expand All @@ -111,10 +108,9 @@ fn duration_to_secs<T: Display>(duration: T) -> f64 {
.to_string()
.split(':')
.map(|entry| {
entry.parse::<f64>().unwrap_or_else(|_| {
print_error("Invalid segment duration found inside a range!");
exit(1);
})
entry
.parse::<f64>()
.unwrap_or_else(|_| error!("Invalid segment duration found inside a range!"))
})
.collect::<Vec<f64>>();

Expand All @@ -126,6 +122,10 @@ fn duration_to_secs<T: Display>(duration: T) -> f64 {
}
}

fn print_error(message: &str) {
println!("\x1b[38;5;203m{message}\x1b[0m")
#[macro_export]
macro_rules! error {
($message:expr) => {{
println!("\x1b[38;5;203m{}\x1b[0m", $message);
exit(1);
}};
}

0 comments on commit cc6e21c

Please sign in to comment.