Add internal clear command (#1249)

* Add clear.rs

* update

* update

* cross-platformify

* update

* fix

* format

* fix warnings

* update implementation

* remove return

* remove semicolon

* change from `.output()` to `.status()`

* format
This commit is contained in:
Sean Hellum 2020-01-20 01:05:32 -06:00 committed by Jonathan Turner
parent fb977ab941
commit 3b57ee5dda
3 changed files with 44 additions and 0 deletions

View File

@ -226,6 +226,7 @@ impl History {
}
}
#[allow(dead_code)]
fn create_default_starship_config() -> Option<toml::Value> {
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<dyn Error>> {
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),

View File

@ -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)]

40
src/commands/clear.rs Normal file
View File

@ -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<OutputStream, ShellError> {
clear(args, registry)
}
}
fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
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())
}