Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for left and right margin #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct ProgressBar<T: Write> {
tick: Vec<String>,
tick_state: usize,
width: Option<usize>,
margin_left: usize,
margin_right: usize,
message: String,
last_refresh_time: SteadyTime,
max_refresh_rate: Option<time::Duration>,
Expand Down Expand Up @@ -131,6 +133,8 @@ impl<T: Write> ProgressBar<T> {
tick: Vec::new(),
tick_state: 0,
width: None,
margin_left: 0,
margin_right: 0,
message: String::new(),
last_refresh_time: SteadyTime::now(),
max_refresh_rate: None,
Expand Down Expand Up @@ -229,6 +233,33 @@ impl<T: Write> ProgressBar<T> {
self.width = w;
}

/// Set margin_left, or 0 for default.
///
/// # Examples
///
/// ```ignore
/// let mut pb = ProgressBar::new(...);
/// pb.set_margin_left(2);
/// ```
pub fn set_margin_left(&mut self, ml: usize) {
self.margin_left= ml;
}

/// Set margin_right, or 0 for default.
///
/// This doesn't work for MultiBar
///
/// # Examples
///
/// ```ignore
/// let mut pb = ProgressBar::new(...);
/// pb.set_margin_right(2);
/// ```
pub fn set_margin_right(&mut self, mr: usize) {
self.margin_right = mr;
}

/// Set max refresh rate, above which the progress bar will not redraw, or `None` for none.
/// Set max refresh rate, above which the progress bar will not redraw, or `None` for none.
///
/// # Examples
Expand Down Expand Up @@ -316,14 +347,16 @@ impl<T: Write> ProgressBar<T> {

let time_elapsed = time_to_std(now - self.start_time);
let speed = self.current as f64 / fract_dur(time_elapsed);
let width = self.width();
// NOTE:
// margin_right doesn't work with multibar
let width = self.width() - self.margin_left - self.margin_right;

let mut base = String::new();
let mut suffix = String::new();
let mut prefix = String::new();
let mut out;

// precent box
// percent box
if self.show_percent {
let percent = self.current as f64 / (self.total as f64 / 100f64);
suffix = suffix +
Expand Down Expand Up @@ -385,6 +418,13 @@ impl<T: Write> ProgressBar<T> {
}
}
out = prefix + &base + &suffix;

// left margin
if self.margin_left > 0 {
let tmp: String = repeat!(" ", self.margin_left).to_string();
out = tmp + &out;
}

// pad
if out.len() < width {
let gap = width - out.len();
Expand Down