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([("columns".into(), Type::Int), ("rows".into(), Type::Int)].into()),
22            )])
23    }
24
25    fn examples(&self) -> Vec<Example<'_>> {
26        vec![
27            Example {
28                description: "Return the columns (width) and rows (height) of the terminal",
29                example: "term size",
30                result: None,
31            },
32            Example {
33                description: "Return the columns (width) of the terminal",
34                example: "(term size).columns",
35                result: None,
36            },
37            Example {
38                description: "Return the rows (height) of the terminal",
39                example: "(term size).rows",
40                result: None,
41            },
42        ]
43    }
44
45    fn run(
46        &self,
47        _engine_state: &EngineState,
48        _stack: &mut Stack,
49        call: &Call,
50        _input: PipelineData,
51    ) -> Result<PipelineData, ShellError> {
52        let head = call.head;
53
54        let (cols, rows) = size().unwrap_or((0, 0));
55
56        Ok(Value::record(
57            record! {
58                "columns" => Value::int(cols as i64, head),
59                "rows" => Value::int(rows as i64, head),
60            },
61            head,
62        )
63        .into_pipeline_data())
64    }
65}