简体   繁体   中英

How to clear console in Windows

I want to clear the console

I tried using this, but it doesn't work for Windows

print!("\x1B[2J");

Is there an easy way to clear the console?

I think I need some clarification first, on what you're attempting to do. Regardless, this is my best attempt at answering your question <3

Preamble

Given that different terminals work differently and have different APIs, I think you probably will want to use a crate that provides this functionality in a cross-plaform manner.

I'm assuming from here on that you want cross-platform functionality and that you don't mind using external dependencies. If this is correct, you might be happy to know that the terminal interaction crates are actually really developed in the Rust ecosystem. I have heard only good things about them.

With that out of the way, let's move on.

Do you want to just perform actions on the terminal?

If you just want to perform some actions on the terminal, like "clearing", "scrolling", "moving the cursor" and whatnot, I think you will be satisfied with the terminal crate. It allows to perform many actions, like clearing, independently of the platform you're in. It also allows for using interactivity features like interacting with the Mouse and the Keyboard:3

Or do you want to write a GUI for the terminal?

If what you want to do is write a Console-based User Interface though, I think that what might work for you instead is the tui crate . It has all of what you need to build terminal GUIs, from clearing of the console up to graphical widgets. Tools like gitui are written with tui .


Did I answer your question? Feel free to follow up if I fell short:)

I've found a way to clear the console in rust By using console crate, console.clear() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM