From c1c95e0a072c123cd1d52879410c211d4d357358 Mon Sep 17 00:00:00 2001 From: Frederik Delaere Date: Mon, 15 Oct 2018 14:41:37 +0200 Subject: [PATCH] added support for left and right margin --- src/pb.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/pb.rs b/src/pb.rs index 004ec6e9..f2c07173 100644 --- a/src/pb.rs +++ b/src/pb.rs @@ -49,6 +49,8 @@ pub struct ProgressBar { tick: Vec, tick_state: usize, width: Option, + margin_left: usize, + margin_right: usize, message: String, last_refresh_time: SteadyTime, max_refresh_rate: Option, @@ -131,6 +133,8 @@ impl ProgressBar { 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, @@ -229,6 +233,33 @@ impl ProgressBar { 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 @@ -316,14 +347,16 @@ impl ProgressBar { 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 + @@ -385,6 +418,13 @@ impl ProgressBar { } } 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();