# Description This doesn't really do much that the user could see, but it helps get us ready to do the steps of the refactor to split the span off of Value, so that values can be spanless. This allows us to have top-level values that can hold both a Value and a Span, without requiring that all values have them. We expect to see significant memory reduction by removing so many unnecessary spans from values. For example, a table of 100,000 rows and 5 columns would have a savings of ~8megs in just spans that are almost always duplicated. # User-Facing Changes Nothing yet # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
144 lines
4.5 KiB
Rust
144 lines
4.5 KiB
Rust
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::{Call, CellPath},
|
|
engine::{Command, EngineState, Stack},
|
|
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Fmt;
|
|
|
|
impl Command for Fmt {
|
|
fn name(&self) -> &str {
|
|
"fmt"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Format a number."
|
|
}
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
Signature::build("fmt")
|
|
.input_output_types(vec![(Type::Number, Type::Record(vec![]))])
|
|
.category(Category::Conversions)
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["display", "render", "format"]
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Get a record containing multiple formats for the number 42",
|
|
example: "42 | fmt",
|
|
result: Some(Value::test_record(Record {
|
|
cols: vec![
|
|
"binary".into(),
|
|
"debug".into(),
|
|
"display".into(),
|
|
"lowerexp".into(),
|
|
"lowerhex".into(),
|
|
"octal".into(),
|
|
"upperexp".into(),
|
|
"upperhex".into(),
|
|
],
|
|
vals: vec![
|
|
Value::test_string("0b101010"),
|
|
Value::test_string("42"),
|
|
Value::test_string("42"),
|
|
Value::test_string("4.2e1"),
|
|
Value::test_string("0x2a"),
|
|
Value::test_string("0o52"),
|
|
Value::test_string("4.2E1"),
|
|
Value::test_string("0x2A"),
|
|
],
|
|
})),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
fmt(engine_state, stack, call, input)
|
|
}
|
|
}
|
|
|
|
fn fmt(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
|
let args = CellPathOnlyArgs::from(cell_paths);
|
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
|
}
|
|
|
|
fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
|
match input {
|
|
Value::Float { val, .. } => fmt_it_64(*val, span),
|
|
Value::Int { val, .. } => fmt_it(*val, span),
|
|
Value::Filesize { val, .. } => fmt_it(*val, span),
|
|
// Propagate errors by explicitly matching them before the final case.
|
|
Value::Error { .. } => input.clone(),
|
|
other => Value::Error {
|
|
error: Box::new(ShellError::OnlySupportsThisInputType {
|
|
exp_input_type: "float , integer or filesize".into(),
|
|
wrong_type: other.get_type().to_string(),
|
|
dst_span: span,
|
|
src_span: other.span(),
|
|
}),
|
|
span,
|
|
},
|
|
}
|
|
}
|
|
|
|
fn fmt_it(num: i64, span: Span) -> Value {
|
|
Value::record(
|
|
record! {
|
|
"binary" => Value::string(format!("{num:#b}"), span),
|
|
"debug" => Value::string(format!("{num:#?}"), span),
|
|
"display" => Value::string(format!("{num}"), span),
|
|
"lowerexp" => Value::string(format!("{num:#e}"), span),
|
|
"lowerhex" => Value::string(format!("{num:#x}"), span),
|
|
"octal" => Value::string(format!("{num:#o}"), span),
|
|
"upperexp" => Value::string(format!("{num:#E}"), span),
|
|
"upperhex" => Value::string(format!("{num:#X}"), span),
|
|
},
|
|
span,
|
|
)
|
|
}
|
|
|
|
fn fmt_it_64(num: f64, span: Span) -> Value {
|
|
Value::record(
|
|
record! {
|
|
"binary" => Value::string(format!("{:b}", num.to_bits()), span),
|
|
"debug" => Value::string(format!("{num:#?}"), span),
|
|
"display" => Value::string(format!("{num}"), span),
|
|
"lowerexp" => Value::string(format!("{num:#e}"), span),
|
|
"lowerhex" => Value::string(format!("{:0x}", num.to_bits()), span),
|
|
"octal" => Value::string(format!("{:0o}", num.to_bits()), span),
|
|
"upperexp" => Value::string(format!("{num:#E}"), span),
|
|
"upperhex" => Value::string(format!("{:0X}", num.to_bits()), span),
|
|
},
|
|
span,
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(Fmt {})
|
|
}
|
|
}
|