ml-progress 0.1.0

Single line progress indicator for terminal/console.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#![doc = include_str!(concat!(env!("OUT_DIR"), "/README-rustdocified.md"))]
#![deny(missing_docs)]
#![forbid(unsafe_code)]

use std::{
    borrow::Cow,
    error::Error as StdError,
    fmt,
    sync::Arc,
    thread::{self, JoinHandle},
    time::Duration,
};

use parking_lot::Mutex;

pub use crate::state::State;

use crate::internal::Item;

#[allow(missing_docs)]
pub mod internal;
mod macros;
mod state;

// ======================================================================
// CONST - PRIVATE

const DEFAULT_DRAW_RATE: usize = 20;
const DEFAULT_DRAW_INTERVAL: Duration =
    Duration::from_nanos(1_000_000_000 / DEFAULT_DRAW_RATE as u64);

const DEFAULT_DRAW_DELAY: Duration = Duration::from_millis(5);

const MIN_ETA_ELAPSED: Duration = Duration::from_millis(100);
const MIN_SPEED_ELAPSED: Duration = Duration::from_millis(100);

const BINARY_PREFIXES: &[&str] = &["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"];
const DECIMAL_PREFIXES: &[&str] = &["", "k", "M", "G", "T", "P", "E", "Z", "Y"];

// ======================================================================
// Error - PUBLIC

/// Represents all possible errors that can occur in this library.
#[derive(Debug, PartialEq)]
pub enum Error {
    /// Given items contain multiple `*_fill` items but at most one is allowed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::{progress, Error};
    ///
    /// assert_eq!(
    ///     progress!(10; bar_fill message_fill).err(),
    ///     Some(Error::MultipleFillItems)
    /// );
    /// ```
    MultipleFillItems,

    /// Given `total` is out-of-range of `u64`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::{progress, Error};
    ///
    /// assert_eq!(progress!(-1).err(), Some(Error::TotalIsOutOfRange));
    /// ```
    TotalIsOutOfRange,
}

// ======================================================================
// Error - IMPL DISPLAY

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::MultipleFillItems => {
                write!(f, "got multiple fill items, at most one is allowed")
            }

            Error::TotalIsOutOfRange => {
                write!(f, "total is out-of-range of `u64`")
            }
        }
    }
}

// ======================================================================
// Error - IMPL ERROR

impl StdError for Error {}

// ======================================================================
// Progress - PUBLIC

/// Progress indicator.
///
/// `Progress` is created either
/// - directly with [`progress!`] macro (if you don’t need custom configuration) or
/// - by first creating [`ProgressBuilder`] with [`progress_builder!`] macro,
///   setting custom options, and then creating `Progress` with [`build`].
///
/// `Progress` is drawn
/// - using background thread to guarantee timely updates
/// - only if terminal is detected
/// - to `STDERR` starting with `"\r"`
/// - from the moment `Progress` is created until `Progress` is finished or dropped
///
/// See crate index for [usage](crate#usage) and [examples](crate#examples).
///
/// [`build`]: crate::ProgressBuilder::build
#[derive(Clone)]
pub struct Progress {
    // This is `None` only in `Drop::drop`.
    drawer: Option<Arc<JoinHandle<()>>>,
    state: Arc<Mutex<State>>,
}

impl Progress {
    /// Finishes `Progress` with 100% completion.
    ///
    /// - Sets [`State`] of `Progress` to 100% completion.
    /// - Draws `Progress` once with additional `"\n"`
    ///   to move cursor to next line.
    /// - Finishes `Progress`, i.e. there will be no further draws.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::progress;
    ///
    /// eprintln!("Begin");
    /// let progress = progress!(10)?;
    /// progress.finish();
    /// eprintln!("End");
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    ///
    /// ```text
    /// Begin
    /// ################################################# 10/10 (0s)
    /// End
    /// ```
    pub fn finish(&self) {
        self.state.lock().finish(self.drawer.as_ref().unwrap());
    }

    /// Finishes and clears `Progress`.
    ///
    /// - Clears drawn `Progress` by overwriting with spaces + `"\r"`,
    ///   leaving cursor at start of the cleared line.
    /// - Finishes `Progress`, i.e. there will be no further draws.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::progress;
    ///
    /// eprintln!("Begin");
    /// let progress = progress!(10)?;
    /// progress.finish_and_clear();
    /// eprintln!("End");
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    ///
    /// ```text
    /// Begin
    /// End
    /// ```
    pub fn finish_and_clear(&self) {
        self.state
            .lock()
            .finish_and_clear(self.drawer.as_ref().unwrap());
    }

    /// Finishes `Progress`.
    ///
    /// - Draws `Progress` once with additional `"\n"`
    ///   to move cursor to next line.
    /// - Finishes `Progress`, i.e. there will be no further draws.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::progress;
    ///
    /// eprintln!("Begin");
    /// let progress = progress!(10)?;
    /// progress.inc(6);
    /// progress.finish_at_current_pos();
    /// eprintln!("End");
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    ///
    /// ```text
    /// Begin
    /// ##############################-------------------- 6/10 (0s)
    /// End
    /// ```
    pub fn finish_at_current_pos(&self) {
        self.state
            .lock()
            .finish_at_current_pos(self.drawer.as_ref().unwrap());
    }

    /// Increments position of `Progress`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::progress;
    ///
    /// let progress = progress!(10)?;
    /// progress.inc(6);
    /// progress.finish_at_current_pos();
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    ///
    /// ```text
    /// ##############################-------------------- 6/10 (0s)
    /// ```
    pub fn inc(&self, steps: u64) {
        self.state.lock().inc(steps, self.drawer.as_ref().unwrap());
    }

    /// Sets the message shown by item `message_fill`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::progress;
    ///
    /// let progress = progress!(10; pos "/" total " " message_fill)?;
    /// progress.inc(6);
    /// progress.message("Hello, World!");
    /// progress.finish_at_current_pos();
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    ///
    /// ```text
    /// 6/10 Hello, World!
    /// ```
    pub fn message(&self, message: impl Into<Cow<'static, str>>) {
        self.state
            .lock()
            .message(message, self.drawer.as_ref().unwrap());
    }

    /// Returns current state of `Progress`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ml_progress::progress;
    ///
    /// let progress = progress!(10)?;
    /// progress.inc(6);
    /// assert_eq!(progress.state().lock().pos(), 6);
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    pub fn state(&self) -> &Arc<Mutex<State>> {
        &self.state
    }
}

impl Drop for Progress {
    fn drop(&mut self) {
        if let Ok(drawer) = Arc::try_unwrap(self.drawer.take().unwrap()) {
            let mut state = self.state.lock();
            if !state.is_finished() {
                state.finish_quietly(&drawer);
            }
            drop(state);
            let _ = drawer.join();
        }
    }
}

// ======================================================================
// Progress - CRATE

impl Progress {
    pub(crate) fn new(state: State) -> Self {
        let state = Arc::new(Mutex::new(state));

        let drawer = thread::spawn({
            let state = state.clone();
            move || loop {
                let mut state = state.lock();

                if state.is_finished() {
                    break;
                }

                let timeout = match state.try_draw() {
                    Ok(()) => None,
                    Err(timeout) => timeout,
                };

                drop(state);

                // NOTE: These may wake spuriously
                if let Some(timeout) = timeout {
                    thread::park_timeout(timeout);
                } else {
                    thread::park();
                }
            }
        });

        Self {
            drawer: Some(Arc::new(drawer)),
            state,
        }
    }
}

// ======================================================================
// ProgressBuilder - PUBLIC

/// A builder to create [`Progress`] with custom configuration.
///
/// See [custom configuration] for an example.
///
/// [custom configuration]: crate#custom-configuration
pub struct ProgressBuilder {
    total: Result<Option<u64>, Error>,
    pre_inc: bool,
    thousands_separator: String,
    items: Vec<Item>,
}

impl ProgressBuilder {
    /// Creates [`Progress`] using configuration of this `ProgressBuilder`.
    ///
    /// See [custom configuration] for an example.
    ///
    /// [custom configuration]: crate#custom-configuration
    pub fn build(self) -> Result<Progress, Error> {
        let state = State::new(
            self.total?,
            self.pre_inc,
            self.thousands_separator,
            self.items,
        )?;

        Ok(Progress::new(state))
    }

    /// Creates `ProgressBuilder` to configure [`Progress`].
    ///
    /// If `items` is empty then default items are used instead.
    ///
    /// [`progress_builder!`] macro should be used instead of this,
    /// which is same as `ProgressBuilder::new(items!(ITEMS))`.
    pub fn new(items: Vec<Item>) -> Self {
        let items = if items.is_empty() {
            // DEFAULT ITEMS
            items!(bar_fill " " pos "/" total " (" eta ")")
        } else {
            items
        };

        Self {
            total: Ok(None),
            pre_inc: false,
            thousands_separator: " ".to_owned(),
            items,
        }
    }

    /// Sets increment mode to `PreInc`.
    ///
    /// Increment mode can be `PostInc` (default) or `PreInc`.
    ///
    /// - `PostInc` means that progress position is
    ///   incremented after the associated work.
    ///     - For example incrementing position from 2 to 3 means that work
    ///       of step 3 has been completed and work of step 4 is about to begin.
    /// - `PreInc` means that progress position is
    ///   incremented before the associated work.
    ///     - For example incrementing position from 2 to 3 means that work
    ///       of step 2 has been completed and work of step 3 is about to begin.
    ///
    /// # Examples
    ///
    /// Here first step has been completed and second is about to begin
    /// so completion percentage is 33%.
    ///
    /// ```rust
    /// use ml_progress::progress_builder;
    ///
    /// let progress = progress_builder!("[" percent "] " pos "/" total)
    ///     .total(Some(3))
    ///     .pre_inc()
    ///     .build()?;
    /// progress.inc(1);
    /// progress.inc(1);
    /// progress.finish_at_current_pos();
    /// # Ok::<(), ml_progress::Error>(())
    /// ```
    ///
    /// ```text
    /// [ 33%] 2/3
    /// ```
    pub fn pre_inc(self) -> Self {
        Self {
            pre_inc: true,
            ..self
        }
    }

    /// Sets thousands separator, default is space.
    ///
    /// See [custom configuration] for an example.
    ///
    /// [custom configuration]: crate#custom-configuration
    pub fn thousands_separator(self, separator: &str) -> Self {
        Self {
            thousands_separator: separator.to_owned(),
            ..self
        }
    }

    /// Sets progress total, default is `None`.
    ///
    /// See [custom configuration] for an example.
    ///
    /// [custom configuration]: crate#custom-configuration
    pub fn total<T: TryInto<u64>>(self, total: Option<T>) -> Self {
        let total = if let Some(total) = total {
            match total.try_into() {
                Ok(total) => Ok(Some(total)),
                Err(_) => Err(Error::TotalIsOutOfRange),
            }
        } else {
            Ok(None)
        };

        Self { total, ..self }
    }
}

// ======================================================================
// FUNCTIONS - PUBLIC

/// Returns given value as binary prefix with corresponding value.
///
/// Uses 1024-based prefixes `Ki`, `Mi`, `Gi`, ..., `Yi`.
///
/// # Examples
///
/// ```rust
/// assert_eq!(ml_progress::binary_prefix(2048.0), (2.0, "Ki"));
/// ```
pub fn binary_prefix(mut value: f64) -> (f64, &'static str) {
    let mut scale = 0;
    while value.abs() >= 1024.0 && scale < BINARY_PREFIXES.len() - 1 {
        value /= 1024.0;
        scale += 1;
    }
    (value, BINARY_PREFIXES[scale])
}

/// Returns given value as decimal prefix with corresponding value.
///
/// Uses 1000-based prefixes `k`, `M`, `G`, ..., `Y`.
///
/// # Examples
///
/// ```rust
/// assert_eq!(ml_progress::decimal_prefix(2000.0), (2.0, "k"));
/// ```
pub fn decimal_prefix(mut value: f64) -> (f64, &'static str) {
    let mut scale = 0;
    while value.abs() >= 1000.0 && scale < DECIMAL_PREFIXES.len() - 1 {
        value /= 1000.0;
        scale += 1;
    }
    (value, DECIMAL_PREFIXES[scale])
}

/// Returns given duration in approximate format: amount and unit.
///
/// - Amount is the number of full units, i.e. it's not rounded.
/// - Unit can be `h` (hours), `m` (minutes) or `s` (seconds)
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// assert_eq!(ml_progress::duration_approx(Duration::from_secs(234)), (3, "m"));
/// ```
pub fn duration_approx(duration: Duration) -> (u64, &'static str) {
    let secs = duration.as_secs();
    if secs < 60 {
        (secs, "s")
    } else if secs < 3600 {
        (secs / 60, "m")
    } else {
        (secs / 3600, "h")
    }
}

/// Returns given duration as hours, minutes and seconds.
///
/// Returned value is the number of full seconds, i.e. it's not rounded.
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// assert_eq!(ml_progress::duration_hms(Duration::from_secs(234)), (0, 3, 54));
/// ```
pub fn duration_hms(duration: Duration) -> (u64, u64, u64) {
    let secs = duration.as_secs();
    let h = secs / 3600;
    let m = (secs % 3600) / 60;
    let s = secs % 60;
    (h, m, s)
}

/// Formats integer with digits in groups of three.
///
/// # Examples
///
/// ```rust
/// assert_eq!(ml_progress::group_digits(12345, " "), "12 345");
/// ```
pub fn group_digits(mut value: u64, separator: &str) -> String {
    // `u64` can have at most 7 3-digit groups
    let mut groups = [0; 7];
    let mut pos = 0;
    while pos == 0 || value > 0 {
        groups[pos] = value % 1000;
        value /= 1000;
        pos += 1;
    }

    let mut result = String::with_capacity(pos * 3 + (pos - 1) * separator.len());
    pos -= 1;
    result.push_str(&format!("{}", groups[pos]));
    while pos > 0 {
        pos -= 1;
        result.push_str(separator);
        result.push_str(&format!("{:03}", groups[pos]));
    }
    result
}

// ======================================================================
// TESTS

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================================
    // binary_prefix

    #[test]
    fn binary_prefix_misc() {
        assert_eq!(binary_prefix(0.0), (0.0, ""));

        assert_eq!(binary_prefix(2560.0), (2.5, "Ki"));
        assert_eq!(binary_prefix(2621440.0), (2.5, "Mi"));

        assert_eq!(binary_prefix(-2560.0), (-2.5, "Ki"));
        assert_eq!(binary_prefix(-2621440.0), (-2.5, "Mi"));
    }

    #[test]
    fn binary_prefix_overflow() {
        assert_eq!(binary_prefix(91.0f64.exp2()), (2048.0, "Yi"));
    }

    // ============================================================
    // decimal_prefix

    #[test]
    fn decimal_prefix_misc() {
        assert_eq!(decimal_prefix(0.0), (0.0, ""));

        assert_eq!(decimal_prefix(2500.0), (2.5, "k"));
        assert_eq!(decimal_prefix(2500000.0), (2.5, "M"));

        assert_eq!(decimal_prefix(-2500.0), (-2.5, "k"));
        assert_eq!(decimal_prefix(-2500000.0), (-2.5, "M"));
    }

    #[test]
    fn decimal_prefix_overflow() {
        // TODO: This shouldn't use exact comparison.
        assert_eq!(decimal_prefix(2.0e27), (2000.0, "Y"));
    }

    // ============================================================
    // duration_approx

    #[test]
    fn duration_approx_no_rounding() {
        assert_eq!(duration_approx(Duration::from_millis(1800)), (1, "s"));
    }

    // ============================================================
    // duration_hms

    #[test]
    fn duration_hms_no_rounding() {
        assert_eq!(duration_hms(Duration::from_millis(1800)), (0, 0, 1));
    }

    // ============================================================
    // group_digits

    #[test]
    fn group_digits_0() {
        assert_eq!(group_digits(0, " "), "0");
    }

    #[test]
    fn group_digits_max() {
        assert_eq!(group_digits(u64::MAX, " "), "18 446 744 073 709 551 615");
    }

    #[test]
    fn group_digits_long_separator() {
        assert_eq!(group_digits(12_345, "abc"), "12abc345");
    }

    #[test]
    fn group_digits_has_zero_padding() {
        assert_eq!(group_digits(1_002_034, " "), "1 002 034");
    }

    #[test]
    fn group_digits_no_zero_padding() {
        assert_eq!(group_digits(1_234_567, " "), "1 234 567");
    }
}