aboutsummaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2024-02-24 09:16:44 +0400
committerIvan Molodetskikh <yalterz@gmail.com>2024-02-24 09:16:44 +0400
commite278e871c357c4832f29629e4f65f57ff0d0ded0 (patch)
treed1543eecc3c381f72a13ca32234dca5691048c67 /src/utils/mod.rs
parentab9d1aab4ed13027896a80200775e5679fca9539 (diff)
downloadniri-e278e871c357c4832f29629e4f65f57ff0d0ded0.tar.gz
niri-e278e871c357c4832f29629e4f65f57ff0d0ded0.tar.bz2
niri-e278e871c357c4832f29629e4f65f57ff0d0ded0.zip
Expand ~ in spawn
Diffstat (limited to 'src/utils/mod.rs')
-rw-r--r--src/utils/mod.rs16
1 files changed, 12 insertions, 4 deletions
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<i32, Logical> {
.to_logical(output_scale)
}
+pub fn expand_home(path: &Path) -> anyhow::Result<Option<PathBuf>> {
+ 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<Option<PathBuf>> {
let Some(path) = &config.screenshot_path else {
return Ok(None);
@@ -72,9 +81,8 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>>
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))