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