Verified Commit 4d25d02a authored by Moritz Sokoll's avatar Moritz Sokoll 🦀
Browse files

progress bars now take floating point numbers instead of unsigned integers

parent 0c9ba598
Pipeline #318 passed with stages
in 33 seconds
[package]
name = "btui"
version = "0.6.7"
version = "0.6.8"
edition = "2018"
authors = ["Moritz Sokoll <moritz@sokoll.com>"]
license = "MIT"
......
......@@ -34,6 +34,7 @@ pub use ft::{effects, print};
#[cfg(test)]
mod tests {
use crate::effects::Color::Black;
use crate::pbar::*;
use crate::print::fg;
use crate::Terminal;
#[test]
......@@ -46,7 +47,26 @@ mod tests {
match t.println("Hello World!") {
Ok(_) => (),
Err(e) => panic!("{}", e),
}
}
#[test]
fn progressbar() {
let t: Terminal = Terminal::default();
let mut pbar = ProgressBar::new("test", '-', '#');
pbar.set_progress(75.6);
match t.println(format!("{}", pbar.render())) {
Ok(_) => (),
Err(e) => panic!("{}", e),
}
}
#[test]
fn extprogressbar() {
let t: Terminal = Terminal::default();
let mut pbar = ExtProgressBar::new("[=> ]", "test");
pbar.set_progress(75.6);
match t.println(format!("{}", pbar.render())) {
Ok(_) => (),
Err(e) => panic!("{}", e),
}
}
}
/// structure representing a progressbar
#[derive(Clone)]
pub struct ProgressBar {
progress: u8,
progress: f32,
text: String,
empty: char,
full: char,
......@@ -17,7 +17,7 @@ impl ProgressBar {
/// Returns a new progress bar
pub fn new(text: &str, empty: char, full: char) -> ProgressBar {
ProgressBar {
progress: 0u8,
progress: 0.0,
text: text.to_string(),
empty,
full,
......@@ -26,15 +26,15 @@ impl ProgressBar {
/// increase the current progress
pub fn incr(&mut self) {
if self.progress < 100u8 {
self.progress += 1u8;
if self.progress < 100.0 {
self.progress += 1.0;
}
}
/// set the current progress
pub fn set_progress(&mut self, amount: u8) {
if amount > 100u8 {
self.progress = 100u8;
pub fn set_progress(&mut self, amount: f32) {
if amount > 100.0 {
self.progress = 100.0;
} else {
self.progress = amount;
}
......@@ -43,11 +43,11 @@ impl ProgressBar {
pub fn render(&self) -> String {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
while (idx as f32) < self.progress {
bar.push_str(format!("{}", self.full).as_str());
idx += 1;
}
let leftover: u8 = 100u8 - self.progress;
let leftover: u8 = (100.0 - self.progress).floor() as u8;
idx = 0;
while idx != leftover {
bar.push_str(format!("{}", self.empty).as_str());
......@@ -62,7 +62,7 @@ impl ProgressBar {
pub struct ExtProgressBar {
format: String,
text: String,
progress: u8,
progress: f32,
}
impl ExtProgressBar {
......@@ -71,23 +71,23 @@ impl ExtProgressBar {
ExtProgressBar {
format: format.to_string(),
text: text.to_string(),
progress: 0,
progress: 0.0,
}
}
/// increase current progress by one
pub fn incr(&mut self) {
if self.progress != 100 {
self.progress += 1;
if self.progress < 100.0 {
self.progress += 1.0;
}
}
/// set the progress to a specific amount
/// # Arguments
/// *`amount`: the amount to set
pub fn set_progress(&mut self, amount: u8) {
if amount > 100 {
self.progress = 100;
pub fn set_progress(&mut self, amount: f32) {
if amount > 100.0 {
self.progress = 100.0;
} else {
self.progress = amount;
}
......@@ -106,13 +106,13 @@ impl ExtProgressBar {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
while (idx as f32) < self.progress {
bar.push_str(format!("{}", full).as_str());
idx += 1;
}
idx = 0;
let leftover: u8 = 100 - self.progress;
let leftover: u8 = (100.0 - self.progress) as u8;
while idx != leftover {
bar.push_str(format!("{}", empty).as_str());
idx += 1;
......@@ -133,9 +133,10 @@ impl ExtProgressBar {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
if (self.progress - idx) == 1 {
while (idx as f32) < self.progress {
if ((self.progress - idx as f32).floor() - 1.0).abs() < 1.0 {
bar.push(full_end);
break;
} else {
bar.push(full);
}
......@@ -143,7 +144,7 @@ impl ExtProgressBar {
}
idx = 0;
let leftover: u8 = 100 - self.progress;
let leftover: u8 = (100.0 - self.progress).floor() as u8;
while idx != leftover {
bar.push(empty);
idx += 1;
......@@ -157,8 +158,8 @@ impl ExtProgressBar {
_ => {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
if (self.progress - idx) == 1 {
while (idx as f32) < self.progress {
if ((self.progress - idx as f32).floor() - 1.0).abs() < 1.0 {
bar.push('>');
} else {
bar.push('=');
......@@ -167,7 +168,7 @@ impl ExtProgressBar {
}
idx = 0;
let leftover: u8 = 100 - self.progress;
let leftover: u8 = (100.0 - self.progress).floor() as u8;
while idx != leftover {
bar.push(' ');
idx += 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment