nushell/crates/nu-command/src/random/dice.rs

95 lines
2.3 KiB
Rust
Raw Normal View History

use nu_engine::command_prelude::*;
use nu_protocol::ListStream;
2021-12-04 17:14:24 +00:00
use rand::prelude::{thread_rng, Rng};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"random dice"
}
fn signature(&self) -> Signature {
Signature::build("random dice")
.input_output_types(vec![(Type::Nothing, Type::ListStream)])
.allow_variants_without_examples(true)
2021-12-04 17:14:24 +00:00
.named(
"dice",
SyntaxShape::Int,
"The amount of dice being rolled",
Some('d'),
)
.named(
"sides",
SyntaxShape::Int,
"The amount of sides a die has",
Some('s'),
)
.category(Category::Random)
}
fn usage(&self) -> &str {
"Generate a random dice roll."
2021-12-04 17:14:24 +00:00
}
fn search_terms(&self) -> Vec<&str> {
vec!["generate", "die", "1-6"]
}
2021-12-04 17:14:24 +00:00
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-12-04 17:14:24 +00:00
dice(engine_state, stack, call)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Roll 1 dice with 6 sides each",
example: "random dice",
result: None,
},
Example {
description: "Roll 10 dice with 12 sides each",
example: "random dice --dice 10 --sides 12",
2021-12-04 17:14:24 +00:00
result: None,
},
]
}
}
fn dice(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let dice: usize = call.get_flag(engine_state, stack, "dice")?.unwrap_or(1);
let sides: usize = call.get_flag(engine_state, stack, "sides")?.unwrap_or(6);
let iter = (0..dice).map(move |_| {
let mut thread_rng = thread_rng();
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 14:27:29 +00:00
Value::int(thread_rng.gen_range(1..sides + 1) as i64, span)
2021-12-04 17:14:24 +00:00
});
Ok(ListStream::new(iter, span, engine_state.ctrlc.clone()).into())
2021-12-04 17:14:24 +00:00
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}