mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
# 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>
146 lines
4.1 KiB
Rust
146 lines
4.1 KiB
Rust
use nu_engine::CallExt;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
ast::Call, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
|
|
Spanned, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct SeqChar;
|
|
|
|
impl Command for SeqChar {
|
|
fn name(&self) -> &str {
|
|
"seq char"
|
|
}
|
|
|
|
fn usage(&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",
|
|
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'),
|
|
],
|
|
Span::test_data(),
|
|
)),
|
|
},
|
|
Example {
|
|
description: "sequence a to e, and put the characters in a pipe-separated string",
|
|
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
|
|
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 {
|
|
ch.is_ascii() && ch.len() == 1 && ch.chars().all(char::is_alphabetic)
|
|
}
|
|
|
|
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(
|
|
"seq char only accepts individual ASCII characters as parameters".into(),
|
|
"should be 1 character long".into(),
|
|
Some(start.span),
|
|
None,
|
|
Vec::new(),
|
|
));
|
|
}
|
|
|
|
if !is_single_character(&end.item) {
|
|
return Err(ShellError::GenericError(
|
|
"seq char only accepts individual ASCII characters as parameters".into(),
|
|
"should be 1 character long".into(),
|
|
Some(end.span),
|
|
None,
|
|
Vec::new(),
|
|
));
|
|
}
|
|
|
|
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> {
|
|
let mut result_vec = vec![];
|
|
for current_ch in start_ch as u8..end_ch as u8 + 1 {
|
|
result_vec.push((current_ch as char).to_string())
|
|
}
|
|
|
|
let result = result_vec
|
|
.into_iter()
|
|
.map(|x| Value::string(x, span))
|
|
.collect::<Vec<Value>>();
|
|
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 {})
|
|
}
|
|
}
|