kyuri 0.2.6

A very simple progress display library
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! A simple progress display library.
//!
//! Kyuri is a simple progress display library. Different from [indicatif](https://github.com/console-rs/indicatif), it:
//! - Depends on std only when terminal support is unnecessary.
//!   - Custom features `console_width` and `unicode` are available for ANSI mode terminal width detection and Unicode width calculation.
//! - The `Manager` (like `MultiProgress` in indicatif) manages all progress bar management and rendering.
//! - Friendly to writing to files.
//! - Predictable about when it would draw.
//! - Custom integrations with other libraries (an example: examples/tracing.rs)
//!
//! ## Examples
//!
//! ```
//! use kyuri::Manager;
//!
//! const TEMPLATE: &str = "{msg}: {bar} ({pos}/{len})";
//! let manager = Manager::new(std::time::Duration::from_secs(1));
//!
//! let bar = manager.create_bar(100, "Processing", TEMPLATE, true);
//! for i in 0..=100 {
//!     bar.set_pos(i);
//!     std::thread::sleep(std::time::Duration::from_millis(1));
//! }
//! bar.finish_and_drop();
//! ```
//!
//! ## Template
//!
//! The template in Kyuri looks like the one in indicatif. However, only a very small subset is implemented, and some have different meanings.
//!
//! Tags in template looks like `{something}`. Supported tags:
//! - `{msg}`, `{message}`: The message of the bar.
//! - `{elapsed}`, `{elapsed_precise}`: The elapsed time (H:MM:SS).
//! - `{bytes}`: The current position in bytes (power-of-two, `KiB`, `MiB`, ...).
//! - `{pos}`: The current position.
//! - `{total_bytes}`: The total length in bytes (power-of-two, `KiB`, `MiB`, ...).
//! - `{total}`, `{len}`: The total length.
//! - `{bytes_per_sec}`, `{bytes_per_second}`: The current speed in bytes per second.
//! - `{eta}`: The estimated time of arrival (H:MM:SS).
//! - `{bar}`, `{barNUM}`: The progress bar. The `NUM` is the size of the bar, default is 20.
//! - `{state_emoji}`: The state emoji of the bar. ✅ for finished, 🆕 for new, 💥 for overflowed, ⏳ for in progress.
//!
//! Doubled `{` and `}` would not be interpreted as tags.

#![warn(missing_docs)]

use std::{
    collections::BTreeMap,
    sync::{
        atomic::{AtomicBool, AtomicUsize},
        Arc, Mutex, Weak,
    },
};

mod template;
mod ticker;
pub mod writer;
use template::{Template, TemplatePart};
use termsize::get_width;
use ticker::Ticker;
mod termsize;

const CLEAR_ANSI: &str = "\r\x1b[K";
const UP_ANSI: &str = "\x1b[F";

pub(crate) struct BarState {
    len: u64,
    pos: u64,
    message: String,
    template: Template,
    created_at: std::time::Instant,
    visible: bool,
    /// Note that need_redraw for individual bars would only be respected when output is not a terminal.
    need_redraw: bool,
}

fn duration_to_human(duration: std::time::Duration) -> String {
    let elapsed = duration.as_secs();
    let hours = elapsed / 3600;
    let minutes = (elapsed % 3600) / 60;
    let seconds = elapsed % 60;
    format!("{}:{:02}:{:02}", hours, minutes, seconds)
}

fn bytes_to_human(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;
    const TB: u64 = GB * 1024;

    if bytes < KB {
        format!("{} B", bytes)
    } else if bytes < MB {
        format!("{:.2} KiB", bytes as f64 / KB as f64)
    } else if bytes < GB {
        format!("{:.2} MiB", bytes as f64 / MB as f64)
    } else if bytes < TB {
        format!("{:.2} GiB", bytes as f64 / GB as f64)
    } else {
        format!("{:.2} TiB", bytes as f64 / TB as f64)
    }
}

fn string_width(s: &str) -> usize {
    #[cfg(feature = "unicode")]
    {
        unicode_width::UnicodeWidthStr::width(s)
    }

    #[cfg(not(feature = "unicode"))]
    {
        s.chars().count()
    }
}

impl BarState {
    pub fn render(&self) -> String {
        let mut result = String::new();
        let elapsed = std::time::Instant::now() - self.created_at;
        let bytes_per_second = self.pos as f64 / elapsed.as_secs_f64();
        for part in self.template.parts.iter() {
            match part {
                TemplatePart::Text(text) => {
                    result.push_str(text);
                }
                TemplatePart::Newline => {
                    result.push('\n');
                }
                TemplatePart::Message => {
                    result.push_str(&self.message);
                }
                TemplatePart::Elapsed => {
                    result.push_str(&duration_to_human(elapsed));
                }
                TemplatePart::Bytes => {
                    result.push_str(&bytes_to_human(self.pos));
                }
                TemplatePart::Pos => {
                    result.push_str(&self.pos.to_string());
                }
                TemplatePart::TotalBytes => {
                    result.push_str(&bytes_to_human(self.len));
                }
                TemplatePart::Total => {
                    result.push_str(&self.len.to_string());
                }
                TemplatePart::BytesPerSecond => {
                    result.push_str(&format!("{}/s", bytes_to_human(bytes_per_second as u64)));
                }
                TemplatePart::Eta => {
                    if self.pos == 0 {
                        result.push_str("Unknown");
                    } else {
                        let eta = (self.len - self.pos) as f64 / bytes_per_second;
                        result.push_str(&duration_to_human(std::time::Duration::from_secs(
                            eta as u64,
                        )));
                    }
                }
                TemplatePart::Bar(size) => {
                    let filled = (self.pos as f64 / self.len as f64 * *size as f64) as usize;
                    if *size >= filled {
                        let empty = *size - filled;
                        result.push('[');
                        for _ in 0..filled {
                            result.push('=');
                        }
                        for _ in 0..empty {
                            result.push(' ');
                        }
                        result.push(']');
                    } else {
                        let overflowed = filled - *size;
                        result.push('[');
                        for _ in 0..*size {
                            result.push('=');
                        }
                        for _ in 0..overflowed {
                            result.push('!');
                        }
                    }
                }
                TemplatePart::StateEmoji => {
                    if self.pos == self.len {
                        result.push('');
                    } else if self.pos == 0 {
                        result.push('🆕');
                    } else if self.pos > self.len {
                        result.push('💥');
                    } else {
                        // 0 < self.pos < self.len
                        result.push('');
                    }
                }
            }
        }
        result
    }
}

/// A handle for users to control a progress bar created by `Manager`.
pub struct Bar {
    id: usize,
    manager: Weak<ManagerInner>,
}

/// Lock order:
/// - last_draw
/// - out
/// - states
pub(crate) struct ManagerInner {
    states: Mutex<BTreeMap<usize, Arc<Mutex<BarState>>>>,
    ansi: Mutex<Option<bool>>,
    interval: std::time::Duration,
    pub(crate) out: Arc<Mutex<Box<dyn Out>>>,
    ticker: Mutex<Option<Ticker>>,
    force_when_finished: AtomicBool,

    // interval states
    next_id: AtomicUsize,
    last_draw: Mutex<std::time::Instant>,
    last_lines: AtomicUsize,
    need_redraw: AtomicBool,
}

impl ManagerInner {
    pub(crate) fn is_ticker_enabled(&self) -> bool {
        self.ticker.lock().unwrap().is_some()
    }

    /// This is expected to be called only when it's ANSI mode.
    pub(crate) fn clear_existing(&self, out: &mut Box<dyn Out>) {
        for _ in 0..self.last_lines.load(std::sync::atomic::Ordering::Acquire) {
            let _ = out.write_all(format!("{}{}", UP_ANSI, CLEAR_ANSI).as_bytes());
        }
    }

    pub(crate) fn is_terminal(&self, out: &mut Box<dyn Out>) -> bool {
        let ansi = self.ansi.lock().unwrap();
        match *ansi {
            None => out.is_terminal(),
            Some(force) => force,
        }
    }

    pub(crate) fn draw_inner(
        &self,
        states: &BTreeMap<usize, Arc<Mutex<BarState>>>,
        out: &mut Box<dyn Out>,
        is_terminal: bool,
    ) {
        let mut newlines = 0;
        for state in states.values() {
            let mut state = state.lock().unwrap();
            if !state.visible {
                continue;
            }
            if !is_terminal && !state.need_redraw {
                continue;
            }
            let outstr = format!("{}\n", state.render());
            if is_terminal {
                let splits = outstr.split('\n');
                let term_col = get_width(out.as_ref()) as usize;
                for i in splits {
                    let width = string_width(i);
                    newlines += width / term_col;
                    if width % term_col != 0 {
                        newlines += 1;
                    }
                }
            }
            let _ = out.write_all(outstr.as_bytes());
            state.need_redraw = false;
        }
        if is_terminal {
            self.last_lines
                .store(newlines, std::sync::atomic::Ordering::Release);
        }
    }

    pub(crate) fn mark_redraw(&self) {
        self.need_redraw
            .store(true, std::sync::atomic::Ordering::Release);
    }

    pub(crate) fn draw(&self, force: bool) {
        if !force && self.is_ticker_enabled() {
            return;
        }
        let now = std::time::Instant::now();
        let mut last_draw = self.last_draw.lock().unwrap();
        if !force && now - *last_draw < self.interval {
            return;
        }

        if !self
            .need_redraw
            .swap(false, std::sync::atomic::Ordering::AcqRel)
        {
            return;
        }
        let mut out = self.out.lock().unwrap();
        let states = self.states.lock().unwrap();
        let is_terminal = self.is_terminal(&mut out);
        if is_terminal && states.len() > 0 {
            // Don't clean output when no bars are present
            self.clear_existing(&mut out);
        }

        self.draw_inner(&states, &mut out, is_terminal);

        *last_draw = now;
    }

    pub(crate) fn suspend<F: FnOnce(&mut Box<dyn Out>) -> R, R>(&self, f: F) -> R {
        let mut out = self.out.lock().unwrap();
        let is_terminal = self.is_terminal(&mut out);
        if is_terminal {
            self.clear_existing(&mut out);
        }
        let result = f(&mut out);
        if is_terminal {
            let states = self.states.lock().unwrap();
            self.draw_inner(&states, &mut out, is_terminal);
        }
        result
    }
}

/// Trait for progress output streams, requires Unix file descriptor support.
/// `std::io::stdout`, `std::io::stderr` and `std::fs::File` implement this trait.
#[cfg(all(unix, feature = "console_width"))]
pub trait Out: std::io::Write + std::io::IsTerminal + std::os::fd::AsRawFd + Send + Sync {}
#[cfg(all(unix, feature = "console_width"))]
impl<T: std::io::Write + std::io::IsTerminal + std::os::fd::AsRawFd + Send + Sync> Out for T {}

/// Trait for progress output streams, requires Windows HANDLE support.
/// `std::io::stdout`, `std::io::stderr` and `std::fs::File` implement this trait.
#[cfg(all(windows, feature = "console_width"))]
pub trait Out:
    std::io::Write + std::io::IsTerminal + std::os::windows::io::AsRawHandle + Send + Sync
{
}
#[cfg(all(windows, feature = "console_width"))]
impl<T: std::io::Write + std::io::IsTerminal + std::os::windows::io::AsRawHandle + Send + Sync> Out
    for T
{
}

/// Trait for progress output streams.
/// `std::io::stdout`, `std::io::stderr` and `std::fs::File` implement this trait.
#[cfg(not(any(
    all(windows, feature = "console_width"),
    all(unix, feature = "console_width")
)))]
pub trait Out: std::io::Write + std::io::IsTerminal + Send + Sync {}
#[cfg(not(any(
    all(windows, feature = "console_width"),
    all(unix, feature = "console_width")
)))]
impl<T: std::io::Write + std::io::IsTerminal + Send + Sync> Out for T {}

/// The manager for progress bars. It's expected for users to create a `Manager`, create progress bars from it,
/// and drop it when all work has been done.
///
/// When manager is dropped, it would force a draw. After that bars would not be able to be interacted with.
pub struct Manager {
    inner: Arc<ManagerInner>,
}

impl Manager {
    /// Create a new `Manager` to stdout.
    ///
    /// The `interval` parameter specifies the minimum interval between two unforced draws.
    pub fn new(interval: std::time::Duration) -> Self {
        Manager {
            inner: Arc::new(ManagerInner {
                states: Mutex::new(BTreeMap::new()),
                next_id: AtomicUsize::new(0),
                interval,
                out: Arc::new(Mutex::new(Box::new(std::io::stdout()))),
                last_draw: Mutex::new(std::time::Instant::now() - interval),
                last_lines: AtomicUsize::new(0),
                ansi: Mutex::new(None),
                need_redraw: AtomicBool::new(false),
                ticker: Mutex::new(None),
                force_when_finished: AtomicBool::new(true),
            }),
        }
    }

    fn mark_redraw(&self) {
        self.inner.mark_redraw();
    }

    /// Set the `Manager` to write to stdout.
    pub fn with_stdout(self) -> Self {
        *self.inner.out.lock().unwrap() = Box::new(std::io::stdout());
        self.mark_redraw();
        self
    }

    /// Set the `Manager` to write to stderr.
    pub fn with_stderr(self) -> Self {
        *self.inner.out.lock().unwrap() = Box::new(std::io::stderr());
        self.mark_redraw();
        self
    }

    /// Set the `Manager` to write to a file.
    pub fn with_file(self, file: std::fs::File) -> Self {
        *self.inner.out.lock().unwrap() = Box::new(file);
        self.mark_redraw();
        self
    }

    /// Let `Manager` automatically detect whether it's writing to a terminal and use ANSI or not.
    pub fn auto_ansi(self) -> Self {
        *self.inner.ansi.lock().unwrap() = None;
        self.mark_redraw();
        self
    }

    /// Force `Manager` to use ANSI escape codes or not.
    pub fn force_ansi(self, force: bool) -> Self {
        *self.inner.ansi.lock().unwrap() = Some(force);
        self.mark_redraw();
        self
    }

    /// Ticker enables a background thread to draw progress bars at a fixed interval.
    ///
    /// When ticker is enabled, unforced draw would be ignored.
    pub fn set_ticker(&self, set_ticker: bool) {
        let mut ticker = self.inner.ticker.lock().unwrap();
        if set_ticker && ticker.is_none() {
            *ticker = Some(Ticker::new(self.inner.clone()));
        } else if !set_ticker && ticker.is_some() {
            *ticker = None;
        }
    }

    /// If manager shall forcely draw when pos == len without explicitly calling finish().
    ///
    /// Default is true.
    pub fn force_draw_when_finished(&self, force: bool) {
        self.inner
            .force_when_finished
            .store(force, std::sync::atomic::Ordering::Release);
    }

    /// Create a new progress bar.
    ///
    /// - `len`: The total length of the progress bar.
    /// - `message`: The message of the bar. Use `{msg}` in the template to refer to this.
    /// - `template`: The template of the bar.
    /// - `visible`: Whether the bar is visible.
    ///
    /// This makes a forced draw when visible is true.
    pub fn create_bar(&self, len: u64, message: &str, template: &str, visible: bool) -> Bar {
        let id = self
            .inner
            .next_id
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let bar_state = Arc::new(Mutex::new(BarState {
            len,
            pos: 0,
            message: message.to_string(),
            template: Template::new(template),
            created_at: std::time::Instant::now(),
            visible,
            need_redraw: true,
        }));

        self.inner
            .states
            .lock()
            .unwrap()
            .insert(id, bar_state.clone());

        if visible {
            self.mark_redraw();
            self.draw(true);
        }

        Bar {
            manager: Arc::downgrade(&self.inner),
            id,
        }
    }

    /// Draw all progress bars. In most cases it's not necessary to call this manually.
    ///
    /// If nothing changed, it would not draw no matter what.
    ///
    /// If ticker is enabled, unforced draw would be ignored. Otherwise, it would only draw when the interval has passed.
    ///
    /// Progress bars would be drawn by the order of `Bar` creation. In ANSI mode, it would clear the previous output.
    ///
    /// Finally, when output is not a terminal, bars would be drawn only when it needs to be redrawn.
    pub fn draw(&self, force: bool) {
        self.inner.draw(force);
    }

    /// Hide all progress bars, run the closure, and show them again like indicatif::MultiProgress::suspend.
    ///
    /// This method is used for implementing integrations with other libraries that may print to the terminal.
    ///
    /// When output is not a terminal, the closure would still be run but nothing would be done to the progress bars.
    pub fn suspend<F: FnOnce(&mut Box<dyn Out>) -> R, R>(&self, f: F) -> R {
        self.inner.suspend(f)
    }

    /// Create a writer for integration with other libraries.
    pub fn create_writer(&self) -> writer::KyuriWriter {
        writer::KyuriWriter::new(self.inner.clone())
    }
}

impl Drop for ManagerInner {
    /// Force a draw when the `ManagerInner` is dropped.
    fn drop(&mut self) {
        self.draw(true);
    }
}

impl Bar {
    fn get_manager_and_state(&self) -> Option<(Arc<ManagerInner>, Arc<Mutex<BarState>>)> {
        let manager = self.manager.upgrade()?;
        let state = manager.states.lock().unwrap().get(&self.id)?.clone();
        Some((manager, state))
    }

    fn check_if_force_draw(&self, manager: Arc<ManagerInner>, pos: u64, len: u64) {
        if pos == len && manager
            .force_when_finished
            .load(std::sync::atomic::Ordering::Acquire)
        {
            manager.draw(true);
        } else {
            manager.draw(false);
        }
    }

    /// Increment the progress bar by `n`. This makes an unforced draw.
    pub fn inc(&self, n: u64) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            state.pos += n;
            state.need_redraw = true;
            let pos = state.pos;
            let len = state.len;
            // Drop state before drawing, deadlock otherwise!
            std::mem::drop(state);
            manager.mark_redraw();
            self.check_if_force_draw(manager, pos, len);
        }
    }

    /// Set the position of the progress bar. This makes an unforced draw.
    pub fn set_pos(&self, pos: u64) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            state.pos = pos;
            state.need_redraw = true;
            let pos = state.pos;
            let len = state.len;
            // Drop state before drawing, deadlock otherwise!
            std::mem::drop(state);
            manager.mark_redraw();
            self.check_if_force_draw(manager, pos, len);
        }
    }

    /// Set the total length of the progress bar. This makes an unforced draw.
    pub fn set_len(&self, len: u64) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            state.len = len;
            state.need_redraw = true;
            let pos = state.pos;
            let len = state.len;
            // Drop state before drawing, deadlock otherwise!
            std::mem::drop(state);
            manager.mark_redraw();
            self.check_if_force_draw(manager, pos, len);
        }
    }

    /// Reset the created_at time to now. This makes an unforced draw.
    /// 
    /// Remember to call this when you want to reuse a bar object.
    pub fn reset_created_at(&self) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            state.created_at = std::time::Instant::now();
            state.need_redraw = true;
            let pos = state.pos;
            let len = state.len;
            // Drop state before drawing, deadlock otherwise!
            std::mem::drop(state);
            manager.mark_redraw();
            self.check_if_force_draw(manager, pos, len);
        }
    }

    /// Get the position of the progress bar.
    ///
    /// When manager is dropped, this would return 0
    pub fn get_pos(&self) -> u64 {
        self.get_manager_and_state()
            .map_or(0, |(_, state)| state.lock().unwrap().pos)
    }

    /// Get the total length of the progress bar.
    ///
    /// When manager is dropped, this would return 0
    pub fn get_len(&self) -> u64 {
        self.get_manager_and_state()
            .map_or(0, |(_, state)| state.lock().unwrap().len)
    }

    /// Set the progress bar to the end, and force a draw.
    pub fn finish(&self) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let state = state.lock().unwrap();
            let pos = state.pos;
            let len = state.len;
            if pos != len {
                self.set_pos(len);
            }
            std::mem::drop(state);
            manager.draw(true);
        }
    }

    /// Set the progress bar to the end, force a draw, and remove the progress bar from the manager.
    pub fn finish_and_drop(self) {
        self.finish();
        // Automatically drop
    }

    /// Set the visibility of the progress bar. This makes an forced draw when visible actually changes.
    pub fn set_visible(&self, visible: bool) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            if state.visible != visible {
                state.visible = visible;
                state.need_redraw = true;
                // Drop state before drawing, deadlock otherwise!
                std::mem::drop(state);
                manager.mark_redraw();
                manager.draw(true);
            }
        }
    }

    /// Get the visibility of the progress bar.
    ///
    /// When manager is dropped, this would return false
    pub fn is_visible(&self) -> bool {
        self.get_manager_and_state()
            .map_or(false, |(_, state)| state.lock().unwrap().visible)
    }

    /// Set the message of the progress bar. This makes an unforced draw.
    pub fn set_message(&self, message: &str) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            state.message = message.to_string();
            state.need_redraw = true;
            // Drop state before drawing, deadlock otherwise!
            std::mem::drop(state);
            manager.mark_redraw();
            manager.draw(false);
        }
    }

    /// Set the template of the progress bar. This makes an unforced draw.
    pub fn set_template(&self, template: &str) {
        if let Some((manager, state)) = self.get_manager_and_state() {
            let mut state = state.lock().unwrap();
            state.template = Template::new(template);
            state.need_redraw = true;
            // Drop state before drawing, deadlock otherwise!
            std::mem::drop(state);
            manager.mark_redraw();
            manager.draw(false);
        }
    }

    /// Return whether the progress bar (the manager) is still alive.
    ///
    /// When the manager is dropped, the progress bar would not be able to be interacted with.
    pub fn alive(&self) -> bool {
        self.get_manager_and_state().is_some()
    }
}

impl Drop for Bar {
    /// Drop the progress bar. This removes the progress bar from the manager and forces a draw.
    fn drop(&mut self) {
        if let Some((manager, _)) = self.get_manager_and_state() {
            manager.states.lock().unwrap().remove(&self.id);
            manager.mark_redraw();
            manager.draw(true);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::io::{Read, Seek};

    use super::*;

    #[test]
    fn basic_test() {
        let manager = Manager::new(std::time::Duration::from_secs(1));
        let bar_1 = manager.create_bar(
            100,
            "Downloading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );
        let bar_2 = manager.create_bar(
            100,
            "Uploading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );

        bar_1.set_pos(50);
        bar_2.set_pos(25);

        std::mem::drop(bar_1);
        std::mem::drop(bar_2);
    }

    #[test]
    fn dont_crash_when_zero() {
        let manager = Manager::new(std::time::Duration::from_secs(1));
        let bar = manager.create_bar(
            0,
            "Downloading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );

        bar.set_pos(0);
        manager.draw(true);
    }

    #[test]
    fn inc() {
        let manager = Manager::new(std::time::Duration::from_secs(1));
        let bar = manager.create_bar(
            100,
            "Downloading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );

        bar.inc(10);
        bar.inc(10);
        bar.inc(10);
        bar.inc(10);
        bar.inc(10);

        assert_eq!(bar.get_pos(), 50);

        std::mem::drop(bar);
    }

    #[test]
    fn visible() {
        let manager = Manager::new(std::time::Duration::from_secs(1));
        let bar = manager.create_bar(
            100,
            "Downloading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );

        assert!(bar.is_visible());

        bar.set_visible(false);
        assert!(!bar.is_visible());

        std::mem::drop(bar);
    }

    #[test]
    fn ticker() {
        let manager = Manager::new(std::time::Duration::from_secs(1));
        manager.set_ticker(true);
        let bar = manager.create_bar(
            100,
            "Downloading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );

        std::thread::sleep(std::time::Duration::from_secs(2));
        std::mem::drop(bar);
    }

    #[test]
    fn alive() {
        let manager = Manager::new(std::time::Duration::from_secs(1));
        let bar = manager.create_bar(
            100,
            "Downloading",
            "{msg}\n[{elapsed}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
            true,
        );

        assert!(bar.alive());

        std::mem::drop(manager);
        assert!(!bar.alive());
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_pb_to_file() {
        const TEMPLATE_SIMPLE: &str = "{msg}\n{bytes}/{total_bytes}";
        let memfd_name = std::ffi::CString::new("test_pb_to_file").unwrap();
        let memfd_fd =
            nix::sys::memfd::memfd_create(&memfd_name, nix::sys::memfd::MemFdCreateFlag::empty())
                .unwrap();
        let memfd_writer: std::fs::File = memfd_fd.into();
        let mut memfd_writer_clone = memfd_writer.try_clone().unwrap();
        let progressbar_manager =
            Manager::new(std::time::Duration::from_secs(1)).with_file(memfd_writer);
        let pb1 = progressbar_manager.create_bar(
            10,
            "Downloading http://d1.example.com/",
            TEMPLATE_SIMPLE,
            true,
        );
        let pb2 = progressbar_manager.create_bar(
            10,
            "Downloading http://d2.example.com/",
            TEMPLATE_SIMPLE,
            true,
        );

        pb1.set_pos(2);
        pb2.set_pos(3);
        progressbar_manager.draw(true);
        pb1.set_pos(5);
        pb2.set_pos(7);

        std::mem::drop(progressbar_manager);
        memfd_writer_clone
            .seek(std::io::SeekFrom::Start(0))
            .unwrap();
        let mut output = String::new();
        memfd_writer_clone.read_to_string(&mut output).unwrap();
        assert_eq!(
            output,
            r#"Downloading http://d1.example.com/
0 B/10 B
Downloading http://d2.example.com/
0 B/10 B
Downloading http://d1.example.com/
2 B/10 B
Downloading http://d2.example.com/
3 B/10 B
Downloading http://d1.example.com/
5 B/10 B
Downloading http://d2.example.com/
7 B/10 B
"#
        );
    }
}