mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-24 22:24:53 +02:00
add better enum for states
This commit is contained in:
parent
0c161ae77e
commit
87ca73a2fd
@ -13,6 +13,8 @@ use crate::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::temperature::TemperatureReading;
|
||||||
|
|
||||||
pub static NVML_DATA: OnceLock<Result<Nvml, NvmlError>> = OnceLock::new();
|
pub static NVML_DATA: OnceLock<Result<Nvml, NvmlError>> = OnceLock::new();
|
||||||
|
|
||||||
pub struct GpusData {
|
pub struct GpusData {
|
||||||
@ -57,7 +59,7 @@ pub fn get_nvidia_vecs(
|
|||||||
|
|
||||||
temp_vec.push(TempHarvest {
|
temp_vec.push(TempHarvest {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
temperature: Some(temperature),
|
temperature: TemperatureReading::Value(temperature),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,18 @@ cfg_if::cfg_if! {
|
|||||||
|
|
||||||
use crate::app::filter::Filter;
|
use crate::app::filter::Filter;
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub enum TemperatureReading {
|
||||||
|
Value(f32),
|
||||||
|
#[default]
|
||||||
|
Unavailable,
|
||||||
|
Off,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct TempHarvest {
|
pub struct TempHarvest {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub temperature: Option<f32>,
|
pub temperature: TemperatureReading,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)]
|
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)]
|
||||||
|
@ -3,16 +3,36 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
sync::OnceLock,
|
||||||
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use hashbrown::{HashMap, HashSet};
|
use hashbrown::{HashMap, HashSet};
|
||||||
|
use humantime::Duration;
|
||||||
|
|
||||||
use super::{is_temp_filtered, TempHarvest, TemperatureType};
|
use super::{is_temp_filtered, TempHarvest, TemperatureReading, TemperatureType};
|
||||||
use crate::{app::filter::Filter, utils::error::BottomError};
|
use crate::{app::filter::Filter, utils::error::BottomError};
|
||||||
|
|
||||||
const EMPTY_NAME: &str = "Unknown";
|
const EMPTY_NAME: &str = "Unknown";
|
||||||
|
|
||||||
|
/// Holds some data about the power states of certain devices.
|
||||||
|
struct PowerStateInfo {
|
||||||
|
autosuspend: Option<Duration>,
|
||||||
|
last_read: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PowerStateInfo {
|
||||||
|
fn new(autosuspend: Option<Duration>) -> Self {
|
||||||
|
Self {
|
||||||
|
autosuspend,
|
||||||
|
last_read: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static POWER_STATE_MAP: OnceLock<HashMap<String, PowerStateInfo>> = OnceLock::new();
|
||||||
|
|
||||||
/// Returned results from grabbing hwmon/coretemp temperature sensor values/names.
|
/// Returned results from grabbing hwmon/coretemp temperature sensor values/names.
|
||||||
struct HwmonResults {
|
struct HwmonResults {
|
||||||
temperatures: Vec<TempHarvest>,
|
temperatures: Vec<TempHarvest>,
|
||||||
@ -241,7 +261,7 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H
|
|||||||
let name = finalize_name(None, None, &sensor_name, &mut seen_names);
|
let name = finalize_name(None, None, &sensor_name, &mut seen_names);
|
||||||
temperatures.push(TempHarvest {
|
temperatures.push(TempHarvest {
|
||||||
name,
|
name,
|
||||||
temperature: None,
|
temperature: TemperatureReading::Off,
|
||||||
});
|
});
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@ -321,7 +341,9 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H
|
|||||||
if let Ok(temp_celsius) = parse_temp(&temp_path) {
|
if let Ok(temp_celsius) = parse_temp(&temp_path) {
|
||||||
temperatures.push(TempHarvest {
|
temperatures.push(TempHarvest {
|
||||||
name,
|
name,
|
||||||
temperature: Some(temp_type.convert_temp_unit(temp_celsius)),
|
temperature: TemperatureReading::Value(
|
||||||
|
temp_type.convert_temp_unit(temp_celsius),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -373,7 +395,9 @@ fn add_thermal_zone_temperatures(
|
|||||||
|
|
||||||
temperatures.push(TempHarvest {
|
temperatures.push(TempHarvest {
|
||||||
name,
|
name,
|
||||||
temperature: Some(temp_type.convert_temp_unit(temp_celsius)),
|
temperature: TemperatureReading::Value(
|
||||||
|
temp_type.convert_temp_unit(temp_celsius),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use super::{is_temp_filtered, TempHarvest, TemperatureType};
|
use super::{is_temp_filtered, TempHarvest, TemperatureReading, TemperatureType};
|
||||||
use crate::app::filter::Filter;
|
use crate::app::filter::Filter;
|
||||||
|
|
||||||
pub fn get_temperature_data(
|
pub fn get_temperature_data(
|
||||||
@ -19,7 +19,9 @@ pub fn get_temperature_data(
|
|||||||
if is_temp_filtered(filter, &name) {
|
if is_temp_filtered(filter, &name) {
|
||||||
temperature_vec.push(TempHarvest {
|
temperature_vec.push(TempHarvest {
|
||||||
name,
|
name,
|
||||||
temperature: Some(temp_type.convert_temp_unit(component.temperature())),
|
temperature: TemperatureReading::Value(
|
||||||
|
temp_type.convert_temp_unit(component.temperature()),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,7 +38,7 @@ pub fn get_temperature_data(
|
|||||||
if let Some(temp) = temp.as_temperature() {
|
if let Some(temp) = temp.as_temperature() {
|
||||||
temperature_vec.push(TempHarvest {
|
temperature_vec.push(TempHarvest {
|
||||||
name,
|
name,
|
||||||
temperature: Some(match temp_type {
|
temperature: TemperatureReading::Value(match temp_type {
|
||||||
TemperatureType::Celsius => temp.celsius(),
|
TemperatureType::Celsius => temp.celsius(),
|
||||||
TemperatureType::Kelvin => temp.kelvin(),
|
TemperatureType::Kelvin => temp.kelvin(),
|
||||||
TemperatureType::Fahrenheit => temp.fahrenheit(),
|
TemperatureType::Fahrenheit => temp.fahrenheit(),
|
||||||
|
@ -8,9 +8,13 @@ use kstring::KString;
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::{data_farmer::DataCollection, AxisScaling},
|
app::{data_farmer::DataCollection, AxisScaling},
|
||||||
canvas::components::time_chart::Point,
|
canvas::components::time_chart::Point,
|
||||||
data_collection::{cpu::CpuDataType, memory::MemHarvest, temperature::TemperatureType},
|
data_collection::{
|
||||||
|
cpu::CpuDataType,
|
||||||
|
memory::MemHarvest,
|
||||||
|
temperature::{TemperatureReading, TemperatureType},
|
||||||
|
},
|
||||||
utils::{data_prefixes::*, data_units::DataUnit, general::*},
|
utils::{data_prefixes::*, data_units::DataUnit, general::*},
|
||||||
widgets::{DiskWidgetData, TempWidgetData},
|
widgets::{DiskWidgetData, TempWidgetData, TempWidgetReading},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@ -130,7 +134,11 @@ impl ConvertedData {
|
|||||||
data.temp_harvest.iter().for_each(|temp_harvest| {
|
data.temp_harvest.iter().for_each(|temp_harvest| {
|
||||||
self.temp_data.push(TempWidgetData {
|
self.temp_data.push(TempWidgetData {
|
||||||
sensor: KString::from_ref(&temp_harvest.name),
|
sensor: KString::from_ref(&temp_harvest.name),
|
||||||
temperature_value: temp_harvest.temperature.map(|temp| temp.ceil() as u64),
|
temperature_value: match temp_harvest.temperature {
|
||||||
|
TemperatureReading::Value(val) => TempWidgetReading::Value(val.ceil() as u32),
|
||||||
|
TemperatureReading::Unavailable => TempWidgetReading::Unavailable,
|
||||||
|
TemperatureReading::Off => TempWidgetReading::Off,
|
||||||
|
},
|
||||||
temperature_type,
|
temperature_type,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
use std::{borrow::Cow, cmp::max};
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
cmp::{max, Ordering},
|
||||||
|
};
|
||||||
|
|
||||||
use concat_string::concat_string;
|
use concat_string::concat_string;
|
||||||
use kstring::KString;
|
use kstring::KString;
|
||||||
@ -17,10 +20,34 @@ use crate::{
|
|||||||
utils::general::{sort_partial_fn, truncate_to_text},
|
utils::general::{sort_partial_fn, truncate_to_text},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum TempWidgetReading {
|
||||||
|
Value(u32),
|
||||||
|
#[default]
|
||||||
|
Unavailable,
|
||||||
|
Off,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for TempWidgetReading {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
match (self, other) {
|
||||||
|
(TempWidgetReading::Value(a), TempWidgetReading::Value(b)) => a.partial_cmp(b),
|
||||||
|
(TempWidgetReading::Value(_), _) => Some(Ordering::Greater),
|
||||||
|
(_, TempWidgetReading::Value(_)) => Some(Ordering::Less),
|
||||||
|
(TempWidgetReading::Unavailable, TempWidgetReading::Unavailable) => {
|
||||||
|
Some(Ordering::Equal)
|
||||||
|
}
|
||||||
|
(TempWidgetReading::Unavailable, TempWidgetReading::Off) => Some(Ordering::Greater),
|
||||||
|
(TempWidgetReading::Off, TempWidgetReading::Unavailable) => Some(Ordering::Less),
|
||||||
|
(TempWidgetReading::Off, TempWidgetReading::Off) => Some(Ordering::Equal),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TempWidgetData {
|
pub struct TempWidgetData {
|
||||||
pub sensor: KString,
|
pub sensor: KString,
|
||||||
pub temperature_value: Option<u64>,
|
pub temperature_value: TempWidgetReading,
|
||||||
pub temperature_type: TemperatureType,
|
pub temperature_type: TemperatureType,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,15 +68,16 @@ impl ColumnHeader for TempWidgetColumn {
|
|||||||
impl TempWidgetData {
|
impl TempWidgetData {
|
||||||
pub fn temperature(&self) -> KString {
|
pub fn temperature(&self) -> KString {
|
||||||
match self.temperature_value {
|
match self.temperature_value {
|
||||||
Some(temp_val) => {
|
TempWidgetReading::Value(val) => {
|
||||||
let temp_type = match self.temperature_type {
|
let temp_type = match self.temperature_type {
|
||||||
TemperatureType::Celsius => "°C",
|
TemperatureType::Celsius => "°C",
|
||||||
TemperatureType::Kelvin => "K",
|
TemperatureType::Kelvin => "K",
|
||||||
TemperatureType::Fahrenheit => "°F",
|
TemperatureType::Fahrenheit => "°F",
|
||||||
};
|
};
|
||||||
concat_string!(temp_val.to_string(), temp_type).into()
|
concat_string!(val.to_string(), temp_type).into()
|
||||||
}
|
}
|
||||||
None => "N/A".to_string().into(),
|
TempWidgetReading::Unavailable => "N/A".to_string().into(),
|
||||||
|
TempWidgetReading::Off => "Off".to_string().into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user