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

added new progress bar

parent 0b83b9bb
Pipeline #300 canceled with stages
[package]
name = "btui"
version = "0.5.5"
version = "0.6.5"
edition = "2018"
authors = ["Moritz Sokoll <moritz@sokoll.com>"]
license = "MIT"
......@@ -17,8 +17,11 @@ crate-type = ["lib"]
required-features = []
doc = true
[dependencies]
ncurses = {version = "5.101.0", optional = true}
[features]
default = ["core", "linux", "pbar"]
core = []
linux = ["core"]
linux = ["core", "ncurses"]
pbar = ["linux"]
......@@ -5,7 +5,7 @@
## Usage
Add this to your `Cargo.toml` dependency list:
```toml
btui = "0.5.5"
btui = "0.6.5"
```
## Features
This crate supports some different features:
......
......@@ -4,7 +4,7 @@
//! with btui you can create beautiful text user interfaces for the terminal.
//! To get started add `btui` as a dependency to your project:
//! ```toml
//! btui = "0.5.5"
//! btui = "0.6.5"
//! ```
//!
//! # Examples
......
/// module containig enums to represent different console actions for linux
/// module containig different console actions for linux
pub mod console {
/// enum for display control
pub enum DisplayControl {
......
/// structure representing a progressbar
#[derive(Clone)]
pub struct ProgressBar {
progress: u8,
text: String,
......@@ -55,3 +56,124 @@ impl ProgressBar {
format!("{}: [{}] {}%", self.text.clone(), bar, self.progress)
}
}
/// struct representing a progressbar with extra features
#[derive(Clone)]
pub struct ExtProgressBar {
format: String,
text: String,
progress: u8,
}
impl ExtProgressBar {
/// create a new progress bar
pub fn new(format: &str, text: &str) -> ExtProgressBar {
ExtProgressBar {
format: format.to_string(),
text: text.to_string(),
progress: 0,
}
}
/// increase current progress by one
pub fn incr(&mut self) {
if self.progress != 100 {
self.progress += 1;
}
}
/// 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;
} else {
self.progress = amount;
}
}
/// render the progress bar to a printable string
pub fn render(&self) -> String {
match self.format.len() {
4 => {
let mut format = self.format.chars();
let begin: char = format.next().unwrap();
let full: char = format.next().unwrap();
let empty: char = format.next().unwrap();
let close: char = format.next().unwrap();
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
bar.push_str(format!("{}", full).as_str());
idx += 1;
}
idx = 0;
let leftover: u8 = 100 - self.progress;
while idx != leftover {
bar.push_str(format!("{}", empty).as_str());
idx += 1;
}
format!(
"{}: {}{}{} {}%",
self.text, begin, bar, close, self.progress
)
}
5 => {
let mut format = self.format.chars();
let begin: char = format.next().unwrap();
let full: char = format.next().unwrap();
let full_end: char = format.next().unwrap();
let empty: char = format.next().unwrap();
let close: char = format.next().unwrap();
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
if (self.progress - idx) == 1 {
bar.push(full_end);
} else {
bar.push(full);
}
idx += 1;
}
idx = 0;
let leftover: u8 = 100 - self.progress;
while idx != leftover {
bar.push(empty);
idx += 1;
}
format!(
"{}: {}{}{} {}%",
self.text, begin, bar, close, self.progress
)
}
_ => {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while idx != self.progress {
if (self.progress - idx) == 1 {
bar.push('>');
} else {
bar.push('=');
}
idx += 1;
}
idx = 0;
let leftover: u8 = 100 - self.progress;
while idx != leftover {
bar.push(' ');
idx += 1;
}
format!("{}: [{}] {}%", self.text, bar, self.progress)
}
}
}
}
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