Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Progress Bar not showing while using wrap_write #590

Open
rawhuul opened this issue Sep 21, 2023 · 2 comments
Open

Bug: Progress Bar not showing while using wrap_write #590

rawhuul opened this issue Sep 21, 2023 · 2 comments

Comments

@rawhuul
Copy link

rawhuul commented Sep 21, 2023

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
Copy link
Collaborator

djc commented Sep 22, 2023

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
Copy link
Author

rawhuul commented Sep 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants