Include filename in screenshot-path

This commit is contained in:
Ivan Molodetskikh
2023-10-31 14:23:54 +04:00
parent 02f6b418fe
commit b0af1129c9
4 changed files with 31 additions and 40 deletions
+2 -1
View File
@@ -112,7 +112,8 @@ gaps 16
// You can change the path where screenshots are saved.
// A ~ at the front will be expanded to the home directory.
screenshot-path "~/Pictures/Screenshots"
// The path is formatted with strftime(3) to give you the screenshot date and time.
screenshot-path "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png"
// You can also set this to null to disable saving screenshots to disk.
// screenshot-path null
+10 -4
View File
@@ -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 {
+7 -11
View File
@@ -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 || {
+12 -24
View File
@@ -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.