forge
Explore
Log in

Don't you dare go Hollow

Include filename in screenshot-path

Browse files at this commit

b0af1129c91a21e29da15f9c5ea6a6dd32c1d85dIvan MolodetskikhParent: 02f6b41
resources/default-config.kdlmodified+2 -1
@@ -112,7 +112,8 @@ gaps 16
112 112
113 // You can change the path where screenshots are saved.113 // You can change the path where screenshots are saved.
114 // A ~ at the front will be expanded to the home directory.114 // A ~ at the front will be expanded to the home directory.
115-screenshot-path "~/Pictures/Screenshots"115+// The path is formatted with strftime(3) to give you the screenshot date and time.
116+screenshot-path "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png"
116 117
117 // You can also set this to null to disable saving screenshots to disk.118 // You can also set this to null to disable saving screenshots to disk.
118 // screenshot-path null119 // screenshot-path null
src/config.rsmodified+10 -4
@@ -26,8 +26,14 @@ pub struct Config {
26 pub preset_column_widths: Vec<PresetWidth>,26 pub preset_column_widths: Vec<PresetWidth>,
27 #[knuffel(child, unwrap(argument), default = 16)]27 #[knuffel(child, unwrap(argument), default = 16)]
28 pub gaps: u16,28 pub gaps: u16,
29- #[knuffel(child, unwrap(argument), default = Some(PathBuf::from("~/Pictures/Screenshots")))]29+ #[knuffel(
30- pub screenshot_path: Option<PathBuf>,30+ child,
31+ unwrap(argument),
32+ default = Some(String::from(
33+ "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png"
34+ )))
35+ ]
36+ pub screenshot_path: Option<String>,
31 #[knuffel(child, default)]37 #[knuffel(child, default)]
32 pub binds: Binds,38 pub binds: Binds,
33 #[knuffel(child, default)]39 #[knuffel(child, default)]
@@ -541,7 +547,7 @@ mod tests {
541 547
542 gaps 8548 gaps 8
543 549
544- screenshot-path "~/Screenshots"550+ screenshot-path "~/Screenshots/screenshot.png"
545 551
546 binds {552 binds {
547 Mod+T { spawn "alacritty"; }553 Mod+T { spawn "alacritty"; }
@@ -617,7 +623,7 @@ mod tests {
617 PresetWidth::Fixed(1280),623 PresetWidth::Fixed(1280),
618 ],624 ],
619 gaps: 8,625 gaps: 8,
620- screenshot_path: Some(PathBuf::from("~/Screenshots")),626+ screenshot_path: Some(String::from("~/Screenshots/screenshot.png")),
621 binds: Binds(vec![627 binds: Binds(vec![
622 Bind {628 Bind {
623 key: Key {629 key: Key {
src/niri.rsmodified+7 -11
@@ -2077,8 +2077,6 @@ impl Niri {
2077 2077
2078 use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement};2078 use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement};
2079 2079
2080- use crate::utils::add_screenshot_filename;
2081-
2082 let _span = tracy_client::span!("Niri::screenshot_all_outputs");2080 let _span = tracy_client::span!("Niri::screenshot_all_outputs");
2083 2081
2084 let mut elements = vec![];2082 let mut elements = vec![];
@@ -2103,15 +2101,13 @@ impl Niri {
2103 let pixels = render_to_vec(renderer, size, Scale::from(1.), Fourcc::Abgr8888, &elements)?;2101 let pixels = render_to_vec(renderer, size, Scale::from(1.), Fourcc::Abgr8888, &elements)?;
2104 2102
2105 let path = make_screenshot_path(&self.config.borrow())2103 let path = make_screenshot_path(&self.config.borrow())
2106- .and_then(|path| match path {2104+ .ok()
2107- Some(path) => Ok(path),2105+ .flatten()
2108- None => {2106+ .unwrap_or_else(|| {
2109- let mut path = env::temp_dir();2107+ let mut path = env::temp_dir();
2110- add_screenshot_filename(&mut path)?;2108+ path.push("screenshot.png");
2111- Ok(path)2109+ path
2112- }2110+ });
2113- })
2114- .context("error making screenshot path")?;
2115 debug!("saving screenshot to {path:?}");2111 debug!("saving screenshot to {path:?}");
2116 2112
2117 thread::spawn(move || {2113 thread::spawn(move || {
src/utils.rsmodified+12 -24
@@ -1,4 +1,4 @@
1-use std::ffi::OsStr;1+use std::ffi::{CString, OsStr};
2 use std::io::{self, Write};2 use std::io::{self, Write};
3 use std::os::unix::prelude::OsStrExt;3 use std::os::unix::prelude::OsStrExt;
4 use std::os::unix::process::CommandExt;4 use std::os::unix::process::CommandExt;
@@ -24,23 +24,14 @@ pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> {
24 }24 }
25 25
26 pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {26 pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
27- let Some(mut path) = config.screenshot_path.clone() else {27+ let Some(path) = &config.screenshot_path else {
28 return Ok(None);28 return Ok(None);
29 };29 };
30 30
31- if let Ok(rest) = path.strip_prefix("~") {31+ let format = CString::new(path.clone()).context("path must not contain nul bytes")?;
32- let dirs = UserDirs::new().context("error retrieving home directory")?;
33- path = [dirs.home_dir(), rest].iter().collect();
34- }
35 32
36- add_screenshot_filename(&mut path)?;33+ let mut buf = [0u8; 2048];
37-34+ let mut path;
38- Ok(Some(path))
39-}
40-
41-pub fn add_screenshot_filename(path: &mut PathBuf) -> anyhow::Result<()> {
42- let mut buf = [0u8; 256];
43- let name;
44 unsafe {35 unsafe {
45 let time = libc::time(null_mut());36 let time = libc::time(null_mut());
46 ensure!(time != -1, "error in time()");37 ensure!(time != -1, "error in time()");
@@ -48,21 +39,18 @@ pub fn add_screenshot_filename(path: &mut PathBuf) -> anyhow::Result<()> {
48 let tm = libc::localtime(&time);39 let tm = libc::localtime(&time);
49 ensure!(!tm.is_null(), "error in localtime()");40 ensure!(!tm.is_null(), "error in localtime()");
50 41
51- let format = b"Screenshot from %Y-%m-%d %H-%M-%S.png\0";42+ let rv = libc::strftime(buf.as_mut_ptr().cast(), buf.len(), format.as_ptr(), tm);
52- let rv = libc::strftime(
53- buf.as_mut_ptr().cast(),
54- buf.len(),
55- format.as_ptr().cast(),
56- tm,
57- );
58 ensure!(rv != 0, "error formatting time");43 ensure!(rv != 0, "error formatting time");
59 44
60- name = OsStr::from_bytes(&buf[..rv]);45+ path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));
61 }46 }
62 47
63- path.push(name);48+ if let Ok(rest) = path.strip_prefix("~") {
49+ let dirs = UserDirs::new().context("error retrieving home directory")?;
50+ path = [dirs.home_dir(), rest].iter().collect();
51+ }
64 52
65- Ok(())53+ Ok(Some(path))
66 }54 }
67 55
68 /// Spawns the command to run independently of the compositor.56 /// Spawns the command to run independently of the compositor.