Skip to content

Commit d39611a

Browse files
committed
feat: 新增进度条的抽象progress.rs
1 parent 7576d85 commit d39611a

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/core/progress.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

0 commit comments

Comments
 (0)