|
|
@ -1,8 +1,9 @@
|
|
|
|
//! Progress bar that has a size and also a max size.
|
|
|
|
///! Progress bar that has a size and also a max size.
|
|
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
fmt::Write,
|
|
|
|
fmt::Write,
|
|
|
|
|
|
|
|
io,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// A progress bar with a size and optionally title. It implements the `ProgressBar` trait, and is the default progress bar.
|
|
|
|
/// A progress bar with a size and optionally title. It implements the `ProgressBar` trait, and is the default progress bar.
|
|
|
@ -23,7 +24,7 @@ use std::{
|
|
|
|
/// It renders in the terminal like:
|
|
|
|
/// It renders in the terminal like:
|
|
|
|
/// `[========================= ]: 50% this is a title that may get cut if it reaches max le...`
|
|
|
|
/// `[========================= ]: 50% this is a title that may get cut if it reaches max le...`
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Bar
|
|
|
|
pub struct Bar/*<T: ?Sized = io::Stdout> //TODO: Implement this after try_new(), WINCH, etc*/
|
|
|
|
{
|
|
|
|
{
|
|
|
|
width: usize,
|
|
|
|
width: usize,
|
|
|
|
max_width: usize,
|
|
|
|
max_width: usize,
|
|
|
@ -32,17 +33,27 @@ pub struct Bar
|
|
|
|
title: String,
|
|
|
|
title: String,
|
|
|
|
#[cfg(feature="size")]
|
|
|
|
#[cfg(feature="size")]
|
|
|
|
fit_to_term: bool,
|
|
|
|
fit_to_term: bool,
|
|
|
|
|
|
|
|
//output_to: T
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for Bar
|
|
|
|
/// 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>*/ Default for Bar/*<T>*/
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self
|
|
|
|
fn default() -> Self
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Self::new(50)
|
|
|
|
Self::new(DEFAULT_SIZE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Bar
|
|
|
|
impl/*<T: io::Write>*/ Bar/*<T>*/
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// Create a new bar `width` long with a title.
|
|
|
|
/// Create a new bar `width` long with a title.
|
|
|
|
pub fn with_title(width: usize, title: impl AsRef<str>) -> Self
|
|
|
|
pub fn with_title(width: usize, title: impl AsRef<str>) -> Self
|
|
|
@ -53,6 +64,21 @@ impl Bar
|
|
|
|
this
|
|
|
|
this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 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")]
|
|
|
|
|
|
|
|
pub fn try_new_with_title(width: usize, title: impl AsRef<str>) -> Option<Self>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let (terminal_size::Width(tw), _) = terminal_size::terminal_size()?;
|
|
|
|
|
|
|
|
let tw = usize::from(tw);
|
|
|
|
|
|
|
|
let mut o = Self::with_max(if width < tw {width} else {tw}, tw);
|
|
|
|
|
|
|
|
o.set_title(title.as_ref());
|
|
|
|
|
|
|
|
o.fit_to_term = true;
|
|
|
|
|
|
|
|
o.update();
|
|
|
|
|
|
|
|
Some(o)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
fn autofit(&mut self)
|
|
|
|
fn autofit(&mut self)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -64,6 +90,8 @@ impl Bar
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Notes
|
|
|
|
/// # Notes
|
|
|
|
/// Without feature `size`, will be the same as `Self::with_max(width, width +20)`
|
|
|
|
/// 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)]
|
|
|
|
#[cfg_attr(not(feature="size"), inline)]
|
|
|
|
pub fn new(width: usize) -> Self
|
|
|
|
pub fn new(width: usize) -> Self
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -75,16 +103,40 @@ impl Bar
|
|
|
|
o.fit_to_term = true;
|
|
|
|
o.fit_to_term = true;
|
|
|
|
o
|
|
|
|
o
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
let mut o = Self::with_max(width, width +20);
|
|
|
|
let mut o = Self::with_max(width, width + DEFAULT_MAX_BORDER_SIZE);
|
|
|
|
o.fit_to_term = true;
|
|
|
|
o.fit_to_term = true;
|
|
|
|
o
|
|
|
|
o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
#[cfg(not(feature="size"))]
|
|
|
|
#[cfg(not(feature="size"))]
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
Self::with_max(width, width +20)
|
|
|
|
Self::with_max(width, width +DEFAULT_MAX_BORDER_SIZE)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 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")]
|
|
|
|
|
|
|
|
pub fn try_new(width: usize) -> Option<Self>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let (terminal_size::Width(tw), _) = terminal_size::terminal_size()?;
|
|
|
|
|
|
|
|
let tw = usize::from(tw);
|
|
|
|
|
|
|
|
let mut o = Self::with_max(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 `stdout` is not a terminal, then `None` is returned.
|
|
|
|
|
|
|
|
#[cfg(feature="size")]
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
pub fn try_new_default_size() -> Option<Self>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Self::try_new(DEFAULT_SIZE)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a bar with a max display width
|
|
|
|
/// Create a bar with a max display width
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// # Panics
|
|
|
|