Skip to content

Commit ec7d80d

Browse files
Send notifications for success and failures
1 parent 16582a7 commit ec7d80d

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/main.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod error;
22

3-
use ashpd::desktop::screenshot::Screenshot;
3+
use ashpd::desktop::screenshot::{Screenshot, ScreenshotRequest};
44
use clap::{command, ArgAction, Parser};
55
use std::{collections::HashMap, fs, os::unix::fs::MetadataExt, path::PathBuf};
66
use tracing::{error, info};
@@ -85,14 +85,7 @@ async fn send_notify(summary: &str, body: &str) -> Result<(), Error> {
8585
.map(|_| ())
8686
}
8787

88-
#[tokio::main(flavor = "current_thread")]
89-
async fn main() -> Result<(), Error> {
90-
tracing_subscriber::registry()
91-
.with(fmt::layer())
92-
.with(EnvFilter::from_default_env())
93-
.init();
94-
95-
let args = Args::parse();
88+
async fn request_screenshot(args: Args) -> Result<String, Error> {
9689
let picture_dir = (!args.interactive)
9790
.then(|| {
9891
args.save_dir
@@ -111,7 +104,7 @@ async fn main() -> Result<(), Error> {
111104
.response()?;
112105

113106
let uri = response.uri();
114-
let path = match uri.scheme() {
107+
match uri.scheme() {
115108
"file" => {
116109
if let Some(picture_dir) = picture_dir {
117110
let date = chrono::Local::now();
@@ -147,21 +140,42 @@ async fn main() -> Result<(), Error> {
147140
})?;
148141
}
149142

150-
path.to_string_lossy().to_string()
143+
Ok(path.to_string_lossy().to_string())
151144
} else {
152-
uri.path().to_string()
145+
Ok(uri.path().to_string())
153146
}
154147
}
155148
scheme => {
156149
error!("Unsupported URL scheme: {scheme}");
157-
return Err(Error::Ashpd(ashpd::Error::Zbus(zbus::Error::Unsupported)));
150+
Err(Error::Ashpd(ashpd::Error::Zbus(zbus::Error::Unsupported)))
158151
}
159-
};
152+
}
153+
}
160154

161-
info!("Saving screenshot to {path}");
155+
#[tokio::main(flavor = "current_thread")]
156+
async fn main() -> Result<(), Error> {
157+
// Init tracing but don't panic if it fails
158+
let _ = tracing_subscriber::registry()
159+
.with(fmt::layer())
160+
.with(EnvFilter::from_default_env())
161+
.try_init();
162+
163+
let args = Args::parse();
164+
let notify = args.notify;
165+
166+
let result = match request_screenshot(args).await {
167+
Ok(path) => {
168+
info!("Saving screenshot to {path}");
169+
path
170+
}
171+
Err(e) => {
172+
error!("Screenshot failed with {e}");
173+
e.to_user_facing()
174+
}
175+
};
162176

163-
if args.notify {
164-
send_notify(&path, "").await?;
177+
if notify {
178+
send_notify(&result, "").await?;
165179
}
166180

167181
Ok(())

0 commit comments

Comments
 (0)