Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update width and height options #263

Merged
merged 10 commits into from
Aug 23, 2024
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ build-binary = [
"dep:indicatif",
"dep:indicatif-log-bridge",
"dep:console",
"dep:regex",
]

# Enables utilization of threads
Expand Down Expand Up @@ -118,6 +119,7 @@ kamadak-exif = { version = "0.5.5", optional = true }
indicatif = { version = "0.17.8", features = ["rayon"], optional = true }
indicatif-log-bridge = { version = "0.2.2", optional = true }
console = { version = "0.15.8", optional = true }
regex = { version = "1.10.6", optional = true }

[target.'cfg(windows)'.dependencies]
glob = { version = "0.3.1", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/cli/preprocessors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl Preprocessors for Command {
- @1.5: Enlarge image size by this multiplier
- 150%: Adjust image size by this percentage
- 100x100: Resize image to these dimensions
- 200x_: Adjust image dimensions while maintaining the aspect ratio based on the specified dimension"#})
- 100w: Adjust image dimensions while maintaining the aspect ratio based on the width
- 100h: Adjust image dimensions while maintaining the aspect ratio based on the height"#})
.value_parser(value_parser!(ResizeValue))
.action(ArgAction::Append),

Expand Down
86 changes: 52 additions & 34 deletions src/cli/preprocessors/resize/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::anyhow;
use regex::Regex;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResizeValue {
Expand Down Expand Up @@ -48,8 +49,8 @@ impl std::fmt::Display for ResizeValue {
ResizeValue::Dimensions(Some(width), Some(height)) => {
f.write_fmt(format_args!("{width}x{height}"))
}
ResizeValue::Dimensions(Some(width), None) => f.write_fmt(format_args!("{width}x_")),
ResizeValue::Dimensions(None, Some(height)) => f.write_fmt(format_args!("_x{height}")),
ResizeValue::Dimensions(Some(width), None) => f.write_fmt(format_args!("{width}w")),
ResizeValue::Dimensions(None, Some(height)) => f.write_fmt(format_args!("{height}h")),
ResizeValue::Dimensions(None, None) => f.write_fmt(format_args!("base")),
}
}
Expand All @@ -59,26 +60,37 @@ impl std::str::FromStr for ResizeValue {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim().to_lowercase();

match s {
s if s.starts_with('@') => Ok(Self::Multiplier(s[1..].parse()?)),
s if s.ends_with('%') => Ok(Self::Percentage(s[..s.len() - 1].parse()?)),
s if s.contains('w') && s.contains('h') => Err(anyhow!("Invalid resize value")),
s if s.contains('w') => {
Mikachu2333 marked this conversation as resolved.
Show resolved Hide resolved
let re = Regex::new(r"(?P<width>\d+)").unwrap();
let Some(cap) = re.captures(&s) else {
return Err(anyhow!("Invalid resize value"));
};

Ok(Self::Dimensions(Some(cap["width"].parse()?), None))
}
s if s.contains('h') => {
let re = Regex::new(r"(?P<height>\d+)").unwrap();
let Some(cap) = re.captures(&s) else {
return Err(anyhow!("Invalid resize value"));
};

Ok(Self::Dimensions(None, Some(cap["height"].parse()?)))
}
s if s.contains('x') => {
let dimensions: Vec<&str> = s.split('x').collect();
if dimensions.len() > 2 {
return Err(anyhow!("There is more that 2 dimensions"));
}

let width = if dimensions[0] == "_" {
None
} else {
Some(dimensions[0].parse::<usize>()?)
};
let width = Some(dimensions[0].parse::<usize>()?);

let height = if dimensions[1] == "_" {
None
} else {
Some(dimensions[1].parse::<usize>()?)
};
let height = Some(dimensions[1].parse::<usize>()?);

Ok(Self::Dimensions(width, height))
}
Expand Down Expand Up @@ -108,23 +120,11 @@ mod tests {
"150x150"
);

assert_eq!(
ResizeValue::Dimensions(None, Some(200)).to_string(),
"_x200"
);
assert_eq!(
ResizeValue::Dimensions(None, Some(150)).to_string(),
"_x150"
);
assert_eq!(ResizeValue::Dimensions(None, Some(200)).to_string(), "200h");
assert_eq!(ResizeValue::Dimensions(None, Some(150)).to_string(), "150h");

assert_eq!(
ResizeValue::Dimensions(Some(200), None).to_string(),
"200x_"
);
assert_eq!(
ResizeValue::Dimensions(Some(150), None).to_string(),
"150x_"
);
assert_eq!(ResizeValue::Dimensions(Some(200), None).to_string(), "200w");
assert_eq!(ResizeValue::Dimensions(Some(150), None).to_string(), "150w");

assert_eq!(ResizeValue::Dimensions(None, None).to_string(), "base");
}
Expand Down Expand Up @@ -162,29 +162,47 @@ mod tests {
);

assert_eq!(
"200x_".parse::<ResizeValue>().unwrap(),
"200w".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(Some(200), None)
);

assert_eq!(
"150x_".parse::<ResizeValue>().unwrap(),
"150w".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(Some(150), None)
);

assert_eq!(
"_x200".parse::<ResizeValue>().unwrap(),
"200h".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, Some(200))
);

assert_eq!(
"_x150".parse::<ResizeValue>().unwrap(),
"150h".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, Some(150))
);

assert_eq!(
"_x_".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, None)
"200 w".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(Some(200), None)
);

assert_eq!(
"150 w".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(Some(150), None)
);

assert_eq!(
"h200".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, Some(200))
);

assert_eq!(
"h150".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, Some(150))
);

assert!("_x_".parse::<ResizeValue>().is_err());
assert!("150wh".parse::<ResizeValue>().is_err());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn main() {
.iter()
.map(|r| r.output.display().to_string().len())
.max()
.unwrap();
.unwrap_or(0);

if !quiet {
let term = Term::stdout();
Expand Down