-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"embargo", | ||
"embargo_widget", | ||
"hyprland_workspaces", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::time::{Duration, Instant}; | ||
use sysinfo::{CpuExt, NetworkExt, SystemExt}; | ||
|
||
use sysinfo::System; | ||
|
||
pub struct HardwareMonitor { | ||
system: System, | ||
modules: Vec<HardwareModule>, | ||
interface_name: String, | ||
network_refresh_frequency: Duration, | ||
} | ||
|
||
impl HardwareMonitor { | ||
pub fn new(interface_name: String) -> Self { | ||
let network_refresh_frequency = Duration::from_secs(2); | ||
let modules = vec![ | ||
HardwareModule::new(Duration::from_millis(500), Box::new(System::refresh_cpu)), | ||
HardwareModule::new( | ||
Duration::from_millis(1000), | ||
Box::new(System::refresh_memory), | ||
), | ||
HardwareModule::new( | ||
network_refresh_frequency, | ||
Box::new(System::refresh_networks), | ||
), | ||
HardwareModule::new( | ||
Duration::from_secs(10), | ||
Box::new(System::refresh_networks_list), | ||
), | ||
]; | ||
Self { | ||
system: System::new(), | ||
modules, | ||
network_refresh_frequency, | ||
interface_name, | ||
} | ||
} | ||
pub fn update(&mut self) { | ||
for module in &mut self.modules { | ||
module.update(&mut self.system); | ||
} | ||
} | ||
pub fn cpu_usage(&self) -> f32 { | ||
self.system.global_cpu_info().cpu_usage() | ||
} | ||
pub fn used_mem(&self) -> u64 { | ||
self.system.used_memory() | ||
} | ||
pub fn total_mem(&self) -> u64 { | ||
self.system.total_memory() | ||
} | ||
pub fn uploaded_bytes(&self) -> u64 { | ||
match self | ||
.system | ||
.networks() | ||
.into_iter() | ||
.find(|(name, _)| **name == self.interface_name) | ||
.map(|(_, network)| network.transmitted()) | ||
{ | ||
Some(bytes) => bytes / self.network_refresh_frequency.as_secs(), | ||
None => { | ||
tracing::warn!("unable to locate interface '{}'", self.interface_name); | ||
0 | ||
} | ||
} | ||
} | ||
pub fn downloaded_bytes(&self) -> u64 { | ||
match self | ||
.system | ||
.networks() | ||
.into_iter() | ||
.find(|(name, _)| **name == self.interface_name) | ||
.map(|(_, network)| network.received()) | ||
{ | ||
Some(bytes) => bytes / self.network_refresh_frequency.as_secs(), | ||
None => { | ||
tracing::warn!("unable to locate interface '{}'", self.interface_name); | ||
0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct HardwareModule { | ||
last_update: Instant, | ||
frequency: Duration, | ||
refresh: Box<dyn Fn(&mut System)>, | ||
} | ||
|
||
impl HardwareModule { | ||
pub fn new(frequency: Duration, refresh: Box<dyn Fn(&mut System)>) -> Self { | ||
Self { | ||
last_update: Instant::now() - frequency, | ||
frequency, | ||
refresh, | ||
} | ||
} | ||
pub fn update(&mut self, system: &mut System) { | ||
if self.last_update.elapsed() > self.frequency { | ||
self.last_update = Instant::now(); | ||
(self.refresh)(system); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "embargo_widget" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
///trait that allows a struct to function as a slint widget | ||
pub trait Widget { | ||
///update internal state. It is recomended to maintain an internal timer that limits the frequency of updates | ||
fn update(&mut self) -> Result<(), Box<dyn std::error::Error>>; | ||
///the value to be displaye by the widget | ||
fn value_str(&self) -> &str; | ||
} |