Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the command line?

Tags:

rust

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.

like image 484
Tom Tetlaw Avatar asked Oct 13 '15 09:10

Tom Tetlaw


People also ask

How do I clear the command line in terminal?

A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.

Is there a clear command?

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.

How do I clear the command line in Linux?

You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators.


2 Answers

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");
}
like image 196
oli_obk Avatar answered Nov 09 '22 23:11

oli_obk


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();
        }
    }
}
like image 40
Jan-Fokke Avatar answered Nov 10 '22 00:11

Jan-Fokke