1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
///! Progress bar that has a size and also a max size.
use super::*;
use std::{
fmt::Write,
io,
};
/// A progress bar with a size and optionally title. It implements the `ProgressBar` trait, and is the default progress bar.
///
/// The bar has a max size, that is usually derived from the size of your terminal (if it can be detected) or can be set yourself, to stop the title line going over the side.
/// It also has a configurable width, which is defaulted to 50.
///
/// # Usage
///
/// To create a new `progress::Bar` with a max size tuned to your terminal (or `Width+20`, if it cannot be detected), and of the default size, `Bar` implements the `Default` trait:
/// ```rust
/// # use termprogress::prelude::*;
/// let mut bar = Bar::default(); //Creates a bar of width 50 by default.
/// ```
///
/// You can configure sizes and initial title with `new()`, `with_title()`, and `with_max()` functions.
/// # How it looks
/// It renders in the terminal like:
/// `[========================= ]: 50% this is a title that may get cut if it reaches max le...`
///
/// # Thread `Sync`safety
/// This type is safely `Sync` (where `T` is), the behaviour is defined to prevent overlapping writes to `T`.
/// Though it is *advised* to not render a `Bar` from more than a single thread, you still safely can.
///
/// Rendering functions should not be called on multiple threads at the same time, though it is safe to do so.
/// A thread-sync'd rendering operation will safetly (and silently) give up before writing if another thread is already engaging in one.
///
/// A display operation on one thread will cause any other threads attempting on to silently and safely abort their display attempt before anything is written to output.
#[derive(Debug)]
pub struct Bar<T: ?Sized = DefaultOutputDevice>
{
width: usize,
max_width: usize,
progress: f64,
buffer: String,
title: String,
#[cfg(feature="size")]
fit_to_term: bool,
// Allowing `Bar` to manage the sync will ensure that the bar is not interrupted by another bar-related write, and so any accidental inter-thread corrupting writes will not be drawn (unlike if we relied on `T`'s sync, since we have multiple `write()` calls when rendering and blanking.) *NOTE*: using `AtomicRefCell` i think is actually still be preferable for those reasons. If `T` can be shared and written to with internal sync (like stdout/err,) then non-`Bar` writes are not affected, but `Bar` writes are better contained.
output: AtomicRefCell<T>
}
/// The default size of the terminal bar when the programmer does not provide her own.
/// Or if `size` is not used.
pub const DEFAULT_SIZE: usize = 50;
/// The default size of the max render bar when the programmer does not provide her own.
/// Or if `size` is not used.
pub const DEFAULT_MAX_BORDER_SIZE: usize = 20;
/*
impl<T: Default + io::Write> Default for Bar<T>
{
#[inline]
fn default() -> Self
{
Self::new(T::default(), DEFAULT_SIZE)
}
}
*/
impl Default for Bar
{
#[inline]
fn default() -> Self
{
Self::new_default(DEFAULT_SIZE)
}
}
impl Bar {
/// Create a new bar `width` long with a title using the default output descriptor `stdout`.
#[inline]
pub fn with_title_default(width: usize, title: impl AsRef<str>) -> Self
{
Self::with_title(create_default_output_device(), width, title)
}
/// Attempt to create a new bar with max display width of our terminal and a title.
///
/// If `stdout` is not a terminal, then `None` is returned.
#[cfg(feature="size")]
#[inline]
pub fn try_new_with_title_default(width: usize, title: impl AsRef<str>) -> Option<Self>
{
Self::try_new_with_title(create_default_output_device(), width, title)
}
/// Create a new bar with max display width of our terminal
///
/// # Notes
/// Without feature `size`, will be the same as `Self::with_max(width, width +20)`
///
/// To try to create one that always adheres to `size`, use the `try_new()` family of functions.
#[inline]
pub fn new_default(width: usize) -> Self
{
Self::new(create_default_output_device(), width)
}
/// Attempt to create a new bar with max display width of our terminal.
///
/// If `stdout` is not a terminal, then `None` is returned.
#[cfg(feature="size")]
#[inline]
pub fn try_new_default(width: usize) -> Option<Self>
{
Self::try_new(create_default_output_device(), width)
}
/// Attempt to create a new bar with max display width of our terminal.
///
/// If `stdout` is not a terminal, then `None` is returned.
#[cfg(feature="size")]
#[inline]
pub fn try_new_default_size_default() -> Option<Self>
{
Self::try_new_default_size(create_default_output_device())
}
/// Create a bar with a max display width
///
/// # Panics
/// If `width` is larger than or equal to `max_width`.
#[inline]
pub fn with_max_default(width: usize, max_width: usize) -> Self
{
Self::with_max(create_default_output_device(), width, max_width)
}
}
impl<T: io::Write + AsRawFd> Bar<T>
{
/// Create a new bar `width` long with a title.
pub fn with_title(output: impl Into<T> + AsRawFd, width: usize, title: impl AsRef<str>) -> Self
{
let mut this = Self::new(output, width);
this.add_title(title.as_ref());
this
}
#[inline(always)] fn add_title(&mut self, title: &str)
{
self.set_title(title);
self.update()
}
/// Attempt to create a new bar with max display width of our terminal and a title.
///
/// If `output` is not a terminal, then `None` is returned.
#[cfg(feature="size")]
pub fn try_new_with_title(output: impl Into<T> + AsRawFd, width: usize, title: impl AsRef<str>) -> Option<Self>
{
let (terminal_size::Width(tw), _) = terminal_size_of(&output)?;
let tw = usize::from(tw);
let mut o = Self::with_max(output.into(), if width < tw {width} else {tw}, tw);
o.set_title(title.as_ref());
o.fit_to_term = true;
o.update();
Some(o)
}
#[inline]
fn autofit(&mut self)
{
#[cfg(feature="size")]
self.fit();
}
/// Create a new bar with max display width of our terminal
///
/// # Notes
/// Without feature `size`, will be the same as `Self::with_max(width, width +20)`
///
/// To try to create one that always adheres to `size`, use the `try_new()` family of functions.
#[cfg_attr(not(feature="size"), inline)]
pub fn new(output: impl Into<T> + AsRawFd, width: usize) -> Self
{
#[cfg(feature="size")]
return {
if let Some((terminal_size::Width(tw), _)) = terminal_size_of(&output) {
let tw = usize::from(tw);
let mut o = Self::with_max(output.into(), if width < tw {width} else {tw}, tw);
o.fit_to_term = true;
o
} else {
let mut o = Self::with_max(output.into(), width, width + DEFAULT_MAX_BORDER_SIZE);
o.fit_to_term = true;
o
}
};
#[cfg(not(feature="size"))]
return {
Self::with_max(output.into(), width, width +DEFAULT_MAX_BORDER_SIZE)
};
}
/// Attempt to create a new bar with max display width of our terminal.
///
/// If `output` is not a terminal, then `None` is returned.
#[cfg(feature="size")]
pub fn try_new(output: impl Into<T> + AsRawFd, width: usize) -> Option<Self>
{
let (terminal_size::Width(tw), _) = terminal_size_of(&output)?;
let tw = usize::from(tw);
let mut o = Self::with_max(output.into(), if width < tw {width} else {tw}, tw);
o.fit_to_term = true;
Some(o)
}
/// Attempt to create a new bar with max display width of our terminal.
///
/// If `output` is not a terminal, then `None` is returned.
#[cfg(feature="size")]
#[inline]
pub fn try_new_default_size(to: impl Into<T> + AsRawFd) -> Option<Self>
{
Self::try_new(to, DEFAULT_SIZE)
}
/// Create a bar with a max display width
///
/// # Panics
/// If `width` is larger than or equal to `max_width`.
pub fn with_max(output: impl Into<T>, width: usize, max_width: usize) -> Self
{
let mut this = Self {
width,
max_width,
progress: 0.0,
buffer: String::with_capacity(width),
title: String::with_capacity(max_width - width),
#[cfg(feature="size")]
fit_to_term: false,
output: AtomicRefCell::new(output.into())
};
this.update();
this
}
}
impl<T: ?Sized + io::Write + AsRawFd> Bar<T> {
#[inline(always)]
#[cfg(feature="size")]
fn try_get_size(&self) -> Option<(terminal_size::Width, terminal_size::Height)>
{
let b = self.output.try_borrow().ok()?;
terminal_size::terminal_size_using_fd(b.as_raw_fd())
}
/// Fit to terminal's width if possible.
///
/// # Notes
/// Available with feature `size` only.
///
/// # Returns
/// If the re-fit succeeded.
/// A `fit()` will also fail if another thread is already engaging in a display operation.
pub fn fit(&mut self) -> bool
{
#[cfg(feature="size")] {
if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size_using_fd(self.output.get_mut().as_raw_fd()) {
let tw = usize::from(tw);
self.width = if self.width < tw {self.width} else {tw};
self.update_dimensions(tw);
return true;
}
}
false
}
#[inline] fn widths(&self) -> (usize, usize)
{
#[cfg(feature="size")]
if self.fit_to_term {
if let Some((terminal_size::Width(tw), _)) = self.try_get_size() {
let tw = usize::from(tw);
let width = if self.width < tw {self.width} else {tw};
return (width, tw);
}
};
(self.width, self.max_width)
}
/// Update the buffer.
pub fn update(&mut self)
{
self.buffer.clear();
let pct = (self.progress * (self.width as f64)) as usize;
for i in 0..self.width
{
if i >= pct {
write!(self.buffer, " ").unwrap();
} else {
write!(self.buffer, "=").unwrap();
}
}
}
}
impl<T: io::Write> Bar<T> {
/// Consume the bar and complete it, regardless of progress.
pub fn complete(self) -> io::Result<()>
{
writeln!(&mut self.output.into_inner(), "")
}
}
fn ensure_eq(input: String, to: usize) -> String
{
let chars = input.chars();
if chars.count() != to {
let mut chars = input.chars();
let mut output = String::with_capacity(to);
for _ in 0..to
{
if let Some(c) = chars.next() {
write!(output, "{}", c).unwrap();
} else {
write!(output, " ").unwrap();
}
}
output
} else {
input
}
}
fn ensure_lower(input: String, to: usize) -> String
{
let chars = input.chars();
if chars.count() > to
{
let chars = input.chars();
let mut output = String::with_capacity(to);
for (i,c) in (0..).zip(chars)
{
write!(output, "{}", c).unwrap();
if to>3 && i == to-3 {
write!(output, "...").unwrap();
break;
} else if i==to {
break;
}
}
output
} else {
input
}
}
impl<T: ?Sized + io::Write + AsRawFd> Display for Bar<T>
{
fn refresh(&self)
{
let (_, max_width) = self.widths();
let temp = format!("[{}]: {:.2}%", self.buffer, self.progress * 100.00);
let title = ensure_lower(format!(" {}", self.title), max_width - temp.chars().count());
let temp = ensure_eq(format!("{}{}", temp, title), max_width);
// If another thread is writing, just abort (XXX: Is this the best way to handle it?)
//
// We acquire the lock after work allocation and computation to keep it for the shortest amount of time, this is an acceptible tradeoff since multiple threads shouldn't be calling this at once anyway.
let Ok(mut out) = self.output.try_borrow_mut() else { return };
//TODO: What to do about I/O errors?
let _ = write!(out, "\x1B[0m\x1B[K{}", temp) // XXX: For now, just abort if one fails.
.and_then(|_| write!(out, "\n\x1B[1A"))
.and_then(move |_| flush!(? out));
}
fn blank(&self)
{
let (_, max_width) = self.widths();
// If another thread is writing, just abort (XXX: Is this the best way to handle it?)
let Ok(mut out) = self.output.try_borrow_mut() else { return };
//TODO: What to do about I/O errors?
let _ = out.write_all(b"\r")
.and_then(|_| stackalloc::stackalloc(max_width, b' ',|spaces| out.write_all(spaces))) // Write `max_width` spaces (TODO: Is there a better way to do this? With no allocation? With a repeating iterator maybe?)
.and_then(|_| out.write_all(b"\r"))
.and_then(move |_| flush!(? out));
}
fn get_title(&self) -> &str
{
&self.title
}
fn set_title(&mut self, from: &str)
{
self.title = from.to_string();
let (_, max_width) = self.widths();
// self.refresh(), with exclusive access. (XXX: Maybe move this to a non-pub `&mut self` helper function)
let out = self.output.get_mut();
//TODO: What to do about I/O errors?
let _ = out.write_all(b"\r")
.and_then(|_| stackalloc::stackalloc(max_width, b' ',|spaces| out.write_all(spaces))) // Write `max_width` spaces (TODO: Is there a better way to do this? With no allocation? With a repeating iterator maybe?)
.and_then(|_| out.write_all(b"\r"))
.and_then(move |_| flush!(? out));
}
fn update_dimensions(&mut self, to: usize)
{
self.max_width = to;
// self.refresh(), with exclusive access. (XXX: Maybe move this to a non-pub `&mut self` helper function)
let out = self.output.get_mut();
//TODO: What to do about I/O errors?
let _ = out.write_all(b"\r")
.and_then(|_| stackalloc::stackalloc(to, b' ',|spaces| out.write_all(spaces))) // Write `max_width` spaces (TODO: Is there a better way to do this? With no allocation? With a repeating iterator maybe?)
.and_then(|_| out.write_all(b"\r"))
.and_then(move |_| flush!(? out));
}
}
impl<T: ?Sized + io::Write + AsRawFd> ProgressBar for Bar<T>
{
fn get_progress(&self) -> f64
{
self.progress
}
fn set_progress(&mut self, value: f64)
{
if self.progress != value {
self.progress = value;
self.update();
}
let (_, max_width) = self.widths();
// self.refresh(), with exclusive access. (XXX: Maybe move this to a non-pub `&mut self` helper function)
let out = self.output.get_mut();
//TODO: What to do about I/O errors?
let _ = out.write_all(b"\r")
.and_then(|_| stackalloc::stackalloc(max_width, b' ',|spaces| out.write_all(spaces))) // Write `max_width` spaces (TODO: Is there a better way to do this? With no allocation? With a repeating iterator maybe?)
.and_then(|_| out.write_all(b"\r"))
.and_then(move |_| flush!(? out));
}
}
impl<T: io::Write + AsRawFd> WithTitle for Bar<T>
{
fn add_title(&mut self, string: impl AsRef<str>)
{
(*self).add_title(string.as_ref())
}
fn update(&mut self)
{
self.update();
}
fn complete(self)
{
//TODO: What to do about I/O errors?
let _ = self.complete();
}
}
const _:() = {
const fn declval<T>() -> Bar<T> {
unreachable!()
}
fn take_title(_: &(impl WithTitle + ?Sized)) {}
fn take_progress(_: &(impl ProgressBar + ?Sized)) {}
fn take_display(_: &(impl Display + ?Sized)) {}
fn test()
{
#[macro_export] macro_rules! assert_is_bar {
($ty:path) => {
take_title(&declval::<$ty>());
take_progress(&declval::<$ty>());
take_display(&declval::<$ty>());
}
}
assert_is_bar!(io::Stdout);
assert_is_bar!(std::fs::File);
}
};
#[cfg(test)]
mod test
{
use super::*;
#[test]
fn rendering_blanking()
{
let mut bar = {
#[cfg(feature="size")]
let Some(bar)= Bar::try_new_default_size_default() else { return };
#[cfg(not(feature="size"))]
let bar= Bar::new_default(50);
bar
};
bar.set_progress(0.5);
bar.blank();
bar.set_progress(0.7);
bar.set_title("70 percent.");
bar.refresh();
//println!();
bar.set_progress(0.2);
bar.set_title("...");
bar.blank();
bar.complete().unwrap();
}
#[test]
fn creating_non_default_fd() {
#[cfg(feature="size")]
let Some(_): Option<Bar<std::io::Stderr>> = Bar::try_new(std::io::stderr(), super::DEFAULT_SIZE) else { return };
#[cfg(not(feature="size"))]
let _: Bar<std::io::Stderr> = Bar::new(std::io::stderr(), super::DEFAULT_SIZE);
}
}