From 4ead4ce4d641f8f615eff129afd4c9c2a042ee8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Kol=C4=8Din?= Date: Wed, 13 Aug 2025 18:45:45 +0000 Subject: [PATCH] feat: move random dice to std (#16420) # Description As per the suggestion in #16350, this PR moves `random dice` to std. It's basically a thin wrapper over `random int` already. # User-Facing Changes (deprecations) ## `random dice` moved to `std` The `random dice` command has been rewritten in Nushell and moved to the standard library. The `random dice` built-in is still available with a deprecation error, but will be removed in 0.108. The new command can be used as follows: ```nushell use std/random random dice ``` It's behavior, parameters, and defaults are the same. # After Submitting Update documentation to reflect the change. Closes #16350 --- crates/nu-command/src/random/dice.rs | 12 +++++++++++- crates/nu-std/src/lib.rs | 1 + crates/nu-std/std/mod.nu | 1 + crates/nu-std/std/random/mod.nu | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 crates/nu-std/std/random/mod.nu diff --git a/crates/nu-command/src/random/dice.rs b/crates/nu-command/src/random/dice.rs index 6e6ff4296c..9a2e3a2177 100644 --- a/crates/nu-command/src/random/dice.rs +++ b/crates/nu-command/src/random/dice.rs @@ -1,5 +1,5 @@ use nu_engine::command_prelude::*; -use nu_protocol::ListStream; +use nu_protocol::{DeprecationEntry, DeprecationType, ListStream, ReportMode}; use rand::random_range; use std::num::NonZeroUsize; @@ -38,6 +38,16 @@ impl Command for RandomDice { vec!["generate", "die", "1-6"] } + fn deprecation_info(&self) -> Vec { + vec![DeprecationEntry { + ty: DeprecationType::Command, + report_mode: ReportMode::FirstUse, + since: Some("0.107.0".to_owned()), + expected_removal: Some("0.108.0".to_owned()), + help: Some("Use `random dice` from std/random instead.".to_owned()), + }] + } + fn run( &self, engine_state: &EngineState, diff --git a/crates/nu-std/src/lib.rs b/crates/nu-std/src/lib.rs index a1c2240eb8..24ff5a774d 100644 --- a/crates/nu-std/src/lib.rs +++ b/crates/nu-std/src/lib.rs @@ -65,6 +65,7 @@ pub fn load_standard_library( include_str!("../std/testing/mod.nu"), ), ("mod.nu", "std/clip", include_str!("../std/clip/mod.nu")), + ("mod.nu", "std/random", include_str!("../std/random/mod.nu")), ]; for (filename, std_subdir_name, content) in std_submodules.drain(..) { diff --git a/crates/nu-std/std/mod.nu b/crates/nu-std/std/mod.nu index 9c45021f1c..51b55e95d7 100644 --- a/crates/nu-std/std/mod.nu +++ b/crates/nu-std/std/mod.nu @@ -16,6 +16,7 @@ export module std/math export module std/xml export module std/config export module std/testing +export module std/random # Load main dirs command and all subcommands export use std/dirs main diff --git a/crates/nu-std/std/random/mod.nu b/crates/nu-std/std/random/mod.nu new file mode 100644 index 0000000000..1f40cc5b64 --- /dev/null +++ b/crates/nu-std/std/random/mod.nu @@ -0,0 +1,19 @@ +# for examples +alias "random dice" = dice + +# Generate a random dice roll +@search-terms "generate" "die" "1-6" +@example "Roll 1 dice with 6 sides each" { random dice } +@example "Roll 10 dice with 12 sides each" { + random dice --dice 10 --sides 12 +} +export def dice [ + --dice = 1 # The amount of dice being rolled + --sides = 6 # The amount of sides a die has +]: nothing -> list { + mut out = [] + for _ in 1..$dice { + $out ++= [(random int 1..$sides)] + } + $out +}