|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use openaction::*; |
| 4 | + |
| 5 | +use sysinfo::System; |
| 6 | + |
| 7 | +struct CPUAction; |
| 8 | +#[async_trait] |
| 9 | +impl Action for CPUAction { |
| 10 | + const UUID: ActionUuid = "me.amankhanna.oasystem.cpu"; |
| 11 | + type Settings = HashMap<String, String>; |
| 12 | +} |
| 13 | + |
| 14 | +struct RAMAction; |
| 15 | +#[async_trait] |
| 16 | +impl Action for RAMAction { |
| 17 | + const UUID: ActionUuid = "me.amankhanna.oasystem.ram"; |
| 18 | + type Settings = HashMap<String, String>; |
| 19 | +} |
| 20 | + |
| 21 | +struct UptimeAction; |
| 22 | +#[async_trait] |
| 23 | +impl Action for UptimeAction { |
| 24 | + const UUID: ActionUuid = "me.amankhanna.oasystem.uptime"; |
| 25 | + type Settings = HashMap<String, String>; |
| 26 | +} |
| 27 | + |
| 28 | +struct OSAction; |
| 29 | +#[async_trait] |
| 30 | +impl Action for OSAction { |
| 31 | + const UUID: ActionUuid = "me.amankhanna.oasystem.os"; |
| 32 | + type Settings = HashMap<String, String>; |
| 33 | + |
| 34 | + async fn will_appear( |
| 35 | + &self, |
| 36 | + instance: &Instance, |
| 37 | + _settings: &Self::Settings, |
| 38 | + ) -> OpenActionResult<()> { |
| 39 | + instance |
| 40 | + .set_title( |
| 41 | + Some( |
| 42 | + System::long_os_version() |
| 43 | + .unwrap_or_else(|| "Unknown".to_owned()) |
| 44 | + .replace(" ", "\n"), |
| 45 | + ), |
| 46 | + None, |
| 47 | + ) |
| 48 | + .await |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[tokio::main] |
| 53 | +async fn main() -> OpenActionResult<()> { |
| 54 | + { |
| 55 | + use simplelog::*; |
| 56 | + if let Err(error) = TermLogger::init( |
| 57 | + LevelFilter::Debug, |
| 58 | + Config::default(), |
| 59 | + TerminalMode::Stdout, |
| 60 | + ColorChoice::Never, |
| 61 | + ) { |
| 62 | + eprintln!("Logger initialization failed: {}", error); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + tokio::spawn(async { |
| 67 | + let mut system = System::new_all(); |
| 68 | + loop { |
| 69 | + system.refresh_cpu_usage(); |
| 70 | + tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 71 | + system.refresh_cpu_usage(); |
| 72 | + |
| 73 | + let cpu_usage = format!("{:.0}%", system.global_cpu_usage()); |
| 74 | + for instance in visible_instances(CPUAction::UUID).await { |
| 75 | + let _ = instance.set_title(Some(cpu_usage.clone()), None).await; |
| 76 | + } |
| 77 | + |
| 78 | + system.refresh_memory(); |
| 79 | + let ram_usage = format!("{:.1}GB", (system.used_memory() as f32) / 1073741824.0); |
| 80 | + for instance in visible_instances(RAMAction::UUID).await { |
| 81 | + let _ = instance.set_title(Some(ram_usage.clone()), None).await; |
| 82 | + } |
| 83 | + |
| 84 | + { |
| 85 | + let total_secs = System::uptime(); |
| 86 | + let days = total_secs / 86_400; |
| 87 | + let hours = (total_secs % 86_400) / 3600; |
| 88 | + let minutes = (total_secs % 3600) / 60; |
| 89 | + let seconds = total_secs % 60; |
| 90 | + |
| 91 | + for instance in visible_instances(UptimeAction::UUID).await { |
| 92 | + let _ = instance |
| 93 | + .set_title( |
| 94 | + Some(format!("{days}d {hours:02}h\n{minutes:02}m {seconds:02}s")), |
| 95 | + None, |
| 96 | + ) |
| 97 | + .await; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + register_action(CPUAction).await; |
| 104 | + register_action(RAMAction).await; |
| 105 | + register_action(UptimeAction).await; |
| 106 | + register_action(OSAction).await; |
| 107 | + |
| 108 | + run(std::env::args().collect()).await |
| 109 | +} |
0 commit comments