39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
mod core;
|
||
|
||
use chrono::Local;
|
||
use core::{XContext, capture_root_image, save_png};
|
||
use std::fs;
|
||
|
||
fn main() -> anyhow::Result<()> {
|
||
// Establish connection to X11 and gather screen info.
|
||
let ctx = XContext::new()?;
|
||
|
||
// Capture the entire screen as an RGBA8 buffer.
|
||
let img = capture_root_image(&ctx)?;
|
||
|
||
// Build an output path:
|
||
// ~/Pictures/Screenshots/screenshot_YYYYMMDDHHMMSS.png
|
||
//
|
||
// Using timestamps avoids overwriting previous screenshots,
|
||
// and matches the behavior of many modern screenshot tools.
|
||
let timestamp = Local::now().format("%Y%m%d%H%M%S").to_string();
|
||
|
||
let mut path = dirs::home_dir().unwrap_or_else(|| ".".into());
|
||
path.push("Pictures");
|
||
path.push("Screenshots");
|
||
|
||
// Ensure the directory exists
|
||
fs::create_dir_all(&path)?;
|
||
|
||
path.push(format!("screenshot_{timestamp}.png"));
|
||
|
||
// Convert PathBuf → &str (safe because it’s UTF-8 on all Linux systems).
|
||
let path_str = path.to_string_lossy();
|
||
|
||
// Write the PNG file to disk.
|
||
save_png(&path_str, ctx.width, ctx.height, &img)?;
|
||
|
||
println!("Saved {path_str}");
|
||
Ok(())
|
||
}
|