Skip to content

Commit

Permalink
Add 'name' and 'set_name' methods to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 16, 2020
1 parent b2af5b9 commit 94c4390
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v9.0.0

### Breaking

* add `set_name(…)` and `name()` to `Progress` trait.

## v8.0.1 - Add missing trailing paranthesis in throughput display

## v8.0.0
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prodash"
version = "8.0.1"
version = "9.0.0"
authors = ["Sebastian Thiel <[email protected]>"]
description = "A dashboard for visualizing progress of asynchronous and possibly blocking tasks"
edition = "2018"
Expand Down
17 changes: 16 additions & 1 deletion src/progress/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Log {
}

const EMIT_LOG_EVERY_S: f32 = 0.5;
const SEP: &str = "::";

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

fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
Log {
name: format!("{}::{}", self.name, Into::<String>::into(name)),
name: format!("{}{}{}", self.name, SEP, Into::<String>::into(name)),
current_level: self.current_level + 1,
max_level: self.max_level,
step: 0,
Expand Down Expand Up @@ -87,6 +88,20 @@ impl Progress for Log {
self.set(self.step + step)
}

fn set_name(&mut self, name: impl Into<String>) {
let name = name.into();
self.name = self
.name
.split("::")
.next()
.map(|parent| format!("{}{}{}", parent.to_owned(), SEP, name))
.unwrap_or(name);
}

fn name(&self) -> Option<String> {
self.name.split(SEP).skip(1).next().map(ToOwned::to_owned)
}

fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
let message: String = message.into();
match level {
Expand Down
36 changes: 36 additions & 0 deletions src/progress/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl Progress for Discard {

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

fn set_name(&mut self, _name: impl Into<String>) {}

fn name(&self) -> Option<String> {
None
}

fn message(&mut self, _level: MessageLevel, _message: impl Into<String>) {}
}

Expand Down Expand Up @@ -83,6 +89,20 @@ where
}
}

fn set_name(&mut self, name: impl Into<String>) {
match self {
Either::Left(l) => l.set_name(name),
Either::Right(r) => r.set_name(name),
}
}

fn name(&self) -> Option<String> {
match self {
Either::Left(l) => l.name(),
Either::Right(r) => r.name(),
}
}

fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
match self {
Either::Left(l) => l.message(level, message),
Expand Down Expand Up @@ -156,6 +176,14 @@ where
self.0.inc_by(step)
}

fn set_name(&mut self, name: impl Into<String>) {
self.0.set_name(name);
}

fn name(&self) -> Option<String> {
self.0.name()
}

fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
self.0.message(level, message)
}
Expand Down Expand Up @@ -202,6 +230,14 @@ impl<T: Progress> Progress for ThroughputOnDrop<T> {
self.0.inc_by(step)
}

fn set_name(&mut self, name: impl Into<String>) {
self.0.set_name(name)
}

fn name(&self) -> Option<String> {
self.0.name()
}

fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
self.0.message(level, message)
}
Expand Down
8 changes: 8 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub trait Progress {
self.inc_by(1)
}

/// Set the name of the instance, altering the value given when crating it with `add_child(…)`
/// The progress is allowed to discard it.
fn set_name(&mut self, name: impl Into<String>);

/// Get the name of the instance as given when creating it with `add_child(…)`
/// The progress is allowed to not be named, thus there is no guarantee that a previously set names 'sticks'.
fn name(&self) -> Option<String>;

/// Create a `message` of the given `level` and store it with the progress tree.
///
/// Use this to provide additional,human-readable information about the progress
Expand Down
8 changes: 8 additions & 0 deletions src/tree/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ impl crate::Progress for Item {
self.inc_by(step)
}

fn set_name(&mut self, name: impl Into<String>) {
Item::set_name(self, name)
}

fn name(&self) -> Option<String> {
Item::name(self)
}

fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
Item::message(self, level, message)
}
Expand Down

0 comments on commit 94c4390

Please sign in to comment.