Skip to content

Commit 3aa36b2

Browse files
Send notifications for success and failures
1 parent f0cd585 commit 3aa36b2

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/main.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
mod error;
2+
mod localize;
3+
14
use std::{collections::HashMap, fs, os::unix::fs::MetadataExt, path::PathBuf};
25

3-
use ashpd::desktop::screenshot::Screenshot;
6+
use ashpd::desktop::screenshot::{Screenshot, ScreenshotRequest};
47
use clap::{ArgAction, Parser, command};
58
use tracing::{error, info};
69
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
@@ -9,13 +12,9 @@ use zbus::{
912
export::futures_util::{TryFutureExt, future::FutureExt},
1013
zvariant::Value,
1114
};
12-
use zbus::{Connection, dbus_proxy, zvariant::Value};
1315

1416
use error::Error;
1517

16-
mod error;
17-
mod localize;
18-
1918
#[derive(Parser, Default, Debug, Clone, PartialEq, Eq)]
2019
#[command(version, about, long_about = None)]
2120
struct Args {
@@ -88,15 +87,7 @@ async fn send_notify(summary: &str, body: &str) -> Result<(), Error> {
8887
.map(|_| ())
8988
}
9089

91-
#[tokio::main(flavor = "current_thread")]
92-
async fn main() -> Result<(), Error> {
93-
tracing_subscriber::registry()
94-
.with(fmt::layer())
95-
.with(EnvFilter::from_default_env())
96-
.init();
97-
crate::localize::localize();
98-
99-
let args = Args::parse();
90+
async fn request_screenshot(args: Args) -> Result<String, Error> {
10091
let picture_dir = (!args.interactive)
10192
.then(|| {
10293
args.save_dir
@@ -115,7 +106,7 @@ async fn main() -> Result<(), Error> {
115106
.response()?;
116107

117108
let uri = response.uri();
118-
let path = match uri.scheme() {
109+
match uri.scheme() {
119110
"file" => {
120111
let response_path = uri
121112
.to_file_path()
@@ -152,29 +143,50 @@ async fn main() -> Result<(), Error> {
152143
context: "moving screenshot",
153144
})?;
154145
}
155-
156-
path.to_string_lossy().to_string()
146+
Ok(path.to_string_lossy().to_string())
157147
} else {
158-
response_path.to_string_lossy().to_string()
148+
Ok(uri.path().to_string())
159149
}
160150
}
161-
"clipboard" => String::new(),
151+
"clipboard" => Ok(String::new()),
162152
scheme => {
163153
error!("Unsupported URL scheme: {scheme}");
164-
return Err(Error::Ashpd(ashpd::Error::Zbus(zbus::Error::Unsupported)));
154+
Err(Error::Ashpd(ashpd::Error::Zbus(zbus::Error::Unsupported)))
165155
}
166-
};
156+
}
157+
}
158+
159+
#[tokio::main(flavor = "current_thread")]
160+
async fn main() -> Result<(), Error> {
161+
// Init tracing but don't panic if it fails
162+
let _ = tracing_subscriber::registry()
163+
.with(fmt::layer())
164+
.with(EnvFilter::from_default_env())
165+
.try_init();
166+
crate::localize::localize();
167+
168+
let args = Args::parse();
169+
let notify = args.notify;
167170

168-
info!("Saving screenshot to {path}");
171+
let path = match request_screenshot(args).await {
172+
Ok(path) => {
173+
info!("Saving screenshot to {path}");
174+
path
175+
}
176+
Err(e) => {
177+
error!("Screenshot failed with {e}");
178+
e.to_user_facing()
179+
}
180+
};
169181

170-
if args.notify {
182+
if notify {
171183
let message = if path.is_empty() {
172184
fl!("screenshot-saved-to-clipboard")
173185
} else {
174186
fl!("screenshot-saved-to")
175187
};
176188

177-
send_notify(&message, &path).await?;
189+
send_notify(&path, &message).await?;
178190
}
179191

180192
Ok(())

0 commit comments

Comments
 (0)