console-rs / indicatif

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

Bug: Progress Bar not showing while using `wrap_write` #590

Open rawhuul opened 9 months ago

rawhuul commented 9 months ago

Hey, there I am using wrap_write function to download some file which does the work but doesn't show the progress bar.

Here is the code:

 let downloader = || {
        let file = File::create(&file_path)
            .map_err(|_| ScoopieError::UnableToCreateFile(file_name.into()))?;

        let st = ProgressStyle::with_template(
            "{spinner:.bold} {msg}: [{percent:.bold}% ({bytes:.bold}/{total_bytes:.bold})]",
        )
        .unwrap();

        let pb = ProgressBar::new(total_size);
        pb.set_style(st);
        pb.set_message(format!("Collecting package {}", style(pkg_name).bold()));

        std::io::copy(&mut response.as_bytes(), &mut pb.wrap_write(file))
            .map_err(|_| ScoopieError::ChunkWrite(file_path.to_path_buf()))?;

        match verify {
            Some(hash) => match hash.verify(&file_path)? {
                true => Ok(DownloadStatus::DownloadedAndVerified(file_name.into())),
                false => Err(ScoopieError::WrongDigest(file_name.into())),
            },
            None => Ok(DownloadStatus::Downloaded(file_name.into())),
        }
    };

Result: https://github.com/console-rs/indicatif/assets/84739019/f634e1ed-1d2e-4db9-8cc2-8972bfa69e7b

While doing this manually, does show progress bar.

let mut downloader = || {
        let mut file = BufWriter::new(
            File::create(&file_path)
                .map_err(|_| ScoopieError::UnableToCreateFile(file_name.into()))?,
        );

        let st = ProgressStyle::with_template(
            "{spinner:.bold} {msg}: [{percent:.bold}% ({bytes:.bold}/{total_bytes:.bold})]",
        )
        .unwrap();

        let pb = ProgressBar::new(total_size);
        pb.set_style(st);
        pb.set_message(format!("Collecting package {}", style(pkg_name).bold()));

        let mut chunk = [0; 4096];

        while let Ok(bytes_read) = response.read(&mut chunk) {
            if bytes_read == 0 {
                break;
            }

            file.write_all(&chunk[..bytes_read])
                .map_err(|_| ScoopieError::ChunkWrite(file_path.to_path_buf()))?;

            pb.inc(bytes_read as u64);
        }

        file.flush()
            .map_err(|_| ScoopieError::FlushFile(file_path.to_path_buf()))?;

        match verify {
            Some(hash) => match hash.verify(&file_path)? {
                true => {
                          Ok(DownloadStatus::DownloadedAndVerified(file_name.into()))
                }
                false => {
                    Err(ScoopieError::WrongDigest(file_name.into()))
                }
            },
            None => {
                Ok(DownloadStatus::Downloaded(file_name.into()))
            }
        }
    };

Result: https://github.com/console-rs/indicatif/assets/84739019/a721680d-7d63-432a-9294-dadec319c6de

My Cargo.toml looks like:

minreq = { version = "2.10.0", features = ["https-rustls"] }
indicatif = { version = "0.17.6", features = ["rayon"] }
console = "0.15.7"
djc commented 9 months ago

Sorry, I can't think of a good reason for this off the top of my head and I don't have much time to investigate. I suggest doing some debugging by using a local copy of indicatif and dumping some debugging from the wrap_write wrapper to get a feeling for what's going on.

rawhuul commented 9 months ago

Sorry, I can't think of a good reason for this off the top of my head and I don't have much time to investigate. I suggest doing some debugging by using a local copy of indicatif and dumping some debugging from the wrap_write wrapper to get a feeling for what's going on.

Okay i will try to figure out whats going on

rockboynton commented 1 month ago

I see this issue as well.

djc commented 1 month ago

(Still don't have time to investigate, but happy to discuss further.)