Rust: Add a "clear terminal" function to standard.

Created on 31 Jul 2020  路  7Comments  路  Source: rust-lang/rust

I haven't found a way to easily clear the console without using an external crate. Using the print!("\x1B[2J"); macro feels like a hack and doesn't always work. This is something that other languages have by default, so I don't see why there shouldn't be a cross platform implementation for clearing the terminal/console in standard?

I am aware that this exist, but it looks ugly unappealing.
assert!(Command::new("cls").status().or_else(|_| Command::new("clear")).unwrap().success());

Please correct me if there already is a function that does this.

C-feature-request T-libs

Most helpful comment

Most of these are not even language functions to clear terminal (you can call external command in Rust as well).
Aside from that calling them like that is bad practice: http://www.cplusplus.com/articles/j3wTURfi/

All 7 comments

This is something that other languages have by default

This is invalid. Not C/C++, Go, Java, Python or most languages.

The only language I am aware that have built-in clear function is JavaScript, but that's mostly because when API of console was originally designed, it's used by browsers which control the presentation of console messages. This is not true for CLI.

@nbdd0121
I believe that C# and F# have the System.Console.Clear(); command.

Also other languages such as Python, Go, have similar commands that clear the output for specific OS's.

GO: Command("clear")
C: system("cls");
LUA: os.execute( "clear" )
OCTAVE: system('clear');
PERL: system('clear')
PYTHON: os.system("clear")
RUBY: system 'clear'
VISUAL BASIC: System.Console.Clear()

The general pattern is that these languages have clear and concise ways of clearing the terminal, and you shouldn't have to write out assert!(Command::new("cls").status().or_else(|_| Command::new("clear")).unwrap().success()); in Rust every single time you wanted to clear the screen.

I was advocating for a function to handle this automatically that would be cross platform.

Most of these are not even language functions to clear terminal (you can call external command in Rust as well).
Aside from that calling them like that is bad practice: http://www.cplusplus.com/articles/j3wTURfi/

@mati865 If not system functions, then what would you recommend?

@sHaDoW-54 platform specific APIs are generally better solution but even they suffer from portability issues, for an example WinAPI doesn't clear terminals from Git For Windows and Cygwin/MSYS2.

Those are just some of the reasons why most of programming languages don't support that in their standard libraries and ones that provide it (developed by Microsoft) have various limitation.

@mati865 Is there some sort of universal solution being created, or is that virtually impossible?

I'm not aware of such solution. It may be possible to create it but with that amount of pitfalls it's surely not an easy thing.

Also obvious thing not mentioned by the previous article is redirecting streams. Stupidly simplified example:

$ clear > t.txt
$ cat -v t.txt
^[[H^[[2J^[[3J
Was this page helpful?
0 / 5 - 0 ratings