nu_command/system/sys/
disks.rs

1use super::trim_cstyle_null;
2use nu_engine::command_prelude::*;
3use sysinfo::Disks;
4
5#[derive(Clone)]
6pub struct SysDisks;
7
8impl Command for SysDisks {
9    fn name(&self) -> &str {
10        "sys disks"
11    }
12
13    fn signature(&self) -> Signature {
14        Signature::build("sys disks")
15            .filter()
16            .category(Category::System)
17            .input_output_types(vec![(Type::Nothing, Type::table())])
18    }
19
20    fn description(&self) -> &str {
21        "View information about the system disks."
22    }
23
24    fn run(
25        &self,
26        _engine_state: &EngineState,
27        _stack: &mut Stack,
28        call: &Call,
29        _input: PipelineData,
30    ) -> Result<PipelineData, ShellError> {
31        Ok(disks(call.head).into_pipeline_data())
32    }
33
34    fn examples(&self) -> Vec<Example> {
35        vec![Example {
36            description: "Show info about the system disks",
37            example: "sys disks",
38            result: None,
39        }]
40    }
41}
42
43fn disks(span: Span) -> Value {
44    let disks = Disks::new_with_refreshed_list()
45        .iter()
46        .map(|disk| {
47            let device = trim_cstyle_null(disk.name().to_string_lossy());
48            let typ = trim_cstyle_null(disk.file_system().to_string_lossy());
49
50            let record = record! {
51                "device" => Value::string(device, span),
52                "type" => Value::string(typ, span),
53                "mount" => Value::string(disk.mount_point().to_string_lossy(), span),
54                "total" => Value::filesize(disk.total_space() as i64, span),
55                "free" => Value::filesize(disk.available_space() as i64, span),
56                "removable" => Value::bool(disk.is_removable(), span),
57                "kind" => Value::string(disk.kind().to_string(), span),
58            };
59
60            Value::record(record, span)
61        })
62        .collect();
63
64    Value::list(disks, span)
65}