Skip to content

Commit

Permalink
📦 Get value from verbose-arg always via lazy_static
Browse files Browse the repository at this point in the history
  • Loading branch information
tgotwig committed Jul 19, 2024
1 parent 4797fca commit a7e17ac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
27 changes: 11 additions & 16 deletions src/commanders/_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::cli::Cli;
use clap::ArgMatches;
use lazy_static::lazy_static;
use path_slash::PathBufExt;
use std::{
io::Error,
Expand All @@ -8,17 +10,19 @@ use std::{
use term_painter::Color::BrightBlue;
use term_painter::ToStyle;

pub fn merge(input: String, output: &String) -> Result<Child, std::io::Error> {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");
lazy_static! {
static ref MATCHES: ArgMatches = Cli::init().get_matches();
static ref VERBOSE: bool = MATCHES.is_present("verbose");
}

pub fn merge(input: String, output: &String) -> Result<Child, std::io::Error> {
let cmd = format!(
"ffmpeg -y -f concat -safe 0 -i {} -map 0 -c copy {}",
input, output
);

println!("🚀 Run Merger, calling: {}", BrightBlue.paint(&cmd));
if verbose {
if *VERBOSE {
execute_cmd(cmd)
} else {
execute_cmd_silently(cmd)
Expand All @@ -30,9 +34,6 @@ pub fn merge_with_chapters(
file_path: PathBuf,
output_file_for_chapterer: &str,
) -> Result<Child, std::io::Error> {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");

let cmd = format!(
"ffmpeg -y -i {} -i {} -map 0 -map_metadata 1 -codec copy {}",
&input_file_for_chapterer,
Expand All @@ -41,7 +42,7 @@ pub fn merge_with_chapters(
);

println!("📖 Run Chapterer, calling: {}", BrightBlue.paint(&cmd));
if verbose {
if *VERBOSE {
execute_cmd(cmd)
} else {
execute_cmd_silently(cmd)
Expand All @@ -59,9 +60,6 @@ pub fn adjust_fps_by_ffmpeg(
fps_goal: &f32,
new_file_location: PathBuf,
) -> PathBuf {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");

let cmd = format!(
"ffmpeg -i {} -r {} {}",
file_to_merge.to_str().unwrap(),
Expand All @@ -73,7 +71,7 @@ pub fn adjust_fps_by_ffmpeg(
// let res = execute_cmd(cmd).unwrap().wait_with_output();
// println!("{:?}", res);

if verbose {
if *VERBOSE {
let res = execute_cmd(cmd).unwrap().wait_with_output();
println!("{:?}", res);
} else {
Expand All @@ -86,15 +84,12 @@ pub fn adjust_fps_by_ffmpeg(
}

pub fn get_media_seconds(media_path: &str) -> Result<f64, Box<Error>> {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");

let cmd = format!(
"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 '{}'",
media_path
);

if verbose {
if *VERBOSE {
println!(
"📖 Getting media seconds, calling: {}",
BrightBlue.paint(&cmd)
Expand Down
14 changes: 9 additions & 5 deletions src/commanders/fps_changer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ use crate::{
str_helper::gen_input_file_content_for_ffmpeg,
},
};
use std::path::Path;
use clap::ArgMatches;
use lazy_static::lazy_static;
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
path::{Path, PathBuf},
};

lazy_static! {
static ref MATCHES: ArgMatches = Cli::init().get_matches();
static ref VERBOSE: bool = MATCHES.is_present("verbose");
}

pub fn change_fps(
files_to_merge: Vec<PathBuf>,
tmp_dir: &Path,
fps_from_cli: f32,
) -> (Vec<PathBuf>, Vec<std::string::String>, std::string::String) {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");
let tmp_dir_for_fps_changer = create_dir_for_fps_changer(tmp_dir).unwrap();

let mut new_files_to_merge = Vec::new();
Expand Down Expand Up @@ -71,7 +75,7 @@ pub fn change_fps(
set.len()
);

if verbose {
if *VERBOSE {
println!();
println!("Will be merged directly: \n");
for line in output_directly {
Expand Down
13 changes: 8 additions & 5 deletions src/helpers/io_helper.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use crate::cli::Cli;
use clap::ArgMatches;
use lazy_static::lazy_static;
use nanoid::nanoid;
use std::env::temp_dir;
use std::fs::{self, canonicalize, File};
use std::io::{self, Result, Write};
use std::path::{Path, PathBuf};
use std::process::exit;

use crate::cli::Cli;
lazy_static! {
static ref MATCHES: ArgMatches = Cli::init().get_matches();
static ref VERBOSE: bool = MATCHES.is_present("verbose");
}

pub fn exit_when_ffmpeg_not_available() {
if which::which("ffmpeg").is_err() {
Expand All @@ -15,11 +21,8 @@ pub fn exit_when_ffmpeg_not_available() {
}

pub fn remove_file(path: &Path) -> Result<()> {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");

if Path::new(path).exists() {
if verbose {
if *VERBOSE {
print!(
"🗑️ Removing old data: `{}`",
path.file_name().unwrap().to_string_lossy()
Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ mod helpers;
use crate::commanders::fps_changer::change_fps;
use crate::commanders::selector::select;
use crate::helpers::str_helper::create_order_of_merging;
use clap::ArgMatches;
use cli::Cli;
use core::time;
use helpers::io_helper::create;
use helpers::io_helper::create_tmp_dir;
use helpers::io_helper::exit_when_ffmpeg_not_available;
use helpers::io_helper::remove_file;
use helpers::str_helper::split;
use lazy_static::lazy_static;
use path_slash::PathExt;
use std::io::Error;
use std::path::Path;
Expand All @@ -20,6 +22,11 @@ use system_shutdown::shutdown;
use term_painter::Color::BrightBlue;
use term_painter::ToStyle;

lazy_static! {
static ref MATCHES: ArgMatches = Cli::init().get_matches();
static ref VERBOSE: bool = MATCHES.is_present("verbose");
}

fn main() -> Result<(), Error> {
let matches = Cli::init().get_matches();
exit_when_ffmpeg_not_available();
Expand All @@ -33,7 +40,6 @@ fn main() -> Result<(), Error> {
let skip_fps_changer = matches.is_present("skip-fps-changer");
let skip_chapterer = matches.is_present("skip-chapterer");
let skip_wait = matches.is_present("skip-wait");
let verbose: bool = matches.is_present("verbose");
let fps_from_cli = matches
.value_of("fps")
.unwrap_or("0")
Expand All @@ -49,7 +55,7 @@ fn main() -> Result<(), Error> {
select(&file_format);

if !ffmpeg_input_content.is_empty() {
if verbose {
if *VERBOSE {
println!("\n\n📜 Order of merging:\n");
println!("{}\n", create_order_of_merging(&ffmpeg_input_content));
if !skip_wait {
Expand Down

0 comments on commit a7e17ac

Please sign in to comment.