nu_command/system/sys/
temp.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use nu_engine::command_prelude::*;
use sysinfo::Components;

#[derive(Clone)]
pub struct SysTemp;

impl Command for SysTemp {
    fn name(&self) -> &str {
        "sys temp"
    }

    fn signature(&self) -> Signature {
        Signature::build("sys temp")
            .filter()
            .category(Category::System)
            .input_output_types(vec![(Type::Nothing, Type::table())])
    }

    fn description(&self) -> &str {
        "View the temperatures of system components."
    }

    fn extra_description(&self) -> &str {
        "Some system components do not support temperature readings, so this command may return an empty list if no components support temperature."
    }

    fn run(
        &self,
        _engine_state: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        Ok(temp(call.head).into_pipeline_data())
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description: "Show the system temperatures",
            example: "sys temp",
            result: None,
        }]
    }
}

fn temp(span: Span) -> Value {
    let components = Components::new_with_refreshed_list()
        .iter()
        .map(|component| {
            let mut record = record! {
                "unit" => Value::string(component.label(), span),
                "temp" => Value::float(component.temperature().into(), span),
                "high" => Value::float(component.max().into(), span),
            };

            if let Some(critical) = component.critical() {
                record.push("critical", Value::float(critical.into(), span));
            }

            Value::record(record, span)
        })
        .collect();

    Value::list(components, span)
}