Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change rate limits/refresh rate to floats #487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/draw_target.rs
Expand Up @@ -38,14 +38,14 @@ impl ProgressDrawTarget {
/// Draw to a buffered stdout terminal at a max of `refresh_rate` times a second.
///
/// For more information see `ProgressDrawTarget::to_term`.
pub fn stdout_with_hz(refresh_rate: u8) -> ProgressDrawTarget {
pub fn stdout_with_hz<T: Into<f64>>(refresh_rate: T) -> ProgressDrawTarget {
ProgressDrawTarget::term(Term::buffered_stdout(), refresh_rate)
}

/// Draw to a buffered stderr terminal at a max of `refresh_rate` times a second.
///
/// For more information see `ProgressDrawTarget::to_term`.
pub fn stderr_with_hz(refresh_rate: u8) -> ProgressDrawTarget {
pub fn stderr_with_hz<T: Into<f64>>(refresh_rate: T) -> ProgressDrawTarget {
ProgressDrawTarget::term(Term::buffered_stderr(), refresh_rate)
}

Expand All @@ -63,7 +63,7 @@ impl ProgressDrawTarget {
/// useless escape codes in that file.
///
/// Will panic if refresh_rate is `Some(0)`. To disable rate limiting use `None` instead.
pub fn term(term: Term, refresh_rate: u8) -> ProgressDrawTarget {
pub fn term<T: Into<f64>>(term: Term, refresh_rate: T) -> ProgressDrawTarget {
ProgressDrawTarget {
kind: TargetKind::Term {
term,
Expand Down Expand Up @@ -378,9 +378,10 @@ struct RateLimiter {

/// Rate limit but allow occasional bursts above desired rate
impl RateLimiter {
fn new(rate: u8) -> Self {
/// New rate limiter with `rate` Hz limit
fn new<T: Into<f64>>(rate: T) -> Self {
Self {
interval: 1000 / (rate as u16), // between 3 and 1000 milliseconds
interval: (1000.0 / rate.into()).round() as u16,
capacity: MAX_BURST,
prev: Instant::now(),
}
Expand Down