Is there a way to overwrite console output using Rust instead of simply appending?
An example would be printing progress as a percentage; I would rather overwrite the line than print a new line.
log. To override a console method, we just need to redefine how the method is executed. You'll need to wrap your code to prevent the access of other functions to the private (original) method.
Another way to console. log your variables is to simply place your mouse cursor on them and then wrap them on the line below with Ctrl+Alt+W + Down or the line above with Ctrl+Alt+W + Up .
Consoles are usually controlled by printing out "control characters", but what they are depends on the platform and terminal type. You probably don't want to reinvent the wheel to do this.
You can use the crossterm crate to get this kind of console control. A simple example is:
use std::{thread, time};
use std::io::{Write, stdout};
use crossterm::{QueueableCommand, cursor};
fn main() {
let mut stdout = stdout();
for i in 1..10 {
stdout.queue(cursor::SavePosition);
stdout.write(format!("Here!!! {}", i).as_bytes());
stdout.queue(cursor::RestorePosition);
stdout.flush();
thread::sleep(time::Duration::from_millis(500));
}
println!("Hello, world!");
}
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