43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
mod core;
|
|
|
|
use chrono::Local;
|
|
use core::{XContext, capture_rect, save_png};
|
|
use std::fs;
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
// Create a connection to the X11 server and read screen information.
|
|
let ctx = XContext::new()?;
|
|
|
|
// Capture the entire screen.
|
|
// We pass (0,0) as the top-left corner and use the screen width/height.
|
|
let img = capture_rect(&ctx, 0, 0, ctx.width, ctx.height)?;
|
|
|
|
// Build the output file path.
|
|
// Example:
|
|
// ~/Pictures/Screenshots/screenshot_20250116094530.png
|
|
//
|
|
// Using a timestamp prevents overwriting old screenshots.
|
|
let timestamp = Local::now().format("%Y%m%d%H%M%S").to_string();
|
|
|
|
// Start from the user's home directory (fallback: current dir).
|
|
let mut path = dirs::home_dir().unwrap_or_else(|| ".".into());
|
|
path.push("Pictures");
|
|
path.push("Screenshots");
|
|
|
|
// Make sure the directory exists.
|
|
fs::create_dir_all(&path)?;
|
|
|
|
// Add the actual filename.
|
|
path.push(format!("screenshot_{timestamp}.png"));
|
|
|
|
// Convert path to string for save_png().
|
|
let path_str = path.to_string_lossy();
|
|
|
|
// Write the captured RGBA image to a PNG file.
|
|
save_png(&path_str, ctx.width, ctx.height, &img)?;
|
|
|
|
println!("Saved {path_str}");
|
|
|
|
Ok(())
|
|
}
|