Skip to content

Commit beb875d

Browse files
committed
Implement it in a cached and long running way
1 parent 25abcf9 commit beb875d

File tree

14 files changed

+431
-124
lines changed

14 files changed

+431
-124
lines changed

Cargo.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
rayon = "1.10.0"
87
modules = { path = "modules" }
8+
itertools = "0.14.0"

modules/src/colors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Color of a Module
2-
#[derive(Clone, Copy, Default)]
2+
#[derive(Clone, Copy, Default, Hash)]
33
pub enum Color {
44
Black,
55
White,
@@ -34,7 +34,7 @@ impl Color {
3434
}
3535
}
3636

37-
#[derive(Clone, Copy, Default)]
37+
#[derive(Clone, Copy, Default, Hash)]
3838
pub struct Style {
3939
pub fg: Color,
4040
pub bg: Color,

modules/src/icons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
// Those are only constructed in config.rs
4-
#[derive(Clone, Copy)]
4+
#[derive(Clone, Copy, Hash)]
55
pub enum Icon {
66
Manual(char),
77
Time,

modules/src/modules/battery.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ use crate::{
44
icons::Icon,
55
};
66
use battery::{units, Manager, State};
7-
use std::fmt;
7+
use std::{
8+
fmt,
9+
time::{Duration, Instant},
10+
};
811

9-
#[derive(Clone, Copy)]
1012
pub struct Battery {
11-
pub percentage: u8,
12-
pub is_charging: bool,
13+
percentage: u8,
14+
is_charging: bool,
1315
minimum_digits: usize,
16+
manager: Manager,
1417
}
1518

1619
impl fmt::Display for Battery {
@@ -38,11 +41,33 @@ impl ToModule for Battery {
3841
Some(Icon::Battery(perc))
3942
}
4043
}
44+
45+
fn update(&mut self) {
46+
let battery = self
47+
.manager
48+
.batteries()
49+
.ok()
50+
.map(|mut batteries| batteries.next())
51+
.flatten()
52+
.and_then(|b| b.ok());
53+
54+
if let Some(battery) = battery {
55+
self.percentage = battery.state_of_charge().get::<units::ratio::percent>() as u8;
56+
self.is_charging = battery.state() != State::Discharging;
57+
}
58+
}
59+
60+
fn next_render_time(&self) -> Option<Instant> {
61+
let percentage: f32 = self.percentage.into();
62+
let secs = percentage.sqrt();
63+
Some(Instant::now() + Duration::from_secs_f32(secs))
64+
}
4165
}
4266

4367
impl Battery {
4468
pub fn try_new(minimum_digits: usize) -> Result<Option<Self>, battery::errors::Error> {
45-
let mut batteries = Manager::new()?.batteries()?;
69+
let manager = Manager::new()?;
70+
let mut batteries = manager.batteries()?;
4671
let battery = match batteries.next() {
4772
Some(val) => val,
4873
None => return Ok(None),
@@ -56,6 +81,7 @@ impl Battery {
5681
percentage,
5782
is_charging,
5883
minimum_digits,
84+
manager,
5985
}))
6086
}
6187
}

modules/src/modules/cpu.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,30 @@ use crate::{
33
colors::{Color, Style},
44
icons::Icon,
55
};
6-
use std::{fmt, thread};
6+
use std::{
7+
fmt,
8+
time::{Duration, Instant},
9+
};
710
use sysinfo::{CpuRefreshKind, RefreshKind, System};
811

912
pub struct Cpu {
1013
usage: f32,
1114
minimum_digits: usize,
1215
decimal_places: usize,
16+
system: System,
1317
}
1418

1519
impl Cpu {
1620
pub fn new(minimum_digits: usize, decimal_places: usize) -> Self {
21+
let specifics = RefreshKind::new().with_cpu(CpuRefreshKind::everything());
22+
let system = System::new_with_specifics(specifics);
1723
Self {
18-
usage: Self::total_average(),
24+
usage: 0.0,
1925
minimum_digits,
2026
decimal_places,
27+
system,
2128
}
2229
}
23-
24-
fn total_average() -> f32 {
25-
let mut s =
26-
System::new_with_specifics(RefreshKind::new().with_cpu(CpuRefreshKind::everything()));
27-
28-
thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
29-
s.refresh_cpu();
30-
31-
let cpus = s.cpus();
32-
33-
let cpu_sum = cpus.iter().map(|cpu| cpu.cpu_usage()).sum::<f32>();
34-
35-
cpu_sum / cpus.len() as f32
36-
}
3730
}
3831

3932
impl fmt::Display for Cpu {
@@ -56,4 +49,18 @@ impl ToModule for Cpu {
5649
fn icon(&self) -> Option<Icon> {
5750
Some(Icon::Cpu)
5851
}
52+
53+
fn update(&mut self) {
54+
let s = &mut self.system;
55+
56+
s.refresh_cpu();
57+
58+
let cpus = s.cpus();
59+
let cpu_sum = cpus.iter().map(|cpu| cpu.cpu_usage()).sum::<f32>();
60+
self.usage = cpu_sum / cpus.len() as f32;
61+
}
62+
63+
fn next_render_time(&self) -> Option<Instant> {
64+
Some(Instant::now() + Duration::from_secs_f32(2.5))
65+
}
5966
}

modules/src/modules/datetime.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ use crate::{
44
icons::Icon,
55
};
66
use chrono::Local;
7-
use std::fmt;
7+
use std::{
8+
fmt,
9+
time::{Duration, Instant},
10+
};
811

12+
#[derive(Clone, Copy)]
913
pub struct DateTime<'a> {
1014
format: &'a str,
1115
}
@@ -37,4 +41,8 @@ impl ToModule for DateTime<'_> {
3741
fn style(&self) -> Style {
3842
Style::new_with_fg(Color::Magenta)
3943
}
44+
fn next_render_time(&self) -> Option<Instant> {
45+
// TODO: Calculate the actual needed instant
46+
Some(Instant::now() + Duration::from_secs(1))
47+
}
4048
}

modules/src/modules/memory.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,34 @@ use crate::{
33
colors::{Color, Style},
44
icons::Icon,
55
};
6-
use std::fmt;
6+
use std::{
7+
fmt,
8+
time::{Duration, Instant},
9+
};
710
use sysinfo::{MemoryRefreshKind, RefreshKind, System};
811

912
pub struct Memory {
1013
usage: f32,
1114
minimum_digits: usize,
1215
decimal_places: usize,
16+
system: System,
1317
}
1418

1519
impl Memory {
1620
pub fn new(minimum_digits: usize, decimal_places: usize) -> Self {
21+
let specifics = RefreshKind::new().with_memory(MemoryRefreshKind::everything());
22+
let mut system = System::new_with_specifics(specifics);
23+
system.refresh_memory();
24+
25+
let usage = (system.used_memory() as f32 / system.total_memory() as f32) * 100.0;
26+
1727
Self {
18-
usage: Self::total_usage(),
28+
usage,
1929
minimum_digits,
2030
decimal_places,
31+
system,
2132
}
2233
}
23-
24-
fn total_usage() -> f32 {
25-
let mut sys = System::new_with_specifics(
26-
RefreshKind::new().with_memory(MemoryRefreshKind::everything()),
27-
);
28-
29-
sys.refresh_memory();
30-
31-
(sys.used_memory() as f32 / sys.total_memory() as f32) * 100.0
32-
}
3334
}
3435

3536
impl fmt::Display for Memory {
@@ -52,4 +53,14 @@ impl ToModule for Memory {
5253
fn icon(&self) -> Option<Icon> {
5354
Some(Icon::DoubleServer)
5455
}
56+
57+
fn update(&mut self) {
58+
let system = &mut self.system;
59+
system.refresh_memory();
60+
self.usage = (system.used_memory() as f32 / system.total_memory() as f32) * 100.0;
61+
}
62+
63+
fn next_render_time(&self) -> Option<Instant> {
64+
Some(Instant::now() + Duration::from_secs_f32(7.5))
65+
}
5566
}

0 commit comments

Comments
 (0)