nushell/crates/nu-command/src/generators/seq_char.rs

151 lines
4.2 KiB
Rust
Raw Normal View History

use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct SeqChar;
impl Command for SeqChar {
fn name(&self) -> &str {
"seq char"
}
fn description(&self) -> &str {
"Print a sequence of ASCII characters."
}
fn signature(&self) -> Signature {
Signature::build("seq char")
.input_output_types(vec![(Type::Nothing, Type::List(Box::new(Type::String)))])
.required(
"start",
SyntaxShape::String,
"Start of character sequence (inclusive).",
)
.required(
"end",
SyntaxShape::String,
"End of character sequence (inclusive).",
)
.category(Category::Generators)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "sequence a to e",
example: "seq char a e",
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>
2023-09-03 16:27:29 +02:00
result: Some(Value::list(
vec![
Value::test_string('a'),
Value::test_string('b'),
Value::test_string('c'),
Value::test_string('d'),
Value::test_string('e'),
],
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>
2023-09-03 16:27:29 +02:00
Span::test_data(),
)),
},
Example {
Seq char update will work on all char (#14261) # Description - fixes #14174 This PR addresses a bug in the `seq char` command where the command's behavior did not align with its help description, which stated that it prints a sequence of ASCII characters. The initial implementation only allowed alphabetic characters, leading to user confusion when non-alphabetic characters (e.g., digits, punctuation) were rejected or when unexpected behavior occurred for certain input ranges. ### Changes Made: - **Updated the input validation**: Modified the `is_single_character` function to accept any ASCII character instead of restricting to alphabetic characters. - **Enhanced error messages**: Clarified error messages to specify that any single ASCII character is acceptable. - **Expanded functionality**: Ensured that the command can now generate sequences that include non-alphabetic ASCII characters. - **Updated tests**: Added tests to cover new use cases involving non-alphabetic characters and improved validation. ### Examples After Fix: - `seq char '0' '9'` now outputs `['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']` - `seq char ' ' '/'` outputs a list of characters from space to `/` - `seq char 'A' 'z'` correctly includes alphabetic and non-alphabetic characters between `A` and `z` # User-Facing Changes - Users can now input any single ASCII character for the `start` and `end` parameters of `seq char`. - The output will accurately include all characters within the specified ASCII range, including digits and punctuation. # Tests + Formatting - Added new tests to ensure the `seq char` command supports sequences including non-alphabetic ASCII characters.
2024-11-15 21:05:29 +01:00
description: "Sequence a to e, and join the characters with a pipe",
example: "seq char a e | str join '|'",
// TODO: it would be nice to test this example, but it currently breaks the input/output type tests
Seq char update will work on all char (#14261) # Description - fixes #14174 This PR addresses a bug in the `seq char` command where the command's behavior did not align with its help description, which stated that it prints a sequence of ASCII characters. The initial implementation only allowed alphabetic characters, leading to user confusion when non-alphabetic characters (e.g., digits, punctuation) were rejected or when unexpected behavior occurred for certain input ranges. ### Changes Made: - **Updated the input validation**: Modified the `is_single_character` function to accept any ASCII character instead of restricting to alphabetic characters. - **Enhanced error messages**: Clarified error messages to specify that any single ASCII character is acceptable. - **Expanded functionality**: Ensured that the command can now generate sequences that include non-alphabetic ASCII characters. - **Updated tests**: Added tests to cover new use cases involving non-alphabetic characters and improved validation. ### Examples After Fix: - `seq char '0' '9'` now outputs `['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']` - `seq char ' ' '/'` outputs a list of characters from space to `/` - `seq char 'A' 'z'` correctly includes alphabetic and non-alphabetic characters between `A` and `z` # User-Facing Changes - Users can now input any single ASCII character for the `start` and `end` parameters of `seq char`. - The output will accurately include all characters within the specified ASCII range, including digits and punctuation. # Tests + Formatting - Added new tests to ensure the `seq char` command supports sequences including non-alphabetic ASCII characters.
2024-11-15 21:05:29 +01:00
// result: Some(Value::test_string("a|b|c|d|e")),
result: None,
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
seq_char(engine_state, stack, call)
}
}
fn is_single_character(ch: &str) -> bool {
Seq char update will work on all char (#14261) # Description - fixes #14174 This PR addresses a bug in the `seq char` command where the command's behavior did not align with its help description, which stated that it prints a sequence of ASCII characters. The initial implementation only allowed alphabetic characters, leading to user confusion when non-alphabetic characters (e.g., digits, punctuation) were rejected or when unexpected behavior occurred for certain input ranges. ### Changes Made: - **Updated the input validation**: Modified the `is_single_character` function to accept any ASCII character instead of restricting to alphabetic characters. - **Enhanced error messages**: Clarified error messages to specify that any single ASCII character is acceptable. - **Expanded functionality**: Ensured that the command can now generate sequences that include non-alphabetic ASCII characters. - **Updated tests**: Added tests to cover new use cases involving non-alphabetic characters and improved validation. ### Examples After Fix: - `seq char '0' '9'` now outputs `['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']` - `seq char ' ' '/'` outputs a list of characters from space to `/` - `seq char 'A' 'z'` correctly includes alphabetic and non-alphabetic characters between `A` and `z` # User-Facing Changes - Users can now input any single ASCII character for the `start` and `end` parameters of `seq char`. - The output will accurately include all characters within the specified ASCII range, including digits and punctuation. # Tests + Formatting - Added new tests to ensure the `seq char` command supports sequences including non-alphabetic ASCII characters.
2024-11-15 21:05:29 +01:00
ch.is_ascii() && (ch.len() == 1)
}
fn seq_char(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let start: Spanned<String> = call.req(engine_state, stack, 0)?;
let end: Spanned<String> = call.req(engine_state, stack, 1)?;
if !is_single_character(&start.item) {
return Err(ShellError::GenericError {
error: "seq char only accepts individual ASCII characters as parameters".into(),
Seq char update will work on all char (#14261) # Description - fixes #14174 This PR addresses a bug in the `seq char` command where the command's behavior did not align with its help description, which stated that it prints a sequence of ASCII characters. The initial implementation only allowed alphabetic characters, leading to user confusion when non-alphabetic characters (e.g., digits, punctuation) were rejected or when unexpected behavior occurred for certain input ranges. ### Changes Made: - **Updated the input validation**: Modified the `is_single_character` function to accept any ASCII character instead of restricting to alphabetic characters. - **Enhanced error messages**: Clarified error messages to specify that any single ASCII character is acceptable. - **Expanded functionality**: Ensured that the command can now generate sequences that include non-alphabetic ASCII characters. - **Updated tests**: Added tests to cover new use cases involving non-alphabetic characters and improved validation. ### Examples After Fix: - `seq char '0' '9'` now outputs `['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']` - `seq char ' ' '/'` outputs a list of characters from space to `/` - `seq char 'A' 'z'` correctly includes alphabetic and non-alphabetic characters between `A` and `z` # User-Facing Changes - Users can now input any single ASCII character for the `start` and `end` parameters of `seq char`. - The output will accurately include all characters within the specified ASCII range, including digits and punctuation. # Tests + Formatting - Added new tests to ensure the `seq char` command supports sequences including non-alphabetic ASCII characters.
2024-11-15 21:05:29 +01:00
msg: "input should be a single ASCII character".into(),
span: Some(start.span),
help: None,
inner: vec![],
});
}
if !is_single_character(&end.item) {
return Err(ShellError::GenericError {
error: "seq char only accepts individual ASCII characters as parameters".into(),
Seq char update will work on all char (#14261) # Description - fixes #14174 This PR addresses a bug in the `seq char` command where the command's behavior did not align with its help description, which stated that it prints a sequence of ASCII characters. The initial implementation only allowed alphabetic characters, leading to user confusion when non-alphabetic characters (e.g., digits, punctuation) were rejected or when unexpected behavior occurred for certain input ranges. ### Changes Made: - **Updated the input validation**: Modified the `is_single_character` function to accept any ASCII character instead of restricting to alphabetic characters. - **Enhanced error messages**: Clarified error messages to specify that any single ASCII character is acceptable. - **Expanded functionality**: Ensured that the command can now generate sequences that include non-alphabetic ASCII characters. - **Updated tests**: Added tests to cover new use cases involving non-alphabetic characters and improved validation. ### Examples After Fix: - `seq char '0' '9'` now outputs `['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']` - `seq char ' ' '/'` outputs a list of characters from space to `/` - `seq char 'A' 'z'` correctly includes alphabetic and non-alphabetic characters between `A` and `z` # User-Facing Changes - Users can now input any single ASCII character for the `start` and `end` parameters of `seq char`. - The output will accurately include all characters within the specified ASCII range, including digits and punctuation. # Tests + Formatting - Added new tests to ensure the `seq char` command supports sequences including non-alphabetic ASCII characters.
2024-11-15 21:05:29 +01:00
msg: "input should be a single ASCII character".into(),
span: Some(end.span),
help: None,
inner: vec![],
});
}
let start = start
.item
.chars()
.next()
// expect is ok here, because we just checked the length
.expect("seq char input must contains 2 inputs");
let end = end
.item
.chars()
.next()
// expect is ok here, because we just checked the length
.expect("seq char input must contains 2 inputs");
let span = call.head;
run_seq_char(start, end, span)
}
fn run_seq_char(start_ch: char, end_ch: char, span: Span) -> Result<PipelineData, ShellError> {
Seq char update will work on all char (#14261) # Description - fixes #14174 This PR addresses a bug in the `seq char` command where the command's behavior did not align with its help description, which stated that it prints a sequence of ASCII characters. The initial implementation only allowed alphabetic characters, leading to user confusion when non-alphabetic characters (e.g., digits, punctuation) were rejected or when unexpected behavior occurred for certain input ranges. ### Changes Made: - **Updated the input validation**: Modified the `is_single_character` function to accept any ASCII character instead of restricting to alphabetic characters. - **Enhanced error messages**: Clarified error messages to specify that any single ASCII character is acceptable. - **Expanded functionality**: Ensured that the command can now generate sequences that include non-alphabetic ASCII characters. - **Updated tests**: Added tests to cover new use cases involving non-alphabetic characters and improved validation. ### Examples After Fix: - `seq char '0' '9'` now outputs `['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']` - `seq char ' ' '/'` outputs a list of characters from space to `/` - `seq char 'A' 'z'` correctly includes alphabetic and non-alphabetic characters between `A` and `z` # User-Facing Changes - Users can now input any single ASCII character for the `start` and `end` parameters of `seq char`. - The output will accurately include all characters within the specified ASCII range, including digits and punctuation. # Tests + Formatting - Added new tests to ensure the `seq char` command supports sequences including non-alphabetic ASCII characters.
2024-11-15 21:05:29 +01:00
let start = start_ch as u8;
let end = end_ch as u8;
let range = if start <= end {
start..=end
} else {
end..=start
};
let result_vec = if start <= end {
range.map(|c| (c as char).to_string()).collect::<Vec<_>>()
} else {
range
.rev()
.map(|c| (c as char).to_string())
.collect::<Vec<_>>()
};
let result = result_vec
.into_iter()
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>
2023-09-03 16:27:29 +02:00
.map(|x| Value::string(x, span))
.collect::<Vec<Value>>();
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>
2023-09-03 16:27:29 +02:00
Ok(Value::list(result, span).into_pipeline_data())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SeqChar {})
}
}