mirror of
https://github.com/niri-wm/niri.git
synced 2026-06-24 02:01:18 +07:00
Expand ~ in spawn
This commit is contained in:
+12
-4
@@ -1,7 +1,7 @@
|
|||||||
use std::ffi::{CString, OsStr};
|
use std::ffi::{CString, OsStr};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::os::unix::prelude::OsStrExt;
|
use std::os::unix::prelude::OsStrExt;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use std::ptr::null_mut;
|
use std::ptr::null_mut;
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -50,6 +50,15 @@ pub fn output_size(output: &Output) -> Size<i32, Logical> {
|
|||||||
.to_logical(output_scale)
|
.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>> {
|
pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
|
||||||
let Some(path) = &config.screenshot_path else {
|
let Some(path) = &config.screenshot_path else {
|
||||||
return Ok(None);
|
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]));
|
path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(rest) = path.strip_prefix("~") {
|
if let Some(expanded) = expand_home(&path).context("error expanding ~")? {
|
||||||
let dirs = UserDirs::new().context("error retrieving home directory")?;
|
path = expanded;
|
||||||
path = [dirs.home_dir(), rest].iter().collect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(path))
|
Ok(Some(path))
|
||||||
|
|||||||
+14
-2
@@ -1,6 +1,7 @@
|
|||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
|
use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
|
||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
|
use std::path::Path;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::{io, thread};
|
use std::{io, thread};
|
||||||
@@ -10,6 +11,8 @@ use smithay::reexports::rustix;
|
|||||||
use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};
|
use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};
|
||||||
use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};
|
use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};
|
||||||
|
|
||||||
|
use crate::utils::expand_home;
|
||||||
|
|
||||||
pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
|
pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
|
||||||
pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
|
pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
@@ -37,7 +40,17 @@ pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {
|
|||||||
fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
|
fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
|
||||||
let _span = tracy_client::span!();
|
let _span = tracy_client::span!();
|
||||||
|
|
||||||
let command = command.as_ref();
|
let mut command = command.as_ref();
|
||||||
|
|
||||||
|
// Expand `~` at the start.
|
||||||
|
let expanded = expand_home(Path::new(command));
|
||||||
|
match &expanded {
|
||||||
|
Ok(Some(expanded)) => command = expanded.as_ref(),
|
||||||
|
Ok(None) => (),
|
||||||
|
Err(err) => {
|
||||||
|
warn!("error expanding ~: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut process = Command::new(command);
|
let mut process = Command::new(command);
|
||||||
process
|
process
|
||||||
@@ -221,7 +234,6 @@ fn read_all(fd: impl AsFd, buf: &mut [u8]) -> rustix::io::Result<()> {
|
|||||||
fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {
|
fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
|||||||
Reference in New Issue
Block a user