Skip to content

Commit

Permalink
Handle HOME env var/tilde in config paths with shellexpand
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigas002 committed Mar 28, 2024
1 parent ef9e96e commit 8ac93d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 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 wayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion wayshot/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
_ => {
Expand Down

0 comments on commit 8ac93d7

Please sign in to comment.