2021-11-28 09:32:44 +01:00
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
|
|
use nu_protocol::{
|
2022-12-21 20:20:46 +01:00
|
|
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
2021-11-28 09:32:44 +01:00
|
|
|
};
|
|
|
|
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 {
|
2022-12-21 20:20:46 +01:00
|
|
|
Signature::build("clear")
|
|
|
|
.category(Category::Platform)
|
|
|
|
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
2021-11-28 09:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2022-09-07 10:40:44 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-11-28 09:32:44 +01:00
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-09-07 10:40:44 +02:00
|
|
|
let span = call.head;
|
|
|
|
|
2021-11-28 09:32:44 +01:00
|
|
|
if cfg!(windows) {
|
|
|
|
CommandSys::new("cmd")
|
|
|
|
.args(["/C", "cls"])
|
|
|
|
.status()
|
2022-09-07 10:40:44 +02:00
|
|
|
.map_err(|e| ShellError::IOErrorSpanned(e.to_string(), span))?;
|
2021-11-28 09:32:44 +01:00
|
|
|
} else if cfg!(unix) {
|
2022-09-07 10:40:44 +02:00
|
|
|
let mut cmd = CommandSys::new("/bin/sh");
|
|
|
|
|
|
|
|
if let Some(Value::String { val, .. }) = stack.get_env_var(engine_state, "TERM") {
|
|
|
|
cmd.env("TERM", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.args(["-c", "clear"])
|
2021-11-28 09:32:44 +01:00
|
|
|
.status()
|
2022-09-07 10:40:44 +02:00
|
|
|
.map_err(|e| ShellError::IOErrorSpanned(e.to_string(), span))?;
|
2021-11-28 09:32:44 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 10:40:44 +02:00
|
|
|
Ok(Value::Nothing { span }.into_pipeline_data())
|
2021-11-28 09:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "Clear the terminal",
|
|
|
|
example: "clear",
|
|
|
|
result: None,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|