History

Add 'inc()' convenience methods to progress

Sebastian Thiel committed 6 years ago

Commit 2e46c9b1 parent 4018c966 files · +53 −14Browse files

Showing the changes to git-features/src/progress (3 of 6 files). Show all

Unified · Split

3 files
  1. git-features/src/progress/log.rs +8 −0
  2. git-features/src/progress/mod.rs +27 −0
  3. git-features/src/progress/prodash.rs +4 −0
git-features/src/progress/log.rs+8−0
@@ -6,6 +6,7 @@ pub struct Log { · 5 unmodified lines
66 max: Option<u32>,
77 unit: Option<&'static str>,
88 last_set: Option<std::time::SystemTime>,
9+ step: u32,
910 current_level: usize,
1011 max_level: usize,
1112 }
@@ -19,6 +20,7 @@ impl Log { · 7 unmodified lines
1920 current_level: 0,
2021 max_level: max_level.unwrap_or(usize::MAX),
2122 max: None,
23+ step: 0,
2224 unit: None,
2325 last_set: None,
2426 }
@@ -33,6 +35,7 @@ impl Progress for Log { · 8 unmodified lines
3335 name: format!("{}::{}", self.name, Into::<String>::into(name)),
3436 current_level: self.current_level + 1,
3537 max_level: self.max_level,
38+ step: 0,
3639 max: None,
3740 unit: None,
3841 last_set: None,
@@ -45,6 +48,7 @@ impl Progress for Log { · 6 unmodified lines
4548 }
4649
4750 fn set(&mut self, step: u32) {
51+ self.step = step;
4852 if self.current_level > self.max_level {
4953 return;
5054 }
@@ -69,6 +73,10 @@ impl Progress for Log { · 18 unmodified lines
6973 }
7074 }
7175
76+ fn inc_by(&mut self, step: u32) {
77+ self.set(self.step + step)
78+ }
79+
7280 fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
7381 let message: String = message.into();
7482 match level {
git-features/src/progress/mod.rs+27−0
@@ -38,6 +38,20 @@ pub trait Progress { · 37 unmodified lines
3838 /// **Note**: that this call has no effect unless `init(…)` was called before.
3939 fn set(&mut self, step: u32);
4040
41+ /// Increment the current progress to the given `step`. The cost of this call is negligible,
42+ /// making manual throttling *not* necessary.
43+ ///
44+ /// **Note**: that this call has no effect unless `init(…)` was called before.
45+ fn inc_by(&mut self, step: u32);
46+
47+ /// Increment the current progress to the given 1. The cost of this call is negligible,
48+ /// making manual throttling *not* necessary.
49+ ///
50+ /// **Note**: that this call has no effect unless `init(…)` was called before.
51+ fn inc(&mut self) {
52+ self.inc_by(1)
53+ }
54+
4155 /// Create a `message` of the given `level` and store it with the progress tree.
4256 ///
4357 /// Use this to provide additional,human-readable information about the progress
@@ -71,6 +85,8 @@ impl Progress for Discard { · 27 unmodified lines
7185
7286 fn set(&mut self, _step: u32) {}
7387
88+ fn inc_by(&mut self, _step: u32) {}
89+
7490 fn message(&mut self, _level: MessageLevel, _message: impl Into<String>) {}
7591 }
7692
@@ -107,6 +123,13 @@ where · 30 unmodified lines
107123 }
108124 }
109125
126+ fn inc_by(&mut self, step: u32) {
127+ match self {
128+ Either::Left(l) => l.inc_by(step),
129+ Either::Right(r) => r.inc_by(step),
130+ }
131+ }
132+
110133 fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
111134 match self {
112135 Either::Left(l) => l.message(level, message),
@@ -147,6 +170,10 @@ where · 34 unmodified lines
147170 self.0.set(step)
148171 }
149172
173+ fn inc_by(&mut self, step: u32) {
174+ self.0.inc_by(step)
175+ }
176+
150177 fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
151178 self.0.message(level, message)
152179 }
git-features/src/progress/prodash.rs+4−0
@@ -17,6 +17,10 @@ impl Progress for Item { · 16 unmodified lines
1717 Item::set(self, step)
1818 }
1919
20+ fn inc_by(&mut self, step: u32) {
21+ self.inc_by(step)
22+ }
23+
2024 fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
2125 Item::message(
2226 self,