Skip to content

Commit

Permalink
lower MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlazy1708 committed Aug 26, 2023
1 parent b7c9cf1 commit c69180d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "tqdm"
edition = "2021"
version = "0.5.1"
rust-version = "1.70.0"
rust-version = "1.60"

readme = "README.md"
license = "MIT OR Apache-2.0"
Expand All @@ -16,3 +16,4 @@ categories = ["accessibility", "command-line-utilities"]

[dependencies]
crossterm = "0.25"
lazy_static = "1.4"
43 changes: 27 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use std::ops::{Deref, DerefMut};
extern crate crossterm;
use crossterm::QueueableCommand;

#[macro_use]
extern crate lazy_static;

#[cfg(test)]
mod test;

Expand All @@ -35,7 +38,7 @@ pub use style::Style;
pub fn tqdm<Item, Iter: Iterator<Item = Item>>(iterable: Iter) -> Tqdm<Item, Iter> {
let id = ID.fetch_add(1, sync::atomic::Ordering::SeqCst);

if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
tqdm.insert(
id,
Info {
Expand Down Expand Up @@ -63,7 +66,7 @@ pub fn tqdm<Item, Iter: Iterator<Item = Item>>(iterable: Iter) -> Tqdm<Item, Ite
/// Manually refresh all progress bars

pub fn refresh() -> io::Result<()> {
if let Ok(tqdm) = bars().lock() {
if let Ok(tqdm) = BAR.lock() {
let (ncols, nrows) = size();

if tqdm.is_empty() {
Expand Down Expand Up @@ -165,7 +168,7 @@ impl<Item, Iter: Iterator<Item = Item>> Tqdm<Item, Iter> {
/// ```

pub fn desc<S: ToString>(self, desc: Option<S>) -> Self {
if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
let info = tqdm.get_mut(&self.id);
if let Some(info) = info {
info.config.desc = desc.map(|desc| desc.to_string());
Expand All @@ -188,7 +191,7 @@ impl<Item, Iter: Iterator<Item = Item>> Tqdm<Item, Iter> {
/// ```

pub fn width(self, width: Option<usize>) -> Self {
if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
let info = tqdm.get_mut(&self.id);
if let Some(info) = info {
info.config.width = width;
Expand All @@ -209,7 +212,7 @@ impl<Item, Iter: Iterator<Item = Item>> Tqdm<Item, Iter> {
/// ```

pub fn style(self, style: Style) -> Self {
if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
let info = tqdm.get_mut(&self.id);
if let Some(info) = info {
info.config.style = style;
Expand All @@ -231,7 +234,7 @@ impl<Item, Iter: Iterator<Item = Item>> Tqdm<Item, Iter> {
/// ```

pub fn clear(self, clear: bool) -> Self {
if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
let info = tqdm.get_mut(&self.id);
if let Some(info) = info {
info.config.clear = clear;
Expand All @@ -246,7 +249,7 @@ impl<Item, Iter: Iterator<Item = Item>> Tqdm<Item, Iter> {
/// Manually close the bar and unregister it

pub fn close(&mut self) -> io::Result<()> {
if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
let mut info = tqdm.remove(&self.id).unwrap();
info.nitem += self.step;

Expand All @@ -271,7 +274,7 @@ impl<Item, Iter: Iterator<Item = Item>> Iterator for Tqdm<Item, Iter> {

fn next(&mut self) -> Option<Self::Item> {
if self.next.elapsed().is_ok() {
if let Ok(mut tqdm) = bars().lock() {
if let Ok(mut tqdm) = BAR.lock() {
let info = tqdm.get_mut(&self.id).unwrap();

info.nitem += self.step;
Expand Down Expand Up @@ -341,22 +344,19 @@ impl<Item, Iter: Iterator<Item = Item>> crate::Iter<Item> for Iter {}

/* --------------------------------- STATIC --------------------------------- */

static ID: sync::atomic::AtomicUsize = sync::atomic::AtomicUsize::new(0);
static BAR: sync::OnceLock<sync::Mutex<collections::BTreeMap<usize, Info>>> = sync::OnceLock::new();
lazy_static! {
static ref ID: sync::atomic::AtomicUsize = sync::atomic::AtomicUsize::new(0);
static ref BAR: sync::Mutex<collections::BTreeMap<usize, Info>> =
sync::Mutex::new(collections::BTreeMap::new());
}

fn size<T: From<u16>>() -> (T, T) {
let (width, height) = crossterm::terminal::size().unwrap_or((80, 24));
(T::from(width), T::from(height))
}

fn bars() -> &'static sync::Mutex<collections::BTreeMap<usize, Info>> {
BAR.get_or_init(|| sync::Mutex::new(collections::BTreeMap::new()))
}

/* --------------------------------- CONFIG --------------------------------- */

#[derive(Default)]

struct Config {
desc: Option<String>,
width: Option<usize>,
Expand All @@ -365,6 +365,17 @@ struct Config {
clear: bool,
}

impl Default for Config {

Check failure on line 368 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this `impl` can be derived
fn default() -> Self {
Config {
desc: None,
width: None,
style: Style::default(),
clear: false,
}
}
}

/* ---------------------------------- INFO ---------------------------------- */

struct Info {
Expand Down
11 changes: 6 additions & 5 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
//!
//! Other styles are open for [contribution](https://github.com/mrlazy1708/tqdm/issues/1).

#[derive(Default)]

pub enum Style {
ASCII,

#[default]
Block,

Balloon,
}

impl Default for Style {
fn default() -> Self {
Style::ASCII
}
}

impl ToString for Style {
fn to_string(&self) -> String {
String::from(match self {
Expand Down

0 comments on commit c69180d

Please sign in to comment.