Skip to content

Commit

Permalink
Option for output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
grimace87 committed Mar 4, 2024
1 parent 1dd08ac commit 540700b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
39 changes: 25 additions & 14 deletions crates/stitchy/src/file_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,40 @@ pub fn to_absolute_dir(path_string: &String) -> Result<PathBuf, String> {
Ok(path)
}

pub fn next_available_output(sources: &ImageFiles<FilePathWithMetadata>, options: &Opt) -> Result<PathBuf, String> {
pub fn next_available_output(
sources: &ImageFiles<FilePathWithMetadata>,
options: &Opt
) -> Result<PathBuf, String> {

let target_extension = ImageFiles::<FilePathWithMetadata>
::get_main_extension(determine_output_format(sources, options)?)
.unwrap_or("jpg");

// Get current path, check if the default file name exists, if not return it
let mut current_path: PathBuf = match std::env::current_dir() {
Ok(dir) => dir,
Err(_) => return Err(String::from("Could not access current directory"))
let mut output_file_path: PathBuf = match &options.output_dir {
Some(output_path) => {
to_absolute_dir(output_path)?
},
None => {
match std::env::current_dir() {
Ok(dir) => dir,
Err(_) => return Err(String::from("Could not access current directory"))
}
}
};

// Get current path, check if the default file name exists, if not return it
let mut un_numbered_file_exists = false;
for &extension in ImageFiles::<FilePathWithMetadata>::allowed_extensions().iter() {
current_path.push(format!("stitch.{}", extension));
if current_path.is_file() {
output_file_path.push(format!("stitch.{}", extension));
if output_file_path.is_file() {
un_numbered_file_exists = true;
current_path.pop();
output_file_path.pop();
break;
}
current_path.pop();
output_file_path.pop();
}
if !un_numbered_file_exists {
let mut path = current_path.clone();
let mut path = output_file_path.clone();
path.push(format!("stitch.{}", target_extension));
return Ok(path);
}
Expand All @@ -47,17 +58,17 @@ pub fn next_available_output(sources: &ImageFiles<FilePathWithMetadata>, options
let mut numbered_file_exists = false;
for &extension in ImageFiles::<FilePathWithMetadata>::allowed_extensions().iter() {
let file_name: String = format!("stitch_{}.{}", i, extension);
current_path.push(file_name);
if current_path.is_file() {
output_file_path.push(file_name);
if output_file_path.is_file() {
numbered_file_exists = true;
}
current_path.pop();
output_file_path.pop();
if numbered_file_exists {
break;
}
}
if !numbered_file_exists {
let mut path = current_path.clone();
let mut path = output_file_path.clone();
path.push(format!("stitch_{}.{}", i, target_extension));
return Ok(path);
}
Expand Down
4 changes: 3 additions & 1 deletion crates/stitchy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() {
/// and prepared for use before calling this function.
fn run_with_options(opt: Opt) -> Result<String, String> {

// Determine the list of files to use as input, and from those, determine the output path
// Determine the list of files to use as input
let number_of_files = opt.number_of_files.ok_or_else(|| String::from(
"Internal error - sorting files before verifying that a number was supplied"))?;
let unsorted_sources = match &opt.input_dir {
Expand All @@ -101,6 +101,8 @@ fn run_with_options(opt: Opt) -> Result<String, String> {
opt.take_from.unwrap_or(TakeFrom::Start),
opt.reverse
)?;

// Determine the output path, considering the input files if need be
let total_source_size = image_sources.total_size();
let output_format = file_util::determine_output_format(&image_sources, &opt)?;
let output_file_path = file_util::next_available_output(&image_sources, &opt)?;
Expand Down
16 changes: 16 additions & 0 deletions crates/stitchy/src/options/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub struct Opt {
#[arg(short, long = "input-dir")]
pub input_dir: Option<String>,

#[arg(short, long = "output-dir")]
pub output_dir: Option<String>,

#[arg(required_unless_present_any =
&["help", "version", "setdefaults", "cleardefaults", "printdefaults"])]
pub number_of_files: Option<usize>,
Expand Down Expand Up @@ -96,6 +99,7 @@ impl Default for Opt {
quality: DEFAULT_QUALITY,
order: None,
input_dir: None,
output_dir: None,
number_of_files: None,
setdefaults: false,
cleardefaults: false
Expand Down Expand Up @@ -154,6 +158,11 @@ impl Opt {
return Some(e);
}
}
if let Some(dir) = &self.output_dir {
if let Err(e) = to_absolute_dir(dir) {
return Some(e);
}
}

// Verify not requesting both horizontal and vertical
if self.horizontal && self.vertical {
Expand Down Expand Up @@ -274,6 +283,11 @@ impl Opt {
(Some(this), _) => Some(this.clone()),
_ => None
};
let output_dir = match (&self.output_dir, &other.output_dir) {
(None, Some(that)) => Some(that.clone()),
(Some(this), _) => Some(this.clone()),
_ => None
};
Opt {
help: self.help,
version: self.version,
Expand All @@ -292,6 +306,7 @@ impl Opt {
quality: if self.quality != DEFAULT_QUALITY { self.quality } else { other.quality },
order,
input_dir,
output_dir,
number_of_files,
setdefaults: self.setdefaults,
cleardefaults: self.cleardefaults
Expand Down Expand Up @@ -321,6 +336,7 @@ impl From<OptV2> for Opt {
quality: value.quality,
order: value.order,
input_dir: None,
output_dir: None,
number_of_files: value.number_of_files,
setdefaults: value.setdefaults,
cleardefaults: value.cleardefaults,
Expand Down

0 comments on commit 540700b

Please sign in to comment.