Created
May 4, 2018 19:08
-
-
Save winstonewert/7a003b6c745226313ade0689e053fec3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use pbr::ProgressBar; | |
use std::io::Read; | |
pub struct ProgressFile { | |
file: File, | |
progress: ProgressBar<::std::io::Stdout>, | |
} | |
impl ProgressFile { | |
pub fn new(file: File) -> Result<ProgressFile, ::std::io::Error> { | |
let size = file.metadata()?.len(); | |
let mut progress = ProgressBar::new(size); | |
progress.set_units(::pbr::Units::Bytes); | |
Ok(ProgressFile { file, progress }) | |
} | |
} | |
impl Read for ProgressFile { | |
fn read(&mut self, bytes: &mut [u8]) -> Result<usize, ::std::io::Error> { | |
let result = self.file.read(bytes); | |
if let Ok(size) = result { | |
if unsafe { ::libc::isatty(::libc::STDOUT_FILENO as i32) } != 0 { | |
self.progress.add(size as u64); | |
} | |
} | |
result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment