mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-03 11:41:04 +02:00
chore: initial commit
This commit is contained in:
commit
e37d8f2b14
36 changed files with 4948 additions and 0 deletions
51
src/modules/script.rs
Normal file
51
src/modules/script.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use crate::modules::{Module, ModuleInfo};
|
||||
use gtk::prelude::*;
|
||||
use gtk::Label;
|
||||
use serde::Deserialize;
|
||||
use std::process::Command;
|
||||
use tokio::spawn;
|
||||
use tokio::time::sleep;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ScriptModule {
|
||||
path: String,
|
||||
#[serde(default = "default_interval")]
|
||||
interval: u64,
|
||||
}
|
||||
|
||||
/// 5000ms
|
||||
const fn default_interval() -> u64 {
|
||||
5000
|
||||
}
|
||||
|
||||
impl Module<Label> for ScriptModule {
|
||||
fn into_widget(self, _info: &ModuleInfo) -> Label {
|
||||
let label = Label::new(None);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
sleep(tokio::time::Duration::from_millis(self.interval)).await;
|
||||
}
|
||||
});
|
||||
|
||||
{
|
||||
let label = label.clone();
|
||||
rx.attach(None, move |s| {
|
||||
label.set_label(s.as_str());
|
||||
Continue(true)
|
||||
});
|
||||
}
|
||||
|
||||
label
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue