Skip to content

Commit

Permalink
feat: Support shell expansion for file arguments (#46)
Browse files Browse the repository at this point in the history
* Added shellexpand dependency

* Added shell file expansion for 'analyze' command

* Added shell file expansion for 'split' command

* Added shell file expansion for 'edit' command

* Added shell file expansion for file saving

* Added shell file expansion for 'view' command

* switchded to scoped calls to `shellexpand::full`

* added test for tilde path expansion

* Removed unnecessary code and dependency

* chore: Update Cargo.lock

---------

Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
  • Loading branch information
JerelJr and orhun committed Oct 18, 2023
1 parent 6948eff commit a411ad3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 40 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ colored = "2.0.0"
log = "0.4.17"
fern_colored = { version = "0.6.1", features = ["colored"] }
thiserror = "1.0.38"
shellexpand = "3.1.0"

[dependencies.gifski]
version = "1.10.0"
Expand Down
6 changes: 5 additions & 1 deletion src/analyze/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ impl AnalyzeSettings {
match parser.args {
Some(matches) => {
let timestamp = matches.is_present("timestamp");
let file = matches.value_of("file").unwrap_or_default();
let file = shellexpand::full(file)
.map(|s| s.to_string())
.unwrap_or(file.to_string());
Self::new(
PathBuf::from(matches.value_of("file").unwrap_or_default()),
PathBuf::from(file),
color.unwrap_or(Self::default().color),
match matches.value_of("time-zone") {
Some("local") => TimeZone::Local(timestamp),
Expand Down
22 changes: 19 additions & 3 deletions src/anim/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ impl AnimSettings {
*/
fn get_frames(args: &ArgMatches<'_>) -> Vec<PathBuf> {
let mut values = if let Some(dir) = args.value_of("dir") {
let dir = shellexpand::full(dir)
.map(|s| s.to_string())
.unwrap_or(dir.to_string());
fs::read_dir(dir)
.expect("Could not read files from directory")
.map(|entry| {
Expand Down Expand Up @@ -212,10 +215,18 @@ impl SplitSettings {
fn from_parser(parser: ArgParser<'_>) -> Self {
match parser.args {
Some(matches) => {
let file =
PathBuf::from(matches.value_of("file").unwrap_or_default());
let file = matches.value_of("file").unwrap_or_default();
let file = shellexpand::full(file)
.map(|s| s.to_string())
.unwrap_or(file.to_string());
let file = PathBuf::from(file);
let dir = match matches.value_of("dir") {
Some(dir) => PathBuf::from(dir),
Some(dir) => {
let dir = shellexpand::full(dir)
.map(|s| s.to_string())
.unwrap_or(dir.to_string());
PathBuf::from(dir)
}
None => File::get_default_path(&format!(
"{}_frames",
file.file_stem()
Expand Down Expand Up @@ -293,5 +304,10 @@ mod tests {
let split_settings = SplitSettings::from_parser(ArgParser::from_args(&args));
assert_eq!(PathBuf::from("x"), split_settings.file);
assert_eq!(Some(OsStr::new("x_frames")), split_settings.dir.file_name());
let args = App::new("test")
.arg(Arg::with_name("dir").long("dir").takes_value(true))
.get_matches_from(vec!["test", "--dir", "~/"]);
let split_settings = SplitSettings::from_parser(ArgParser::from_args(&args));
assert_eq!(dirs::home_dir().unwrap(), split_settings.dir)
}
}
71 changes: 41 additions & 30 deletions src/edit/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,36 +187,47 @@ impl EditSettings {
*/
fn from_parser(parser: ArgParser<'_>) -> Self {
match parser.args {
Some(ref matches) => Self::new(
PathBuf::from(matches.value_of("file").unwrap_or_default()),
matches.is_present("convert"),
ImageSettings::new(
Padding::parse(matches.value_of("crop").unwrap_or_default()),
Geometry::parse(matches.value_of("resize").unwrap_or_default()),
parser.parse("ratio", ImageSettings::default().ratio),
match matches.value_of("flip") {
Some("horizontal") => Some(Flip::Horizontal),
Some("vertical") => Some(Flip::Vertical),
_ => None,
},
parser.parse("rotate", ImageSettings::default().rotate),
parser.parse("blur", ImageSettings::default().blur),
match matches.value_of("filter") {
Some("nearest") => FilterType::Nearest,
Some("triangle") => FilterType::Triangle,
Some("catmull-rom") => FilterType::CatmullRom,
Some("gaussian") => FilterType::Gaussian,
_ => FilterType::Lanczos3,
},
),
ColorSettings::new(
matches.is_present("grayscale"),
matches.is_present("invert"),
parser.parse("hue", ColorSettings::default().hue),
parser.parse("contrast", ColorSettings::default().contrast),
parser.parse("brightness", ColorSettings::default().brightness),
),
),
Some(ref matches) => {
let file = matches.value_of("file").unwrap_or_default();
let file = shellexpand::full(file)
.map(|s| s.to_string())
.unwrap_or(file.to_string());
Self::new(
PathBuf::from(file),
matches.is_present("convert"),
ImageSettings::new(
Padding::parse(matches.value_of("crop").unwrap_or_default()),
Geometry::parse(
matches.value_of("resize").unwrap_or_default(),
),
parser.parse("ratio", ImageSettings::default().ratio),
match matches.value_of("flip") {
Some("horizontal") => Some(Flip::Horizontal),
Some("vertical") => Some(Flip::Vertical),
_ => None,
},
parser.parse("rotate", ImageSettings::default().rotate),
parser.parse("blur", ImageSettings::default().blur),
match matches.value_of("filter") {
Some("nearest") => FilterType::Nearest,
Some("triangle") => FilterType::Triangle,
Some("catmull-rom") => FilterType::CatmullRom,
Some("gaussian") => FilterType::Gaussian,
_ => FilterType::Lanczos3,
},
),
ColorSettings::new(
matches.is_present("grayscale"),
matches.is_present("invert"),
parser.parse("hue", ColorSettings::default().hue),
parser.parse("contrast", ColorSettings::default().contrast),
parser.parse(
"brightness",
ColorSettings::default().brightness,
),
),
)
}
None => Self::default(),
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/file/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ impl SaveSettings {
fn from_parser(parser: ArgParser<'_>, file_format: FileFormat) -> Self {
match parser.args {
Some(matches) => {
let mut path =
PathBuf::from(matches.value_of("file").unwrap_or_default());
let file = matches.value_of("file").unwrap_or_default();
let file = shellexpand::full(file)
.map(|s| s.to_string())
.unwrap_or(file.to_string());
let mut path = PathBuf::from(file);
if let Some(info) = FileInfo::from_args(&matches) {
path.set_file_name(format!(
"{}_{}{}",
Expand Down
11 changes: 7 additions & 4 deletions src/view/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ impl ViewSettings {
*/
fn from_parser(parser: ArgParser<'_>) -> Self {
match parser.args {
Some(matches) => Self::new(
PathBuf::from(matches.value_of("file").unwrap_or_default()),
matches.is_present("transparent"),
),
Some(matches) => {
let file = matches.value_of("file").unwrap_or_default();
let file = shellexpand::full(file)
.map(|s| s.to_string())
.unwrap_or(file.to_string());
Self::new(PathBuf::from(file), matches.is_present("transparent"))
}
None => Self::default(),
}
}
Expand Down

0 comments on commit a411ad3

Please sign in to comment.