Skip to content

Commit

Permalink
Add Percentage and Bar at the same time!!! :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 4, 2019
1 parent 59ad2e6 commit 5bde50f
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/interactive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum ByteVisualization {
Percentage,
Bar,
LongBar,
PercentageAndBar,
}

pub struct DisplayByteVisualization {
Expand All @@ -57,7 +58,8 @@ impl ByteVisualization {
*self = match self {
Bar => LongBar,
LongBar => Percentage,
Percentage => Bar,
Percentage => PercentageAndBar,
PercentageAndBar => Bar,
}
}
pub fn display(&self, percentage: f32) -> DisplayByteVisualization {
Expand All @@ -73,14 +75,36 @@ impl fmt::Display for DisplayByteVisualization {
use ByteVisualization::*;
let Self { format, percentage } = self;

const BAR_SIZE: usize = 10;
match format {
Percentage => write!(f, " {:>5.02}% ", percentage * 100.0),
Bar => Self::make_bar(f, percentage, 10),
Percentage => Self::make_percentage(f, percentage),
PercentageAndBar => {
Self::make_percentage(f, percentage)?;
f.write_str(" ")?;
Self::make_bar(f, percentage, BAR_SIZE)
}
Bar => Self::make_bar(f, percentage, BAR_SIZE),
LongBar => Self::make_bar(f, percentage, 20),
}
}
}

impl DisplayByteVisualization {
fn make_bar(f: &mut fmt::Formatter, percentage: &f32, length: usize) -> Result<(), fmt::Error> {
let block_length = (length as f32 * percentage).round() as usize;
for _ in 0..block_length {
f.write_str(tui::symbols::block::FULL)?;
}
for _ in 0..length - block_length {
f.write_str(" ")?;
}
Ok(())
}
fn make_percentage(f: &mut fmt::Formatter, percentage: &f32) -> Result<(), fmt::Error> {
write!(f, " {:>5.02}% ", percentage * 100.0)
}
}

/// Options to configure how we display things
#[derive(Clone, Copy)]
pub struct DisplayOptions {
Expand Down Expand Up @@ -366,16 +390,3 @@ impl TerminalApp {
})
}
}

impl DisplayByteVisualization {
fn make_bar(f: &mut fmt::Formatter, percentage: &f32, length: usize) -> Result<(), fmt::Error> {
let block_length = (length as f32 * percentage).round() as usize;
for _ in 0..block_length {
f.write_str(tui::symbols::block::FULL)?;
}
for _ in 0..length - block_length {
f.write_str(" ")?;
}
Ok(())
}
}

0 comments on commit 5bde50f

Please sign in to comment.