nushell/crates/nu-command/src/platform/clear.rs
nibon7 ca05553fc6
Simplify clear implementation (#11273)
# Description
This PR uses the `crossterm` api to reimplement `clear` command, since
`crossterm` is cross-platform.
This seems to work on linux and windows.

# User-Facing Changes
N/A

# Tests + Formatting
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `cargo test --workspace` to check that all tests pass (on Windows
make sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

# After Submitting
N/A
2023-12-09 15:24:19 -06:00

52 lines
1.2 KiB
Rust

use crossterm::{
cursor::MoveTo,
terminal::{Clear as ClearCommand, ClearType},
QueueableCommand,
};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Type};
use std::io::Write;
#[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)
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
std::io::stdout()
.queue(ClearCommand(ClearType::All))?
.queue(MoveTo(0, 0))?
.flush()?;
Ok(PipelineData::Empty)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Clear the terminal",
example: "clear",
result: None,
}]
}
}