# Description While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` constructors, which seem designed to make it easier to construct various Values, but which aren't used often at all in the codebase. So, using a few find-replaces regexes, I increased their usage. This reduces overall LOC because structures like this: ``` Value::Int { val: a, span: head } ``` are changed into ``` Value::int(a, head) ``` and are respected as such by the project's formatter. There are little readability concerns because the second argument to all of these is `span`, and it's almost always extremely obvious which is the span at every callsite. # User-Facing Changes None. # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # 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.
141 lines
4.0 KiB
Rust
141 lines
4.0 KiB
Rust
use crate::input_handler::{operate, CellPathOnlyArgs};
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::{Call, CellPath},
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Example, PipelineData, 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::Record {
|
|
cols: vec![
|
|
"binary".into(),
|
|
"debug".into(),
|
|
"display".into(),
|
|
"lowerexp".into(),
|
|
"lowerhex".into(),
|
|
"octal".into(),
|
|
"upperexp".into(),
|
|
"upperhex".into(),
|
|
],
|
|
vals: vec![
|
|
Value::string("0b101010", Span::test_data()),
|
|
Value::string("42", Span::test_data()),
|
|
Value::string("42", Span::test_data()),
|
|
Value::string("4.2e1", Span::test_data()),
|
|
Value::string("0x2a", Span::test_data()),
|
|
Value::string("0o52", Span::test_data()),
|
|
Value::string("4.2E1", Span::test_data()),
|
|
Value::string("0x2A", Span::test_data()),
|
|
],
|
|
span: Span::test_data(),
|
|
}),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
|
fmt(engine_state, stack, call, input)
|
|
}
|
|
}
|
|
|
|
fn fmt(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::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::Int { val, .. } => fmt_it(*val, span),
|
|
Value::Filesize { val, .. } => fmt_it(*val, span),
|
|
_ => Value::Error {
|
|
error: ShellError::UnsupportedInput(
|
|
format!("unsupported input type: {:?}", input.get_type()),
|
|
span,
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
fn fmt_it(num: i64, span: Span) -> Value {
|
|
let mut cols = vec![];
|
|
let mut vals = vec![];
|
|
|
|
cols.push("binary".into());
|
|
vals.push(Value::string(format!("{:#b}", num), span));
|
|
|
|
cols.push("debug".into());
|
|
vals.push(Value::string(format!("{:#?}", num), span));
|
|
|
|
cols.push("display".into());
|
|
vals.push(Value::string(format!("{}", num), span));
|
|
|
|
cols.push("lowerexp".into());
|
|
vals.push(Value::string(format!("{:#e}", num), span));
|
|
|
|
cols.push("lowerhex".into());
|
|
vals.push(Value::string(format!("{:#x}", num), span));
|
|
|
|
cols.push("octal".into());
|
|
vals.push(Value::string(format!("{:#o}", num), span));
|
|
|
|
// cols.push("pointer".into());
|
|
// vals.push(Value::string(format!("{:#p}", &num), span));
|
|
|
|
cols.push("upperexp".into());
|
|
vals.push(Value::string(format!("{:#E}", num), span));
|
|
|
|
cols.push("upperhex".into());
|
|
vals.push(Value::string(format!("{:#X}", num), span));
|
|
|
|
Value::Record { cols, vals, span }
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(Fmt {})
|
|
}
|
|
}
|