Skip to content

Commit

Permalink
Ensure CPU frequency gets dynamically updated
Browse files Browse the repository at this point in the history
- Fix state refresh logic for sysinfo system
- Let each module refresh happen when needed. This requires mutable
  System instance to be passed around though.
  • Loading branch information
crodjer committed Dec 22, 2023
1 parent 290075a commit 86d043b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ this program. If not, see <https://www.gnu.org/licenses/>.
use super::colors::colorize;
use super::config::Config;
use colored::*;
use sysinfo::{CpuExt, System, SystemExt};
use sysinfo::{CpuExt, CpuRefreshKind, System, SystemExt};

pub fn overview(config: &Config, system: &mut System) -> String {
system.refresh_cpu_specifics(CpuRefreshKind::everything());

pub fn overview(config: &Config, system: &System) -> String {
if config.frequency {
format!("{} {}", usage(config, system), frequency(system))
} else {
usage(config, system)
}
}

fn usage(config: &Config, system: &System) -> String {
fn usage(config: &Config, system: &mut System) -> String {
let usage = system.global_cpu_info().cpu_usage();

colorize(
Expand Down
9 changes: 4 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ struct State {
ping: ping::PingManager,
}

fn line(state: &State) -> String {
fn line(state: &mut State) -> String {
[
memory::usage(&state.config, &state.system),
cpu::overview(&state.config, &state.system),
sensors::temperature(&state.config, &state.system),
memory::usage(&state.config, &mut state.system),
cpu::overview(&state.config, &mut state.system),
sensors::temperature(&state.config, &mut state.system),
state.ping.current(),
]
.join(" ")
}

fn render_line(delimiter: char, state: &mut State) -> () {
state.system.refresh_all();
print!("{}{}", line(state), delimiter);
()
}
Expand Down
4 changes: 3 additions & 1 deletion src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use super::colors::colorize;
use super::config::Config;
use sysinfo::{System, SystemExt};

pub fn usage(config: &Config, system: &System) -> String {
pub fn usage(config: &Config, system: &mut System) -> String {
system.refresh_memory();

let total = system.total_memory() as f32;
let available = system.available_memory() as f32;
let usage = (100.0 * (total - available) / total).round();
Expand Down
5 changes: 3 additions & 2 deletions src/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ use super::colors::colorize;
use super::config::Config;
use sysinfo::{ComponentExt, System, SystemExt};

fn max_temperature(system: &System) -> f32 {
fn max_temperature(system: &mut System) -> f32 {
system.refresh_components();
system
.components()
.iter()
.rfold(0.0, |acc, x| acc.max(x.temperature()))
}

pub fn temperature(config: &Config, system: &System) -> String {
pub fn temperature(config: &Config, system: &mut System) -> String {
let temp = max_temperature(system);

colorize(
Expand Down

0 comments on commit 86d043b

Please sign in to comment.