nushell/crates/nu-command/src/platform/term_size.rs
Ian Manske 8da27a1a09
Create Record type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
   ```rust
   record! {
       "key1" => some_value,
       "key2" => Value::string("text", span),
       "key3" => Value::int(optional_int.unwrap_or(0), span),
       "key4" => Value::bool(config.setting, span),
   }
   ```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.

Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.

# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-25 07:50:29 +12:00

76 lines
2.1 KiB
Rust

use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, 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 a record containing the number of columns (width) and rows (height) of the terminal."
}
fn signature(&self) -> Signature {
Signature::build("term size")
.category(Category::Platform)
.input_output_types(vec![(
Type::Nothing,
Type::Record(vec![
("columns".into(), Type::Int),
("rows".into(), Type::Int),
]),
)])
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Return the columns (width) and rows (height) of the terminal",
example: "term size",
result: None,
},
Example {
description: "Return the columns (width) of the terminal",
example: "(term size).columns",
result: None,
},
Example {
description: "Return the rows (height) of the terminal",
example: "(term size).rows",
result: None,
},
]
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let (cols, rows) = match terminal_size() {
Some((w, h)) => (Width(w.0), Height(h.0)),
None => (Width(0), Height(0)),
};
Ok(Value::record(
record! {
"columns" => Value::int(cols.0 as i64, head),
"rows" => Value::int(rows.0 as i64, head),
},
head,
)
.into_pipeline_data())
}
}