Is there a Rust equivalent to the system
function in the C/C++ standard library? Specifically I am trying to use cls
to clear the command line.
A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.
clear is a computer operating system command which is used to bring the command line on top of the computer terminal. It is available in various Unix shells on Unix and Unix-like operating systems as well as on other systems such as KolibriOS.
You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators.
You could use C's system
function through the libc
crate. But fortunately there's a much better way: std::process::Command
.
A quick and dirty way to call cls
would be
if std::process::Command::new("cls").status().unwrap().success() {
println!("screen successfully cleared");
}
The mentioned methods did not work for me
The following method works in windows cmd
Cargo.toml
[dependencies]
winapi = "0.2.8"
kernel32-sys = "0.2.1"
Code
extern crate kernel32;
extern crate winapi;
use winapi::HANDLE;
use winapi::wincon::CONSOLE_SCREEN_BUFFER_INFO;
use winapi::wincon::COORD;
use winapi::wincon::SMALL_RECT;
use winapi::WORD;
use winapi::DWORD;
static mut CONSOLE_HANDLE: Option<HANDLE> = None;
fn get_output_handle() -> HANDLE {
unsafe {
if let Some(handle) = CONSOLE_HANDLE {
return handle;
} else {
let handle = kernel32::GetStdHandle(winapi::STD_OUTPUT_HANDLE);
CONSOLE_HANDLE = Some(handle);
return handle;
}
}
}
fn get_buffer_info() -> winapi::CONSOLE_SCREEN_BUFFER_INFO {
let handle = get_output_handle();
if handle == winapi::INVALID_HANDLE_VALUE {
panic!("NoConsole")
}
let mut buffer = CONSOLE_SCREEN_BUFFER_INFO {
dwSize: COORD { X: 0, Y: 0 },
dwCursorPosition: COORD { X: 0, Y: 0 },
wAttributes: 0 as WORD,
srWindow: SMALL_RECT {
Left: 0,
Top: 0,
Right: 0,
Bottom: 0,
},
dwMaximumWindowSize: COORD { X: 0, Y: 0 },
};
unsafe {
kernel32::GetConsoleScreenBufferInfo(handle, &mut buffer);
}
buffer
}
fn clear() {
let handle = get_output_handle();
if handle == winapi::INVALID_HANDLE_VALUE {
panic!("NoConsole")
}
let screen_buffer = get_buffer_info();
let console_size: DWORD = screen_buffer.dwSize.X as u32 * screen_buffer.dwSize.Y as u32;
let coord_screen = COORD { X: 0, Y: 0 };
let mut amount_chart_written: DWORD = 0;
unsafe {
kernel32::FillConsoleOutputCharacterW(
handle,
32 as winapi::WCHAR,
console_size,
coord_screen,
&mut amount_chart_written,
);
}
set_cursor_possition(0, 0);
}
fn set_cursor_possition(y: i16, x: i16) {
let handle = get_output_handle();
if handle == winapi::INVALID_HANDLE_VALUE {
panic!("NoConsole")
}
unsafe {
kernel32::SetConsoleCursorPosition(handle, COORD { X: x, Y: y });
}
}
Example
fn main() {
loop {
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
println!("You typed: {}", input);
if input.trim() == "clear" {
clear();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With