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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::prelude::*;
use indexmap::IndexMap;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Dictionary, Signature, UntaggedValue};
pub struct TermSize;
#[derive(Deserialize, Clone)]
pub struct TermSizeArgs {
wide: bool,
tall: bool,
}
impl WholeStreamCommand for TermSize {
fn name(&self) -> &str {
"term size"
}
fn signature(&self) -> Signature {
Signature::build("term size")
.switch("wide", "Report only the width of the terminal", Some('w'))
.switch("tall", "Report only the height of the terminal", Some('t'))
}
fn usage(&self) -> &str {
"Returns the terminal size as W H"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let wide = args.has_flag("wide");
let tall = args.has_flag("tall");
let size = term_size::dimensions();
match size {
Some((w, h)) => {
if wide && !tall {
Ok(ActionStream::one(
UntaggedValue::int(w as i64).into_value(tag),
))
} else if !wide && tall {
Ok(ActionStream::one(
UntaggedValue::int(h as i64).into_value(tag),
))
} else {
let mut indexmap = IndexMap::with_capacity(2);
indexmap.insert(
"width".to_string(),
UntaggedValue::int(w as i64).into_value(&tag),
);
indexmap.insert(
"height".to_string(),
UntaggedValue::int(h as i64).into_value(&tag),
);
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
Ok(ActionStream::one(value))
}
}
_ => Ok(ActionStream::one(
UntaggedValue::string("0 0".to_string()).into_value(tag),
)),
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Return the width height of the terminal",
example: "term size",
result: None,
},
Example {
description: "Return the width of the terminal",
example: "term size -w",
result: None,
},
Example {
description: "Return the height (t for tall) of the terminal",
example: "term size -t",
result: None,
},
]
}
}