Skip to content

Instantly share code, notes, and snippets.

@winstonewert
Created May 4, 2018 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save winstonewert/7a003b6c745226313ade0689e053fec3 to your computer and use it in GitHub Desktop.
Save winstonewert/7a003b6c745226313ade0689e053fec3 to your computer and use it in GitHub Desktop.
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