Skip to content

Commit

Permalink
move message/prefix into the state
Browse files Browse the repository at this point in the history
It makes more sense here anyway, plus it gets rid of the mem::swap stuff
  • Loading branch information
chris-laplante committed Jun 17, 2022
1 parent af5d925 commit 72c25c1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
51 changes: 43 additions & 8 deletions src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::borrow::Cow;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, Weak};
use std::time::{Duration, Instant};
use std::{fmt, io, mem, thread};
use std::{fmt, io, thread};

use console::strip_ansi_codes;
#[cfg(test)]
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -71,13 +72,13 @@ impl ProgressBar {

/// A convenience builder-like function for a progress bar with a given prefix
pub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> ProgressBar {
self.state().style.prefix = prefix.into();
self.state().state.prefix = prefix.into();
self
}

/// A convenience builder-like function for a progress bar with a given message
pub fn with_message(self, message: impl Into<Cow<'static, str>>) -> ProgressBar {
self.state().style.message = message.into();
self.state().state.message = message.into();
self
}

Expand Down Expand Up @@ -121,11 +122,8 @@ impl ProgressBar {
/// Overrides the stored style
///
/// This does not redraw the bar. Call [`ProgressBar::tick()`] to force it.
pub fn set_style(&self, mut style: ProgressStyle) {
let mut state = self.state();
mem::swap(&mut state.style.message, &mut style.message);
mem::swap(&mut state.style.prefix, &mut style.prefix);
state.style = style;
pub fn set_style(&self, style: ProgressStyle) {
self.state().style = style;
}

/// Spawns a background thread to tick the progress bar
Expand Down Expand Up @@ -517,6 +515,26 @@ impl ProgressBar {
self.state().draw_target.remote().map(|(_, idx)| idx)
}

/// Current message
pub fn message(&self) -> Cow<'static, str> {
self.state().state.message.clone()
}

/// Current message (with ANSI escape codes stripped)
pub fn message_unstyled(&self) -> String {
strip_ansi_codes(&self.message()).to_string()
}

/// Current prefix
pub fn prefix(&self) -> Cow<'static, str> {
self.state().state.prefix.clone()
}

/// Current prefix (with ANSI escape codes stripped)
pub fn prefix_unstyled(&self) -> String {
strip_ansi_codes(&self.prefix()).to_string()
}

#[inline]
pub(crate) fn state(&self) -> MutexGuard<'_, BarState> {
self.state.lock().unwrap()
Expand Down Expand Up @@ -644,6 +662,7 @@ pub(crate) static TICKER_TEST: Lazy<Mutex<()>> = Lazy::new(Mutex::default);
#[cfg(test)]
mod tests {
use super::*;
use console::Style;

#[allow(clippy::float_cmp)]
#[test]
Expand Down Expand Up @@ -743,4 +762,20 @@ mod tests {
drop(pb2);
assert!(!TICKER_RUNNING.load(Ordering::SeqCst));
}

#[test]
fn access_message_and_prefix() {
let pb = ProgressBar::new(80);
pb.set_message(Style::new().red().bold().apply_to("text").to_string());
pb.set_prefix(
Style::new()
.on_blue()
.italic()
.apply_to("prefix!")
.to_string(),
);

assert_eq!(pb.message_unstyled(), "text");
assert_eq!(pb.prefix_unstyled(), "prefix!");
}
}
12 changes: 8 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl BarState {
if let Some(len) = self.state.len {
self.state.pos.set(len);
}
self.style.message = msg;
self.state.message = msg;
}
ProgressFinish::AndClear => {
if let Some(len) = self.state.len {
Expand All @@ -51,7 +51,7 @@ impl BarState {
self.state.status = Status::DoneHidden;
}
ProgressFinish::Abandon => {}
ProgressFinish::AbandonWithMessage(msg) => self.style.message = msg,
ProgressFinish::AbandonWithMessage(msg) => self.state.message = msg,
}

// There's no need to update the estimate here; once the `status` is no longer
Expand Down Expand Up @@ -93,12 +93,12 @@ impl BarState {
}

pub(crate) fn set_message(&mut self, now: Instant, msg: Cow<'static, str>) {
self.style.message = msg;
self.state.message = msg;
self.update_estimate_and_draw(now);
}

pub(crate) fn set_prefix(&mut self, now: Instant, prefix: Cow<'static, str>) {
self.style.prefix = prefix;
self.state.prefix = prefix;
self.update_estimate_and_draw(now);
}

Expand Down Expand Up @@ -190,6 +190,8 @@ pub struct ProgressState {
pub(crate) started: Instant,
status: Status,
est: Estimator,
pub(crate) message: Cow<'static, str>,
pub(crate) prefix: Cow<'static, str>,
}

impl ProgressState {
Expand All @@ -201,6 +203,8 @@ impl ProgressState {
status: Status::InProgress,
started: Instant::now(),
est: Estimator::new(Instant::now()),
message: "".into(),
prefix: "".into(),
}
}

Expand Down
36 changes: 16 additions & 20 deletions src/style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;

use std::collections::HashMap;
use std::fmt::{self, Write};
use std::mem;
Expand All @@ -15,8 +15,6 @@ use crate::state::ProgressState;
/// Controls the rendering style of progress bars
#[derive(Clone)]
pub struct ProgressStyle {
pub(crate) message: Cow<'static, str>,
pub(crate) prefix: Cow<'static, str>,
tick_strings: Vec<Box<str>>,
progress_chars: Vec<Box<str>>,
template: Template,
Expand Down Expand Up @@ -81,8 +79,6 @@ impl ProgressStyle {
let progress_chars = segment("█░");
let char_width = width(&progress_chars);
Self {
message: "".into(),
prefix: "".into(),
tick_strings: "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ "
.chars()
.map(|c| c.to_string().into())
Expand Down Expand Up @@ -258,8 +254,8 @@ impl ProgressStyle {
wide = Some(WideElement::Message { align });
buf.push('\x00');
}
"msg" => buf.push_str(&self.message),
"prefix" => buf.push_str(&self.prefix),
"msg" => buf.push_str(&state.message),
"prefix" => buf.push_str(&state.prefix),
"pos" => buf.write_fmt(format_args!("{}", pos)).unwrap(),
"human_pos" => {
buf.write_fmt(format_args!("{}", HumanCount(pos))).unwrap()
Expand Down Expand Up @@ -392,7 +388,7 @@ impl<'a> WideElement<'a> {
buf.write_fmt(format_args!(
"{}",
PaddedStringDisplay {
str: &style.message,
str: &state.message,
width: left,
align: *align,
truncate: true,
Expand Down Expand Up @@ -558,7 +554,7 @@ impl fmt::Display for TemplateError {

impl std::error::Error for TemplateError {}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
enum TemplatePart {
Literal(String),
Placeholder {
Expand All @@ -572,7 +568,7 @@ enum TemplatePart {
NewLine,
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum State {
Literal,
MaybeOpen,
Expand Down Expand Up @@ -733,23 +729,23 @@ mod tests {
fn align_truncation() {
const WIDTH: u16 = 10;
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

let mut style = ProgressStyle::with_template("{wide_msg}").unwrap();
style.message = "abcdefghijklmnopqrst".into();
let style = ProgressStyle::with_template("{wide_msg}").unwrap();
state.message = "abcdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "abcdefghij");

buf.clear();
let mut style = ProgressStyle::with_template("{wide_msg:>}").unwrap();
style.message = "abcdefghijklmnopqrst".into();
let style = ProgressStyle::with_template("{wide_msg:>}").unwrap();
state.message = "abcdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "klmnopqrst");

buf.clear();
let mut style = ProgressStyle::with_template("{wide_msg:^}").unwrap();
style.message = "abcdefghijklmnopqrst".into();
let style = ProgressStyle::with_template("{wide_msg:^}").unwrap();
state.message = "abcdefghijklmnopqrst".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "fghijklmno");
}
Expand All @@ -761,7 +757,7 @@ mod tests {
let pos = Arc::new(AtomicPosition::new());
// half finished
pos.set(2);
let state = ProgressState::new(Some(4), pos);
let mut state = ProgressState::new(Some(4), pos);
let mut buf = Vec::new();

let style = ProgressStyle::with_template("{wide_bar}")
Expand All @@ -781,8 +777,8 @@ mod tests {
);

buf.clear();
let mut style = ProgressStyle::with_template("{wide_msg:^.red.on_blue}").unwrap();
style.message = "foobar".into();
let style = ProgressStyle::with_template("{wide_msg:^.red.on_blue}").unwrap();
state.message = "foobar".into();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "\u{1b}[31m\u{1b}[44m foobar \u{1b}[0m");
}
Expand Down

0 comments on commit 72c25c1

Please sign in to comment.