diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 14 | ||||
| -rw-r--r-- | src/niri.rs | 18 | ||||
| -rw-r--r-- | src/utils.rs | 36 |
3 files changed, 29 insertions, 39 deletions
diff --git a/src/config.rs b/src/config.rs index 2fb603c7..51607efb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,8 +26,14 @@ pub struct Config { pub preset_column_widths: Vec<PresetWidth>, #[knuffel(child, unwrap(argument), default = 16)] pub gaps: u16, - #[knuffel(child, unwrap(argument), default = Some(PathBuf::from("~/Pictures/Screenshots")))] - pub screenshot_path: Option<PathBuf>, + #[knuffel( + child, + unwrap(argument), + default = Some(String::from( + "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png" + ))) + ] + pub screenshot_path: Option<String>, #[knuffel(child, default)] pub binds: Binds, #[knuffel(child, default)] @@ -541,7 +547,7 @@ mod tests { gaps 8 - screenshot-path "~/Screenshots" + screenshot-path "~/Screenshots/screenshot.png" binds { Mod+T { spawn "alacritty"; } @@ -617,7 +623,7 @@ mod tests { PresetWidth::Fixed(1280), ], gaps: 8, - screenshot_path: Some(PathBuf::from("~/Screenshots")), + screenshot_path: Some(String::from("~/Screenshots/screenshot.png")), binds: Binds(vec![ Bind { key: Key { diff --git a/src/niri.rs b/src/niri.rs index 559411d7..5f5a02e4 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -2077,8 +2077,6 @@ impl Niri { use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; - use crate::utils::add_screenshot_filename; - let _span = tracy_client::span!("Niri::screenshot_all_outputs"); let mut elements = vec![]; @@ -2103,15 +2101,13 @@ impl Niri { let pixels = render_to_vec(renderer, size, Scale::from(1.), Fourcc::Abgr8888, &elements)?; let path = make_screenshot_path(&self.config.borrow()) - .and_then(|path| match path { - Some(path) => Ok(path), - None => { - let mut path = env::temp_dir(); - add_screenshot_filename(&mut path)?; - Ok(path) - } - }) - .context("error making screenshot path")?; + .ok() + .flatten() + .unwrap_or_else(|| { + let mut path = env::temp_dir(); + path.push("screenshot.png"); + path + }); debug!("saving screenshot to {path:?}"); thread::spawn(move || { diff --git a/src/utils.rs b/src/utils.rs index 92b6509c..15917f85 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use std::ffi::OsStr; +use std::ffi::{CString, OsStr}; use std::io::{self, Write}; use std::os::unix::prelude::OsStrExt; use std::os::unix::process::CommandExt; @@ -24,23 +24,14 @@ pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> { } pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> { - let Some(mut path) = config.screenshot_path.clone() else { + let Some(path) = &config.screenshot_path else { return Ok(None); }; - if let Ok(rest) = path.strip_prefix("~") { - let dirs = UserDirs::new().context("error retrieving home directory")?; - path = [dirs.home_dir(), rest].iter().collect(); - } + let format = CString::new(path.clone()).context("path must not contain nul bytes")?; - add_screenshot_filename(&mut path)?; - - Ok(Some(path)) -} - -pub fn add_screenshot_filename(path: &mut PathBuf) -> anyhow::Result<()> { - let mut buf = [0u8; 256]; - let name; + let mut buf = [0u8; 2048]; + let mut path; unsafe { let time = libc::time(null_mut()); ensure!(time != -1, "error in time()"); @@ -48,21 +39,18 @@ pub fn add_screenshot_filename(path: &mut PathBuf) -> anyhow::Result<()> { let tm = libc::localtime(&time); ensure!(!tm.is_null(), "error in localtime()"); - let format = b"Screenshot from %Y-%m-%d %H-%M-%S.png\0"; - let rv = libc::strftime( - buf.as_mut_ptr().cast(), - buf.len(), - format.as_ptr().cast(), - tm, - ); + let rv = libc::strftime(buf.as_mut_ptr().cast(), buf.len(), format.as_ptr(), tm); ensure!(rv != 0, "error formatting time"); - name = OsStr::from_bytes(&buf[..rv]); + path = PathBuf::from(OsStr::from_bytes(&buf[..rv])); } - path.push(name); + if let Ok(rest) = path.strip_prefix("~") { + let dirs = UserDirs::new().context("error retrieving home directory")?; + path = [dirs.home_dir(), rest].iter().collect(); + } - Ok(()) + Ok(Some(path)) } /// Spawns the command to run independently of the compositor. |
