1use std::io::{Read, Result};
2use std::time::{Duration, Instant};
3
4pub struct Progress<R> {
5 bytes: usize,
6 tick: Instant,
7 stream: R,
8}
9
10impl<R> Progress<R> {
11 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
20impl<R: Read> Read for Progress<R> {
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
33impl<R> Drop for Progress<R> {
34 fn drop(&mut self) {
35 errorf!("done ({} bytes)\n", self.bytes);
36 }
37}
38