mirror of
https://github.com/nushell/nushell.git
synced 2024-12-21 22:53:00 +01:00
add term size
command (#792)
* add `term-size` command * Update term_size.rs Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
parent
ac07d93b02
commit
057bfff0cb
@ -172,6 +172,7 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
|
|||||||
Input,
|
Input,
|
||||||
Kill,
|
Kill,
|
||||||
Sleep,
|
Sleep,
|
||||||
|
TermSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Date
|
// Date
|
||||||
|
@ -3,9 +3,11 @@ mod clear;
|
|||||||
mod input;
|
mod input;
|
||||||
mod kill;
|
mod kill;
|
||||||
mod sleep;
|
mod sleep;
|
||||||
|
mod term_size;
|
||||||
|
|
||||||
pub use ansi::{Ansi, AnsiGradient, AnsiStrip};
|
pub use ansi::{Ansi, AnsiGradient, AnsiStrip};
|
||||||
pub use clear::Clear;
|
pub use clear::Clear;
|
||||||
pub use input::Input;
|
pub use input::Input;
|
||||||
pub use kill::Kill;
|
pub use kill::Kill;
|
||||||
pub use sleep::Sleep;
|
pub use sleep::Sleep;
|
||||||
|
pub use term_size::TermSize;
|
||||||
|
113
crates/nu-command/src/platform/term_size.rs
Normal file
113
crates/nu-command/src/platform/term_size.rs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, Signature, Span, Value};
|
||||||
|
use terminal_size::{terminal_size, Height, Width};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TermSize;
|
||||||
|
|
||||||
|
impl Command for TermSize {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"term size"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Returns the terminal size"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("term size")
|
||||||
|
.switch(
|
||||||
|
"columns",
|
||||||
|
"Report only the width of the terminal",
|
||||||
|
Some('c'),
|
||||||
|
)
|
||||||
|
.switch("rows", "Report only the height of the terminal", Some('r'))
|
||||||
|
.category(Category::Platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (columns) of the terminal",
|
||||||
|
example: "term size -c",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Return the height (rows) of the terminal",
|
||||||
|
example: "term size -r",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
_engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let wide = call.has_flag("columns");
|
||||||
|
let tall = call.has_flag("rows");
|
||||||
|
|
||||||
|
let (cols, rows) = match terminal_size() {
|
||||||
|
Some((w, h)) => (Width(w.0), Height(h.0)),
|
||||||
|
None => (Width(0), Height(0)),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((match (wide, tall) {
|
||||||
|
(true, false) => Value::Record {
|
||||||
|
cols: vec!["columns".into()],
|
||||||
|
vals: vec![Value::Int {
|
||||||
|
val: cols.0 as i64,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}],
|
||||||
|
span: head,
|
||||||
|
},
|
||||||
|
(true, true) => Value::Record {
|
||||||
|
cols: vec!["columns".into(), "rows".into()],
|
||||||
|
vals: vec![
|
||||||
|
Value::Int {
|
||||||
|
val: cols.0 as i64,
|
||||||
|
span: Span::test_data(),
|
||||||
|
},
|
||||||
|
Value::Int {
|
||||||
|
val: rows.0 as i64,
|
||||||
|
span: Span::test_data(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
span: head,
|
||||||
|
},
|
||||||
|
(false, true) => Value::Record {
|
||||||
|
cols: vec!["rows".into()],
|
||||||
|
vals: vec![Value::Int {
|
||||||
|
val: rows.0 as i64,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}],
|
||||||
|
span: head,
|
||||||
|
},
|
||||||
|
(false, false) => Value::Record {
|
||||||
|
cols: vec!["columns".into(), "rows".into()],
|
||||||
|
vals: vec![
|
||||||
|
Value::Int {
|
||||||
|
val: cols.0 as i64,
|
||||||
|
span: Span::test_data(),
|
||||||
|
},
|
||||||
|
Value::Int {
|
||||||
|
val: rows.0 as i64,
|
||||||
|
span: Span::test_data(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
span: head,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.into_pipeline_data())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user