mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-03 11:41:04 +02:00
feat: logging support and proper error handling
This commit is contained in:
parent
917838c98c
commit
ab8f7ecfc8
28 changed files with 1056 additions and 388 deletions
|
@ -1,10 +1,12 @@
|
|||
use crate::modules::{Module, ModuleInfo};
|
||||
use color_eyre::{eyre::Report, eyre::Result, eyre::WrapErr, Section};
|
||||
use gtk::prelude::*;
|
||||
use gtk::Label;
|
||||
use serde::Deserialize;
|
||||
use std::process::Command;
|
||||
use tokio::spawn;
|
||||
use tokio::time::sleep;
|
||||
use tracing::{error, instrument};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ScriptModule {
|
||||
|
@ -19,19 +21,15 @@ const fn default_interval() -> u64 {
|
|||
}
|
||||
|
||||
impl Module<Label> for ScriptModule {
|
||||
fn into_widget(self, _info: &ModuleInfo) -> Label {
|
||||
fn into_widget(self, _info: &ModuleInfo) -> Result<Label> {
|
||||
let label = Label::builder().use_markup(true).build();
|
||||
|
||||
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
||||
spawn(async move {
|
||||
loop {
|
||||
let output = Command::new("sh").arg("-c").arg(&self.path).output();
|
||||
if let Ok(output) = output {
|
||||
let stdout = String::from_utf8(output.stdout)
|
||||
.map(|output| output.trim().to_string())
|
||||
.expect("Script output not valid UTF-8");
|
||||
|
||||
tx.send(stdout).unwrap();
|
||||
match self.run_script() {
|
||||
Ok(stdout) => tx.send(stdout).expect("Failed to send stdout"),
|
||||
Err(err) => error!("{:?}", err),
|
||||
}
|
||||
|
||||
sleep(tokio::time::Duration::from_millis(self.interval)).await;
|
||||
|
@ -46,6 +44,34 @@ impl Module<Label> for ScriptModule {
|
|||
});
|
||||
}
|
||||
|
||||
label
|
||||
Ok(label)
|
||||
}
|
||||
}
|
||||
|
||||
impl ScriptModule {
|
||||
#[instrument]
|
||||
fn run_script(&self) -> Result<String> {
|
||||
let output = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(&self.path)
|
||||
.output()
|
||||
.wrap_err("Failed to get script output")?;
|
||||
|
||||
if output.status.success() {
|
||||
let stdout = String::from_utf8(output.stdout)
|
||||
.map(|output| output.trim().to_string())
|
||||
.wrap_err("Script stdout not valid UTF-8")?;
|
||||
|
||||
Ok(stdout)
|
||||
} else {
|
||||
let stderr = String::from_utf8(output.stderr)
|
||||
.map(|output| output.trim().to_string())
|
||||
.wrap_err("Script stderr not valid UTF-8")?;
|
||||
|
||||
Err(Report::msg(stderr)
|
||||
.wrap_err("Script returned non-zero error code")
|
||||
.suggestion("Check the path to your script")
|
||||
.suggestion("Check the script for errors"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue