Skip to content

Commit

Permalink
rough layout on how to get much more powerful unit and value rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 8, 2020
1 parent 129d09d commit 90a9c2d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{fmt, sync::Arc, time::SystemTime};

#[cfg(test)]
mod tests;
mod unit;

/// The top-level of the progress tree.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -208,16 +209,6 @@ pub struct MessageCopyState {
total: usize,
}

pub trait DisplayValue {
fn display_value(&self, f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result;
fn display_unit(&self, f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result;
}

pub enum Unit {
Static(&'static str),
Dynamic(Box<dyn DisplayValue>),
}

/// A `Tree` represents an element of the progress tree.
///
/// It can be used to set progress and send messages.
Expand Down
49 changes: 49 additions & 0 deletions src/tree/unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::tree::ProgressStep;
use std::fmt;

pub trait DisplayValue {
fn display_current_value(
&self,
f: &mut fmt::Formatter,
value: ProgressStep,
max: Option<ProgressStep>,
) -> fmt::Result;
fn display_upper_bound(&self, f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result;
fn display_unit(&self, f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result;
fn display_percentage(&self, f: &mut fmt::Formatter, fraction: f64) -> fmt::Result;
}

pub enum Unit {
Static(&'static str, Option<UnitMode>),
Dynamic(Box<dyn DisplayValue>, Option<UnitMode>),
}

pub enum UnitMode {
PercentageBeforeValue,
PercentageAfterUnit,
}

pub(crate) struct UnitDisplay<'a> {
current_value: ProgressStep,
upper_bound: Option<ProgressStep>,
parent: &'a Unit,
}

impl Unit {
pub(crate) fn display(&self, current_value: ProgressStep, upper_bound: Option<ProgressStep>) -> UnitDisplay {
UnitDisplay {
current_value,
upper_bound,
parent: self,
}
}
}

impl<'a> fmt::Display for UnitDisplay<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.parent {
Unit::Static(unit, mode) => unimplemented!("static mode"),
Unit::Dynamic(unit, mode) => unimplemented!("dynamic mode"),
}
}
}

0 comments on commit 90a9c2d

Please sign in to comment.