Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94c4390

Browse files
committed
Add 'name' and 'set_name' methods to trait
1 parent b2af5b9 commit 94c4390

6 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v9.0.0
2+
3+
### Breaking
4+
5+
* add `set_name(…)` and `name()` to `Progress` trait.
6+
17
## v8.0.1 - Add missing trailing paranthesis in throughput display
28

39
## v8.0.0

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prodash"
3-
version = "8.0.1"
3+
version = "9.0.0"
44
authors = ["Sebastian Thiel <[email protected]>"]
55
description = "A dashboard for visualizing progress of asynchronous and possibly blocking tasks"
66
edition = "2018"

src/progress/log.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct Log {
1212
}
1313

1414
const EMIT_LOG_EVERY_S: f32 = 0.5;
15+
const SEP: &str = "::";
1516

1617
impl Log {
1718
pub fn new(name: impl Into<String>, max_level: Option<usize>) -> Self {
@@ -32,7 +33,7 @@ impl Progress for Log {
3233

3334
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
3435
Log {
35-
name: format!("{}::{}", self.name, Into::<String>::into(name)),
36+
name: format!("{}{}{}", self.name, SEP, Into::<String>::into(name)),
3637
current_level: self.current_level + 1,
3738
max_level: self.max_level,
3839
step: 0,
@@ -87,6 +88,20 @@ impl Progress for Log {
8788
self.set(self.step + step)
8889
}
8990

91+
fn set_name(&mut self, name: impl Into<String>) {
92+
let name = name.into();
93+
self.name = self
94+
.name
95+
.split("::")
96+
.next()
97+
.map(|parent| format!("{}{}{}", parent.to_owned(), SEP, name))
98+
.unwrap_or(name);
99+
}
100+
101+
fn name(&self) -> Option<String> {
102+
self.name.split(SEP).skip(1).next().map(ToOwned::to_owned)
103+
}
104+
90105
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
91106
let message: String = message.into();
92107
match level {

src/progress/utils.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ impl Progress for Discard {
1919

2020
fn inc_by(&mut self, _step: usize) {}
2121

22+
fn set_name(&mut self, _name: impl Into<String>) {}
23+
24+
fn name(&self) -> Option<String> {
25+
None
26+
}
27+
2228
fn message(&mut self, _level: MessageLevel, _message: impl Into<String>) {}
2329
}
2430

@@ -83,6 +89,20 @@ where
8389
}
8490
}
8591

92+
fn set_name(&mut self, name: impl Into<String>) {
93+
match self {
94+
Either::Left(l) => l.set_name(name),
95+
Either::Right(r) => r.set_name(name),
96+
}
97+
}
98+
99+
fn name(&self) -> Option<String> {
100+
match self {
101+
Either::Left(l) => l.name(),
102+
Either::Right(r) => r.name(),
103+
}
104+
}
105+
86106
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
87107
match self {
88108
Either::Left(l) => l.message(level, message),
@@ -156,6 +176,14 @@ where
156176
self.0.inc_by(step)
157177
}
158178

179+
fn set_name(&mut self, name: impl Into<String>) {
180+
self.0.set_name(name);
181+
}
182+
183+
fn name(&self) -> Option<String> {
184+
self.0.name()
185+
}
186+
159187
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
160188
self.0.message(level, message)
161189
}
@@ -202,6 +230,14 @@ impl<T: Progress> Progress for ThroughputOnDrop<T> {
202230
self.0.inc_by(step)
203231
}
204232

233+
fn set_name(&mut self, name: impl Into<String>) {
234+
self.0.set_name(name)
235+
}
236+
237+
fn name(&self) -> Option<String> {
238+
self.0.name()
239+
}
240+
205241
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
206242
self.0.message(level, message)
207243
}

src/traits.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ pub trait Progress {
5353
self.inc_by(1)
5454
}
5555

56+
/// Set the name of the instance, altering the value given when crating it with `add_child(…)`
57+
/// The progress is allowed to discard it.
58+
fn set_name(&mut self, name: impl Into<String>);
59+
60+
/// Get the name of the instance as given when creating it with `add_child(…)`
61+
/// The progress is allowed to not be named, thus there is no guarantee that a previously set names 'sticks'.
62+
fn name(&self) -> Option<String>;
63+
5664
/// Create a `message` of the given `level` and store it with the progress tree.
5765
///
5866
/// Use this to provide additional,human-readable information about the progress

src/tree/item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ impl crate::Progress for Item {
259259
self.inc_by(step)
260260
}
261261

262+
fn set_name(&mut self, name: impl Into<String>) {
263+
Item::set_name(self, name)
264+
}
265+
266+
fn name(&self) -> Option<String> {
267+
Item::name(self)
268+
}
269+
262270
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
263271
Item::message(self, level, message)
264272
}

0 commit comments

Comments
 (0)