nushell on your web browser
nushell wasm terminal
at main 893 B view raw
1use js_sys::Math; 2use nu_protocol::Type; 3use nu_protocol::engine::Call; 4use nu_protocol::{ 5 Category, IntoPipelineData, PipelineData, ShellError, Signature, Value, 6 engine::{Command, EngineState, Stack}, 7}; 8 9#[derive(Clone)] 10pub struct Random; 11 12impl Command for Random { 13 fn name(&self) -> &str { 14 "random" 15 } 16 17 fn signature(&self) -> Signature { 18 Signature::build("random") 19 .category(Category::Math) 20 .input_output_type(Type::Nothing, Type::Float) 21 } 22 23 fn description(&self) -> &str { 24 "returns a random float between 0 and 1." 25 } 26 27 fn run( 28 &self, 29 _engine_state: &EngineState, 30 _stack: &mut Stack, 31 call: &Call, 32 _input: PipelineData, 33 ) -> Result<PipelineData, ShellError> { 34 let v: f64 = Math::random(); 35 Ok(Value::float(v, call.head).into_pipeline_data()) 36 } 37}