Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solve ratio problem #12

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn run(
let mut gpu_list = crate::gpu::try_init_gpus(&nvml, lh)?;

let mut selected_gpu: usize = 0;
let mut have_fans: bool = true;

loop {
_ = terminal.draw(|f| {
Expand All @@ -44,8 +45,12 @@ pub fn run(
.constraints([Constraint::Min(0), Constraint::Length(1)])
.split(f.size());

#[cfg(target_os = "linux")]
f.render_widget(Paragraph::new("q to quit, p to rescan devices"), layout[1]);

#[cfg(target_os = "windows")]
f.render_widget(Paragraph::new("q to quit"), layout[1]);

layout[0]
} else {
let layout = Layout::default()
Expand Down Expand Up @@ -138,9 +143,15 @@ pub fn run(
let temp_gauge = draw_gpu_die_temp(gpu);
f.render_widget(temp_gauge, chunks[1]);

// Fanspeed:
let fan_gauge = draw_fan_speed(gpu);
f.render_widget(fan_gauge, chunks[2]);
// Fan speed:
if have_fans {
match draw_fan_speed(gpu) {
Ok(gauge) => {
f.render_widget(gauge, chunks[2]);
}
Err(_) => have_fans = false,
alphastrata marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
})?;

Expand All @@ -155,6 +166,7 @@ pub fn run(
}
KeyCode::Char('p') => {
// re-scan pci tree to let driver discover new devices (only works as sudo)
#[cfg(target_os = "linux")]
alphastrata marked this conversation as resolved.
Show resolved Hide resolved
match nvml.discover_gpus(PciInfo {
bus: 0,
bus_id: "".into(),
Expand Down Expand Up @@ -191,24 +203,28 @@ pub fn run(
Ok(())
}

fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> {
fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Result<Gauge<'d>, ()> {
let temps = gpu.inner.num_fans().map_or(0, |fc| fc);
let avg = (0..temps as usize)
.flat_map(|v| gpu.inner.fan_speed(v as u32))
.map(|u| u as f64)
.sum::<f64>()
/ temps as f64;

if avg.is_nan() {
return Err(());
}

let percentage = (avg / 100.).clamp(0., 1.0);
let label = format!("{:.1}%", avg);
let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black));

Gauge::default()
Ok(Gauge::default()
.block(Block::default().borders(Borders::ALL).title("Fan Speed"))
.gauge_style(calculate_severity(percentage).style_for())
.label(spanned_label)
.set_style(Style::default())
.ratio(percentage)
.ratio(percentage))
}

fn draw_gpu_die_temp<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> {
Expand All @@ -219,7 +235,7 @@ fn draw_gpu_die_temp<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> {

let label = format!("{:.2}°C", gpu_die_temperature);
let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black));
let temp_ratio = (gpu_die_temperature as f64 / 100.).clamp(0., 1.0);
let temp_ratio = (gpu_die_temperature as f64 / 100.).clamp(0.0, 1.0);

Gauge::default()
.block(Block::default().borders(Borders::ALL).title("Temp"))
Expand Down