diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 6ac977d2aa..a61f47d92b 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -26,6 +26,7 @@ pub fn create_default_context() -> EngineState { Benchmark, BuildString, Cd, + Clear, Collect, Cp, Date, diff --git a/crates/nu-command/src/lib.rs b/crates/nu-command/src/lib.rs index ef23b8beaa..3e8cbee229 100644 --- a/crates/nu-command/src/lib.rs +++ b/crates/nu-command/src/lib.rs @@ -9,6 +9,7 @@ mod filesystem; mod filters; mod formats; mod math; +mod platform; mod shells; mod strings; mod system; @@ -28,6 +29,7 @@ pub use filesystem::*; pub use filters::*; pub use formats::*; pub use math::*; +pub use platform::*; pub use shells::*; pub use strings::*; pub use system::*; diff --git a/crates/nu-command/src/platform/clear.rs b/crates/nu-command/src/platform/clear.rs new file mode 100644 index 0000000000..fdc540bd63 --- /dev/null +++ b/crates/nu-command/src/platform/clear.rs @@ -0,0 +1,53 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{ + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Value, +}; +use std::process::Command as CommandSys; + +#[derive(Clone)] +pub struct Clear; + +impl Command for Clear { + fn name(&self) -> &str { + "clear" + } + + fn usage(&self) -> &str { + "Clear the terminal." + } + + fn signature(&self) -> Signature { + Signature::build("clear").category(Category::Platform) + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + if cfg!(windows) { + CommandSys::new("cmd") + .args(["/C", "cls"]) + .status() + .expect("failed to execute process"); + } else if cfg!(unix) { + CommandSys::new("/bin/sh") + .args(["-c", "clear"]) + .status() + .expect("failed to execute process"); + } + + Ok(Value::Nothing { span: call.head }.into_pipeline_data()) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Clear the terminal", + example: "clear", + result: None, + }] + } +} diff --git a/crates/nu-command/src/platform/mod.rs b/crates/nu-command/src/platform/mod.rs new file mode 100644 index 0000000000..0d2cdbdbdc --- /dev/null +++ b/crates/nu-command/src/platform/mod.rs @@ -0,0 +1,3 @@ +mod clear; + +pub use clear::Clear; diff --git a/crates/nu-protocol/src/signature.rs b/crates/nu-protocol/src/signature.rs index e7fa3a4975..6908d9253e 100644 --- a/crates/nu-protocol/src/signature.rs +++ b/crates/nu-protocol/src/signature.rs @@ -42,6 +42,7 @@ pub enum Category { Filters, Formats, Math, + Platform, Shells, Strings, System, @@ -62,6 +63,7 @@ impl std::fmt::Display for Category { Category::Filters => "filters", Category::Formats => "formats", Category::Math => "math", + Category::Platform => "platform", Category::Shells => "shells", Category::Strings => "strings", Category::System => "system",