Skip to content

Commit

Permalink
Merge pull request #15 from aaarkid/0.2.2
Browse files Browse the repository at this point in the history
0.2.2
  • Loading branch information
aaarkid authored Aug 20, 2022
2 parents 0c16018 + e3a0dd5 commit 50a6176
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eta"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
authors = ["Arkid Kaleci <akaleci@jacobs-university.de>"]
description = "Tracks progress on repetive tasks and measures estimated remaining times."
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<p align="center">
Rust library for tracking progress on repetive tasks and measuring estimated remaining times.
<br />
<a href="https://docs.rs/eta/0.2.1/eta/"><strong>Explore the docs »</strong></a>
<a href="https://docs.rs/eta/latest/eta/"><strong>Explore the docs »</strong></a>
<br />
<br />
<a href="https://github.com/aaarkid/eta">View Demo</a>
Expand Down Expand Up @@ -71,7 +71,7 @@ ETA aims to be a simple, easy to use, and efficient library for tracking progres
Add this to your `Cargo.toml`:
```rust
[dependencies]
eta = "0.2.1"
eta = "0.2.2"
```

Add this to your source code:
Expand Down
50 changes: 33 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
#![doc(html_favicon_url = "https://github.com/aaarkid/eta/blob/master/images/favicon.png")]
#![doc(html_logo_url = "https://github.com/aaarkid/eta/blob/master/images/logo.png")]
#![doc(html_favicon_url = "https://raw.githubusercontent.com/aaarkid/eta/master/images/favicon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/aaarkid/eta/master/images/logo.png")]

#![warn(missing_docs)]

//!Tracking progress on repetive tasks and measuring remaining times.
Expand All @@ -9,7 +10,7 @@
//! Add this to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! eta = "0.2.1"
//! eta = "0.2.2"
//! ```
//! and this to your source code:
//! ```rust
Expand Down Expand Up @@ -49,22 +50,22 @@ pub struct Eta {
tasks_count: usize,
tasks_done: usize,
recent_time: Instant,
total_time_elapsed: usize,
time_elapsed: usize,
time_accuracy: TimeAcc,
time_paused: usize,
paused: bool
}

#[derive(Clone, Debug, PartialEq, Eq)]
///`TimeAcc` is an enum for keeping track of the grade of accuracy for any information regarding time.
///`TimeAcc` determines the accuracy of the time measurement.
pub enum TimeAcc {
///SEC stands for seconds. 1 seconds has 1000 milliseconds.
///SEC stands for seconds. This will be displayed in minutes and seconds.
SEC,
///MILLI stands for milliseconds.
///MILLI stands for milliseconds. This will be displayed in seconds.
MILLI,
///MICRO stands for microseconds. 1,000 microseconds make up 1 millisecond
///MICRO stands for microseconds. This will be displayed in milliseconds.
MICRO,
///NANO stands for nanoseconds. 1,000,000 nanoseconds make up 1 millisecond.
///NANO stands for nanoseconds. This will be displayed in microseconds.
NANO
}

Expand All @@ -74,7 +75,7 @@ impl Eta {
tasks_count,
tasks_done,
recent_time: Instant::now(),
total_time_elapsed: 0,
time_elapsed: 0,
time_accuracy,
time_paused: 0,
paused: false,
Expand Down Expand Up @@ -118,7 +119,7 @@ impl Eta {
pub fn pause(&mut self) {
if !self.paused {
self.paused = true;
self.time_paused += self.elapsed();
self.time_paused += self.step_elapsed();
}
}

Expand Down Expand Up @@ -165,15 +166,15 @@ impl Eta {
pub fn step(&mut self) {
self.tasks_done += 1;
if !self.paused {
self.total_time_elapsed += self.elapsed();
self.time_elapsed += self.step_elapsed();
}
self.total_time_elapsed += self.time_paused;
self.time_elapsed += self.time_paused;
self.recent_time = Instant::now();
self.paused = false;
self.time_paused = 0;
}

fn elapsed(&self) -> usize {
fn step_elapsed(&self) -> usize {
match self.time_accuracy {
TimeAcc::SEC => self.recent_time.elapsed().as_secs() as usize,
TimeAcc::MILLI => self.recent_time.elapsed().as_millis() as usize,
Expand Down Expand Up @@ -218,15 +219,30 @@ impl Eta {
/// # }
/// ```
pub fn time_remaining(&self) -> usize {
((self.tasks_count - self.tasks_done) as f64 * (self.total_time_elapsed as f64) / (self.tasks_done as f64))
((self.tasks_count - self.tasks_done) as f64 * (self.time_elapsed as f64) / (self.tasks_done as f64))
as usize
}
}

fn minutes_format (time: usize) -> String {
if time < 60 {
format!("{}s", time)
} else if time % 60 == 0 {
format!("{}m", time / 60)
} else {
format!("{}m {}s", time / 60, time % 60)
}
}

#[doc(hidden)]
impl std::fmt::Display for Eta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}: {}% ({}{} remaining)", self.tasks_done, self.tasks_count, self.progress()*100.0, self.time_remaining(), self.time_accuracy)
write!(f, "{}/{}: {}% ({} remaining)", self.tasks_done, self.tasks_count, (self.progress()*100.0).round(), match self.time_accuracy {
TimeAcc::SEC => minutes_format(self.time_remaining()),
TimeAcc::MILLI => format!("{}s", self.time_remaining() / 1000),
TimeAcc::MICRO => format!("{}ms", self.time_remaining() / 1000),
TimeAcc::NANO => format!("{}µs", self.time_remaining() / 1000)
})
}
}

Expand All @@ -236,7 +252,7 @@ impl std::fmt::Display for TimeAcc {
match self {
TimeAcc::SEC => write!(f, "s"),
TimeAcc::MILLI => write!(f, "ms"),
TimeAcc::MICRO => write!(f, "us"),
TimeAcc::MICRO => write!(f, "µs"),
TimeAcc::NANO => write!(f, "ns"),
}
}
Expand Down
39 changes: 27 additions & 12 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod tests {
assert_eq!(eta.tasks_count, 10);
assert_eq!(eta.tasks_done, 0);
assert_eq!(eta.time_accuracy, TimeAcc::SEC);
assert_eq!(eta.total_time_elapsed, 0);
assert_eq!(eta.time_elapsed, 0);
assert_eq!(eta.paused, false);
assert_eq!(eta.time_paused, 0);
}
Expand All @@ -29,18 +29,18 @@ mod tests {
let eta3 = Eta::new(10, TimeAcc::MICRO);
let eta4 = Eta::new(10, TimeAcc::NANO);
sleep(Duration::from_secs(1));
assert!(eta1.elapsed() > 0);
assert!(eta2.elapsed() > 0);
assert!(eta3.elapsed() > 0);
assert!(eta4.elapsed() > 0);
assert!(eta1.step_elapsed() > 0);
assert!(eta2.step_elapsed() > 0);
assert!(eta3.step_elapsed() > 0);
assert!(eta4.step_elapsed() > 0);
}

#[test]
fn test_eta_total_elapsed_time() {
let mut eta = Eta::new(10, TimeAcc::SEC);
sleep(Duration::from_secs(1));
eta.step();
assert!(eta.total_time_elapsed > 0);
assert!(eta.time_elapsed > 0);
}

#[test]
Expand Down Expand Up @@ -80,7 +80,7 @@ mod tests {
sleep(Duration::from_secs(1));
eta.resume();
eta.step();
assert_eq!(eta.total_time_elapsed, 0);
assert_eq!(eta.time_elapsed, 0);
}

#[test]
Expand All @@ -94,22 +94,37 @@ mod tests {
eta.pause();
sleep(Duration::from_secs(1));
eta.step();
assert_eq!(eta.total_time_elapsed, 2);
assert_eq!(eta.time_elapsed, 2);
}

#[test]
fn test_timeacc_display() {
assert_eq!(format!("{}", TimeAcc::SEC), "s");
assert_eq!(format!("{}", TimeAcc::MILLI), "ms");
assert_eq!(format!("{}", TimeAcc::MICRO), "us");
assert_eq!(format!("{}", TimeAcc::MICRO), "µs");
assert_eq!(format!("{}", TimeAcc::NANO), "ns");
}

#[test]
fn test_eta_display() {
let mut eta = Eta::new(10, TimeAcc::SEC);
let mut eta1 = Eta::new(10, TimeAcc::SEC);
let mut eta2 = Eta::new(100, TimeAcc::SEC);
let mut eta3 = Eta::new(61, TimeAcc::SEC);
let mut eta4 = Eta::new(1, TimeAcc::MILLI);
let mut eta5 = Eta::new(1, TimeAcc::MICRO);
let mut eta6 = Eta::new(1, TimeAcc::NANO);
sleep(Duration::from_secs(1));
eta.step();
assert_eq!(eta.to_string(), "1/10: 10% (9s remaining)".to_owned());
eta1.step();
eta2.step();
eta3.step();
eta4.step();
eta5.step();
eta6.step();
assert_eq!(eta1.to_string(), "1/10: 10% (9s remaining)".to_owned());
assert_eq!(eta2.to_string(), "1/100: 1% (1m 39s remaining)".to_owned());
assert_eq!(eta3.to_string(), "1/61: 2% (1m remaining)".to_owned());
assert_eq!(eta4.to_string(), "1/1: 100% (0s remaining)".to_owned());
assert_eq!(eta5.to_string(), "1/1: 100% (0ms remaining)".to_owned());
assert_eq!(eta6.to_string(), "1/1: 100% (0µs remaining)".to_owned());
}
}

0 comments on commit 50a6176

Please sign in to comment.