From e278e871c357c4832f29629e4f65f57ff0d0ded0 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sat, 24 Feb 2024 09:16:44 +0400 Subject: Expand ~ in spawn --- src/utils/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/utils/mod.rs') diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e83378ae..df21ffdb 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,7 +1,7 @@ use std::ffi::{CString, OsStr}; use std::io::Write; use std::os::unix::prelude::OsStrExt; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::ptr::null_mut; use std::sync::atomic::AtomicBool; use std::time::Duration; @@ -50,6 +50,15 @@ pub fn output_size(output: &Output) -> Size { .to_logical(output_scale) } +pub fn expand_home(path: &Path) -> anyhow::Result> { + if let Ok(rest) = path.strip_prefix("~") { + let dirs = UserDirs::new().context("error retrieving home directory")?; + Ok(Some([dirs.home_dir(), rest].iter().collect())) + } else { + Ok(None) + } +} + pub fn make_screenshot_path(config: &Config) -> anyhow::Result> { let Some(path) = &config.screenshot_path else { return Ok(None); @@ -72,9 +81,8 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result> path = PathBuf::from(OsStr::from_bytes(&buf[..rv])); } - if let Ok(rest) = path.strip_prefix("~") { - let dirs = UserDirs::new().context("error retrieving home directory")?; - path = [dirs.home_dir(), rest].iter().collect(); + if let Some(expanded) = expand_home(&path).context("error expanding ~")? { + path = expanded; } Ok(Some(path)) -- cgit