Is it possible to overwrite or clear an output on the terminal in Rust?
I'm just wondering. Thanks!
hwchen
March 12, 2016, 2:52pm
2
For clearing the current line, I've used a hack with ANSI escape codes.
I tried with various readline-like libraries, but was unsuccessful at the time. I might have missed something obvious though.
mod cli;
mod client;
mod executor;
mod ws;
use anyhow::{Context as _, Result};
use ansi_term::Colour::Blue;
use url::Url;
fn main() -> Result<()> {
// Command line interface
let matches = cli::get_cli();
let cert= matches.value_of("cert_path")
.map(|path| {
use std::io::Read;
let mut res = Vec::new();
let mut f = std::fs::File::open(path)?;
f.read_to_end(&mut res)?;
Ok::<_, std::io::Error>(res)
})
.transpose()
.context("Could not read certificate")?;
let opts = client::Opts {
auth: matches.value_of("auth").map(|s| s.to_owned()),
show_ping_pong: matches.is_present("show_ping_pong"),
cert,
};
if let Some(url_option) = matches.value_of("URL") {
let url: Url = url_option.parse()
.with_context(|| format!("Error parsing {:?}", url_option))?;
// print that client is connecting
let out_url = format!("Connecting to {:?} (Ctrl-C to exit)", url_option);
println!("{}", Blue.bold().paint(out_url));
client::wscat_client(url, opts)?;
}
Ok(())
}
1 Like
term
has a delete_line
method to clear the current line.
1 Like
If you just want to overwrite the last line (e.g. to update a status message), the easiest way is to write a '\r'
which will put the cursor at the beginning of the line.
2 Likes
Thanks, that was what I was looking for!