nu_command/system/sys/
temp.rs

1use nu_engine::command_prelude::*;
2use sysinfo::Components;
3
4#[derive(Clone)]
5pub struct SysTemp;
6
7impl Command for SysTemp {
8    fn name(&self) -> &str {
9        "sys temp"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("sys temp")
14            .filter()
15            .category(Category::System)
16            .input_output_types(vec![(Type::Nothing, Type::table())])
17    }
18
19    fn description(&self) -> &str {
20        "View the temperatures of system components."
21    }
22
23    fn extra_description(&self) -> &str {
24        "Some system components do not support temperature readings, so this command may return an empty list if no components support temperature."
25    }
26
27    fn run(
28        &self,
29        _engine_state: &EngineState,
30        _stack: &mut Stack,
31        call: &Call,
32        _input: PipelineData,
33    ) -> Result<PipelineData, ShellError> {
34        Ok(temp(call.head).into_pipeline_data())
35    }
36
37    fn examples(&self) -> Vec<Example> {
38        vec![Example {
39            description: "Show the system temperatures",
40            example: "sys temp",
41            result: None,
42        }]
43    }
44}
45
46fn temp(span: Span) -> Value {
47    let components = Components::new_with_refreshed_list()
48        .iter()
49        .map(|component| {
50            let mut record = record! {
51                "unit" => Value::string(component.label(), span),
52                "temp" => Value::float(component.temperature().unwrap_or(f32::NAN).into(), span),
53                "high" => Value::float(component.max().unwrap_or(f32::NAN).into(), span),
54            };
55
56            if let Some(critical) = component.critical() {
57                record.push("critical", Value::float(critical.into(), span));
58            }
59
60            Value::record(record, span)
61        })
62        .collect();
63
64    Value::list(components, span)
65}