• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::io::{Read, Result};
2 use std::time::{Duration, Instant};
3 
4 pub struct Progress<R> {
5     bytes: usize,
6     tick: Instant,
7     stream: R,
8 }
9 
10 impl<R> Progress<R> {
new(stream: R) -> Self11     pub fn new(stream: R) -> Self {
12         Progress {
13             bytes: 0,
14             tick: Instant::now() + Duration::from_millis(2000),
15             stream,
16         }
17     }
18 }
19 
20 impl<R: Read> Read for Progress<R> {
read(&mut self, buf: &mut [u8]) -> Result<usize>21     fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
22         let num = self.stream.read(buf)?;
23         self.bytes += num;
24         let now = Instant::now();
25         if now > self.tick {
26             self.tick = now + Duration::from_millis(500);
27             errorf!("downloading... {} bytes\n", self.bytes);
28         }
29         Ok(num)
30     }
31 }
32 
33 impl<R> Drop for Progress<R> {
drop(&mut self)34     fn drop(&mut self) {
35         errorf!("done ({} bytes)\n", self.bytes);
36     }
37 }
38