console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.21k stars 238 forks source link

How to get the message content in memory #604

Closed victor-vb closed 7 months ago

victor-vb commented 7 months ago
let mut downloaded = 0;
let total_size = 231231231;

let pb = ProgressBar::new(total_size);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}")
    .unwrap()
    .progress_chars("=>-"));
let target = InMemoryTerm::new(15,150);
pb.set_draw_target(ProgressDrawTarget::term_like(Box::new(target)));
pb.wrap_write(std::io::stdout());
while downloaded < total_size {
    let new = min(downloaded + 223211, total_size);
    downloaded = new;
    pb.set_position(new);
    pb.tick();
    thread::sleep(Duration::from_millis(12));
}

pb.finish_with_message("downloaded");

I want to intercept the output. I don’t know if there is a method or feature to customize the relevant standard output and forward it to the front-end component: xtermjs. What kind of tool should I use?

chris-laplante commented 7 months ago

See here for the available methods on InMemoryTerminal: https://github.com/console-rs/indicatif/blob/main/src/in_memory.rs. However instead of using InMemoryTerminal, you should probably just implement the TermLike trait yourself. Then you can use it with ProgressDrawTarget::term_like.

victor-vb commented 7 months ago

See here for the available methods on InMemoryTerminal: https://github.com/console-rs/indicatif/blob/main/src/in_memory.rs. However instead of using InMemoryTerminal, you should probably just implement the TermLike trait yourself. Then you can use it with ProgressDrawTarget::term_like.

ok, I understand, implement a branch by myself