Skip to main content

nu_command/platform/term/
term_size.rs

1use crossterm::terminal::size;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct TermSize;
6
7impl Command for TermSize {
8    fn name(&self) -> &str {
9        "term size"
10    }
11
12    fn description(&self) -> &str {
13        "Returns a record containing the number of columns (width) and rows (height) of the terminal."
14    }
15
16    fn signature(&self) -> Signature {
17        Signature::build("term size")
18            .category(Category::Platform)
19            .input_output_types(vec![(
20                Type::Nothing,
21                Type::Record(
22                    vec![("columns".into(), Type::Int), ("rows".into(), Type::Int)].into(),
23                ),
24            )])
25    }
26
27    fn examples(&self) -> Vec<Example<'_>> {
28        vec![
29            Example {
30                description: "Return the columns (width) and rows (height) of the terminal.",
31                example: "term size",
32                result: None,
33            },
34            Example {
35                description: "Return the columns (width) of the terminal.",
36                example: "(term size).columns",
37                result: None,
38            },
39            Example {
40                description: "Return the rows (height) of the terminal.",
41                example: "(term size).rows",
42                result: None,
43            },
44        ]
45    }
46
47    fn run(
48        &self,
49        _engine_state: &EngineState,
50        _stack: &mut Stack,
51        call: &Call,
52        _input: PipelineData,
53    ) -> Result<PipelineData, ShellError> {
54        let head = call.head;
55
56        let (cols, rows) = size().unwrap_or((0, 0));
57
58        Ok(Value::record(
59            record! {
60                "columns" => Value::int(cols as i64, head),
61                "rows" => Value::int(rows as i64, head),
62            },
63            head,
64        )
65        .into_pipeline_data())
66    }
67}