File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use std:: time:: { Duration , Instant } ;
2+
3+ pub struct ProgressTracker {
4+ start : Instant ,
5+ last_emit : Instant ,
6+ current : u64 ,
7+ total : u64 ,
8+ }
9+
10+ pub struct ProgressSnapshot {
11+ pub current : u64 ,
12+ pub total : u64 ,
13+ pub speed : f64 ,
14+ }
15+
16+ impl ProgressTracker {
17+ pub fn new ( ) -> Self {
18+ let now = Instant :: now ( ) ;
19+ Self {
20+ start : now,
21+ last_emit : now,
22+ current : 0 ,
23+ total : 0 ,
24+ }
25+ }
26+
27+ pub fn set_total ( & mut self , total : u64 ) {
28+ self . total = total;
29+ }
30+
31+ pub fn update ( & mut self , current : u64 ) -> Option < ProgressSnapshot > {
32+ self . current = current;
33+
34+ if self . last_emit . elapsed ( ) < Duration :: from_millis ( 200 ) {
35+ return None ;
36+ }
37+
38+ self . last_emit = Instant :: now ( ) ;
39+
40+ let elapsed = self . start . elapsed ( ) . as_secs_f64 ( ) ;
41+ let speed = if elapsed > 0.0 {
42+ self . current as f64 / elapsed
43+ } else {
44+ 0.0
45+ } ;
46+
47+ Some ( ProgressSnapshot {
48+ current : self . current ,
49+ total : self . total ,
50+ speed,
51+ } )
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments