diff --git a/Cargo.lock b/Cargo.lock index 7870e087..fd1b0933 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -813,6 +813,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" +[[package]] +name = "shellexpand" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da03fa3b94cc19e3ebfc88c4229c49d8f08cdbd1228870a45f0ffdf84988e14b" +dependencies = [ + "dirs", +] + [[package]] name = "simd-adler32" version = "0.3.7" @@ -1160,6 +1169,7 @@ dependencies = [ "libwayshot", "nix 0.28.0", "serde", + "shellexpand", "toml", "tracing", "tracing-subscriber", diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 3aee22f8..41688da3 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -40,6 +40,7 @@ nix = { version = "0.28.0", features = ["process"] } toml = { version = "0.8.12", default-features = false, features = ["parse"] } serde = { version = "1.0.197", features = ["derive"] } dirs = "5.0.1" +shellexpand = "3.1.0" [[bin]] name = "wayshot" diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index 480b2abd..b4f3a040 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -4,6 +4,7 @@ use eyre::{bail, ContextCompat, Error, Result}; use libwayshot::region::{LogicalRegion, Position, Region, Size}; use serde::{Deserialize, Serialize}; use std::{ + env, fmt::{Display, Write}, path::{Path, PathBuf}, str::FromStr, @@ -138,10 +139,21 @@ impl FromStr for EncodingFormat { } } +pub fn get_checked_path(path: &str) -> PathBuf { + let checked_path = shellexpand::full(path); + + if let Ok(checked_path) = checked_path { + PathBuf::from(checked_path.into_owned()) + } else { + env::current_dir().unwrap_or_default() + } +} + pub fn get_full_file_name(dir: &Path, filename_format: &str, encoding: EncodingFormat) -> PathBuf { let filename = get_default_file_name(filename_format, encoding); - dir.join(filename) + let checked_path = get_checked_path(dir.to_str().unwrap_or_default()); + checked_path.join(filename) } pub fn get_default_file_name(filename_format: &str, encoding: EncodingFormat) -> PathBuf { diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 0510b4aa..6b912f11 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -84,7 +84,7 @@ fn main() -> Result<()> { if f.is_dir() { Some(utils::get_full_file_name(&f, &filename_format, encoding)) } else { - Some(f) + Some(utils::get_checked_path(f.to_str().unwrap_or_default())) } } _ => {