mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 11:45:50 +02:00
Move Value to helpers, separate span call (#10121)
# Description As part of the refactor to split spans off of Value, this moves to using helper functions to create values, and using `.span()` instead of matching span out of Value directly. Hoping to get a few more helping hands to finish this, as there are a lot of commands to update :) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
@ -86,53 +86,35 @@ impl Command for Fill {
|
||||
description:
|
||||
"Fill a string on the left side to a width of 15 with the character '─'",
|
||||
example: "'nushell' | fill -a l -c '─' -w 15",
|
||||
result: Some(Value::String {
|
||||
val: "nushell────────".into(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("nushell────────", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description:
|
||||
"Fill a string on the right side to a width of 15 with the character '─'",
|
||||
example: "'nushell' | fill -a r -c '─' -w 15",
|
||||
result: Some(Value::String {
|
||||
val: "────────nushell".into(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("────────nushell", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Fill a string on both sides to a width of 15 with the character '─'",
|
||||
example: "'nushell' | fill -a m -c '─' -w 15",
|
||||
result: Some(Value::String {
|
||||
val: "────nushell────".into(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("────nushell────", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description:
|
||||
"Fill a number on the left side to a width of 5 with the character '0'",
|
||||
example: "1 | fill --alignment right --character '0' --width 5",
|
||||
result: Some(Value::String {
|
||||
val: "00001".into(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("00001", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Fill a number on both sides to a width of 5 with the character '0'",
|
||||
example: "1.1 | fill --alignment center --character '0' --width 5",
|
||||
result: Some(Value::String {
|
||||
val: "01.10".into(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("01.10", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description:
|
||||
"Fill a filesize on the left side to a width of 5 with the character '0'",
|
||||
example: "1kib | fill --alignment middle --character '0' --width 10",
|
||||
result: Some(Value::String {
|
||||
val: "0001024000".into(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("0001024000", Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -198,15 +180,15 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
Value::String { val, .. } => fill_string(val, args, span),
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
Value::Error { .. } => input.clone(),
|
||||
other => Value::Error {
|
||||
error: Box::new(ShellError::OnlySupportsThisInputType {
|
||||
other => Value::error(
|
||||
ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "int, filesize, float, string".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: other.span(),
|
||||
}),
|
||||
},
|
||||
span,
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@ -214,18 +196,18 @@ fn fill_float(num: f64, args: &Arguments, span: Span) -> Value {
|
||||
let s = num.to_string();
|
||||
let out_str = pad(&s, args.width, &args.character, args.alignment, false);
|
||||
|
||||
Value::String { val: out_str, span }
|
||||
Value::string(out_str, span)
|
||||
}
|
||||
fn fill_int(num: i64, args: &Arguments, span: Span) -> Value {
|
||||
let s = num.to_string();
|
||||
let out_str = pad(&s, args.width, &args.character, args.alignment, false);
|
||||
|
||||
Value::String { val: out_str, span }
|
||||
Value::string(out_str, span)
|
||||
}
|
||||
fn fill_string(s: &str, args: &Arguments, span: Span) -> Value {
|
||||
let out_str = pad(s, args.width, &args.character, args.alignment, false);
|
||||
|
||||
Value::String { val: out_str, span }
|
||||
Value::string(out_str, span)
|
||||
}
|
||||
|
||||
fn pad(s: &str, width: usize, pad_char: &str, alignment: FillAlignment, truncate: bool) -> String {
|
||||
|
Reference in New Issue
Block a user