diff --git a/src/cli.rs b/src/cli.rs index 9ca315cd05..c84c39af19 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -226,6 +226,7 @@ impl History { } } +#[allow(dead_code)] fn create_default_starship_config() -> Option { let mut map = toml::value::Table::new(); map.insert("add_newline".into(), toml::Value::Boolean(false)); @@ -265,6 +266,7 @@ pub async fn cli() -> Result<(), Box> { per_item_command(Mkdir), per_item_command(Move), whole_stream_command(Version), + whole_stream_command(Clear), whole_stream_command(What), whole_stream_command(Which), whole_stream_command(Debug), diff --git a/src/commands.rs b/src/commands.rs index d28ce3b279..e9b8adf2c3 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -115,6 +115,8 @@ pub(crate) use debug::Debug; pub(crate) use default::Default; pub(crate) use echo::Echo; pub(crate) use edit::Edit; +pub(crate) mod clear; +pub(crate) use clear::Clear; pub(crate) use enter::Enter; pub(crate) use env::Env; #[allow(unused_imports)] diff --git a/src/commands/clear.rs b/src/commands/clear.rs new file mode 100644 index 0000000000..21590e10ac --- /dev/null +++ b/src/commands/clear.rs @@ -0,0 +1,40 @@ +use crate::commands::WholeStreamCommand; +use crate::prelude::*; +use nu_errors::ShellError; +use nu_protocol::Signature; +use std::process::Command; + +pub struct Clear; + +impl WholeStreamCommand for Clear { + fn name(&self) -> &str { + "clear" + } + fn signature(&self) -> Signature { + Signature::build("clear") + } + fn usage(&self) -> &str { + "clears the terminal" + } + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + clear(args, registry) + } +} +fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result { + if cfg!(windows) { + Command::new("cmd") + .args(&["/C", "cls"]) + .status() + .expect("failed to execute process"); + } else if cfg!(unix) { + Command::new("/bin/sh") + .args(&["-c", "clear"]) + .status() + .expect("failed to execute process"); + } + Ok(OutputStream::empty()) +}