2022-09-25 22:49:00 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
2022-10-16 22:16:48 +01:00
|
|
|
use crate::script::exec_command;
|
|
|
|
use color_eyre::Result;
|
2022-08-14 14:30:13 +01:00
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::Label;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use tokio::spawn;
|
2022-09-25 22:49:00 +01:00
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
2022-08-14 14:30:13 +01:00
|
|
|
use tokio::time::sleep;
|
2022-10-16 22:16:48 +01:00
|
|
|
use tracing::error;
|
2022-08-14 14:30:13 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct ScriptModule {
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Path to script to execute.
|
2022-08-14 14:30:13 +01:00
|
|
|
path: String,
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Time in milliseconds between executions.
|
2022-08-14 14:30:13 +01:00
|
|
|
#[serde(default = "default_interval")]
|
|
|
|
interval: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 5000ms
|
|
|
|
const fn default_interval() -> u64 {
|
|
|
|
5000
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module<Label> for ScriptModule {
|
2022-09-25 22:49:00 +01:00
|
|
|
type SendMessage = String;
|
|
|
|
type ReceiveMessage = ();
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
|
|
|
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
|
|
|
|
_rx: Receiver<Self::ReceiveMessage>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let interval = self.interval;
|
|
|
|
let path = self.path.clone();
|
2022-08-14 14:30:13 +01:00
|
|
|
spawn(async move {
|
|
|
|
loop {
|
2022-10-16 22:16:48 +01:00
|
|
|
match exec_command(&path) {
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(stdout) => tx
|
|
|
|
.send(ModuleUpdateEvent::Update(stdout))
|
|
|
|
.await
|
|
|
|
.expect("Failed to send stdout"),
|
2022-08-21 23:36:07 +01:00
|
|
|
Err(err) => error!("{:?}", err),
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
sleep(tokio::time::Duration::from_millis(interval)).await;
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2022-10-15 16:27:25 +01:00
|
|
|
info: &ModuleInfo,
|
2022-09-25 22:49:00 +01:00
|
|
|
) -> Result<ModuleWidget<Label>> {
|
|
|
|
let label = Label::builder().use_markup(true).build();
|
2022-10-15 16:27:25 +01:00
|
|
|
label.set_angle(info.bar_position.get_angle());
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2022-08-14 14:30:13 +01:00
|
|
|
{
|
|
|
|
let label = label.clone();
|
2022-09-25 22:49:00 +01:00
|
|
|
context.widget_rx.attach(None, move |s| {
|
2022-10-16 12:56:39 +01:00
|
|
|
label.set_markup(s.as_str());
|
2022-08-14 14:30:13 +01:00
|
|
|
Continue(true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(ModuleWidget {
|
|
|
|
widget: label,
|
|
|
|
popup: None,
|
|
|
|
})
|
2022-08-21 23:36:07 +01:00
|
|
|
}
|
|
|
|
}
|