-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscreenshot
More file actions
executable file
·118 lines (96 loc) · 2.54 KB
/
screenshot
File metadata and controls
executable file
·118 lines (96 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env scriptisto
// vim: shiftwidth=2 softtabstop=2
// scriptisto-begin
// script_src: src/main.rs
// build_cmd: >
// cargo clippy --color=always &&
// cargo build --release --color=always &&
// strip ./target/release/screenshot
// target_bin: ./target/release/screenshot
// files:
// - path: Cargo.toml
// content: |
// package = { name = "screenshot", version = "0.1.0", edition = "2024"}
// [dependencies]
// scopeguard = "1.2.0"
// scriptisto-end
#![allow(dead_code)]
#![deny(warnings)]
#![deny(clippy::unwrap_used)]
use scopeguard::defer;
use std::process::{
Command,
Stdio,
};
use std::error::Error;
use std::time::SystemTime;
fn init_screenshot(filename: &str) -> Result<bool, Box<dyn Error>> {
let coords = {
let output = Command::new("slurp")
.args(["-c", "#FA7265AF", "-s", "#00000030", "-b", "#00000030"])
.stdout(Stdio::piped())
.spawn()?
.wait_with_output()?;
String::from_utf8_lossy(&output.stdout).to_string()
};
if coords.is_empty() {
return Ok(false);
}
Command::new("grim")
.arg("-g")
.arg(coords.to_string().trim())
.arg(filename)
.spawn()?
.wait()
.map(drop)?;
Ok(true)
}
fn annotate_screenshot(path: &str) -> Result<(), Box<dyn Error>> {
Ok(
Command::new("satty")
.arg("-f")
.arg(path)
.arg("-o")
.arg(path)
.arg("--fullscreen")
.arg("--disable-notifications")
.spawn()?
.wait()
.map(drop)?,
)
}
fn main() {
let lock_file_path = ".screenshot.lock";
if std::fs::exists(lock_file_path).unwrap_or(false) {
println!(
"Screenshot already in progress, exiting. delete {} to force",
lock_file_path
);
std::process::exit(0);
}
std::fs::write(lock_file_path, "").expect("Failed to write lock file");
defer! {
std::fs::remove_file(lock_file_path).ok();
};
let screenshots_dir = std::env::args().nth(1).expect("No screenshot directory specified");
let time = SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("Failed to get time");
let path = format!("{}/{}.png", screenshots_dir, time.as_secs());
let done = init_screenshot(&path).expect("Failed to initialize screenshot");
if !done {
return;
}
if std::env::args().any(|arg| arg == "--annotate") {
annotate_screenshot(&path).expect("Failed to annotate screenshot");
} else {
let content = std::fs::File::open(&path).expect("Failed to open screenshot");
let mut cmd = Command::new("wl-copy");
let child = cmd.stdin(Stdio::from(content));
child
.spawn()
.expect("Could not spawn wl-copy")
.wait()
.expect("Failed to copy screenshot");
}
}