From 10742e1cace460e1f612036c74b56b751c4335b7 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 25 Jul 2020 22:07:50 +0100 Subject: [PATCH] changed to optional dependancy --- Cargo.toml | 9 +++++++-- README.md | 7 ++++++- src/progress.rs | 26 ++++++++++++++++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 950815e..3863565 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,11 +2,16 @@ name = "termprogress" description = "A terminal progress bar renderer with status and spinners" license = "GPL-3.0-or-later" -version = "0.1.1" +version = "0.1.2" authors = ["Avril "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["size"] + +size = ["terminal_size"] + [dependencies] -terminal_size = "0.1" \ No newline at end of file +terminal_size = {version = "0.1", optional = true} \ No newline at end of file diff --git a/README.md b/README.md index 2cb05f9..14345f4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Simple and customiseable terminal progress bars for Rust. ## Features - Customiseable, has a traits system to allow for passing any type of progress bar around - - Prevents long titles from overflowing the terminal by using the [terminal_size][terminal-size] crate + - Optionally prevents long titles from overflowing the terminal by using the [terminal_size][terminal-size] crate - Interfaces for easily manipulating bar [terminal-size]: https://crates.io/crates/terminal_size @@ -66,6 +66,11 @@ spinner.bump(); progress.complete_with("Done!"); ``` +## Default features + +By default, the `size` feature is enabled, which requires the dependency [`terminal_size`][terminal-size]. +Without this, `Bar` will not attempt to get the terminal's size to prevent overflows. You can disable it with `default-features=false`. + ## Traits The library comes with traits for progress bars: [`ProgressBar`][progress-bar], and [`Spinner`][spinner]. diff --git a/src/progress.rs b/src/progress.rs index ffc015e..8d888d4 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -50,14 +50,25 @@ impl Bar this } /// 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)` + #[cfg_attr(not(feature="size"), inline)] pub fn new(width: usize) -> Self { - if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() { - let tw = usize::from(tw); - Self::with_max(if width < tw {width} else {tw}, tw) - } else { + #[cfg(feature="size")] + return { + if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() { + let tw = usize::from(tw); + Self::with_max(if width < tw {width} else {tw}, tw) + } else { + Self::with_max(width, width +20) + } + }; + #[cfg(not(feature="size"))] + return { Self::with_max(width, width +20) - } + }; } /// Create a bar with a max display width /// @@ -77,9 +88,12 @@ impl Bar } /// Fit to terminal's width if possible. + /// + /// # Notes + /// Available with feature `size` only. pub fn fit(&mut self) -> bool { - + #[cfg(feature="size")] if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() { let tw = usize::from(tw); self.width = if self.width < tw {self.width} else {tw};