Feature `size`: Added `Bar::try_new_*()` functions

Bar: Changed default size(s) to constants instead of magic numbers.

Fortune for termprogress's current commit: Half curse − 半凶
master
Avril 1 year ago
parent 16df2d9cdf
commit 678483acc1
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -11,8 +11,12 @@ edition = "2018"
[features] [features]
default = ["size"] default = ["size"]
# Use terminal size when drawing bars.
size = ["terminal_size"] size = ["terminal_size"]
# React to `SIGWINCH` when `stdout` is a terminal (linux only.)
reactive = []
[dependencies] [dependencies]
terminal_size = {version = "0.1", optional = true} terminal_size = {version = "0.1", optional = true}
[build-dependencies] [build-dependencies]

@ -11,6 +11,9 @@ macro_rules! flush {
} }
} }
#[cfg(feature="size")]
use std::os::unix::io::*;
mod util; mod util;
mod inter; mod inter;
pub use inter::*; pub use inter::*;
@ -20,6 +23,24 @@ pub mod wheel;
pub mod spinner; pub mod spinner;
pub mod silent; pub mod silent;
/// Returns true if `stdout` has a terminal output and can be used with terminal size responsiveness.
///
/// Requires `size` feature.
#[cfg(feature="size")]
pub fn has_terminal_output_default() -> bool
{
terminal_size::terminal_size().is_some()
}
/// Returns true if `f` has a terminal output and can be used with terminal size responsiveness.
///
/// Requires `size` feature.
#[cfg(feature="size")]
pub fn has_terminal_output(f: &(impl AsRawFd + ?Sized)) -> bool
{
terminal_size::terminal_size_using_fd(f.as_raw_fd()).is_some()
}
/// The prelude exposes the traits for spinners and progress bars, and the `spinner::Spin` and `progress::Bar` types for easy access and use. /// The prelude exposes the traits for spinners and progress bars, and the `spinner::Spin` and `progress::Bar` types for easy access and use.
pub mod prelude { pub mod prelude {
pub use super::inter::*; pub use super::inter::*;

@ -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
@ -52,6 +63,21 @@ impl Bar
this.update(); this.update();
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

Loading…
Cancel
Save