Skip to main content

kdam/std/
extension.rs

1use std::io::{Result, Write};
2
3/// Comman progress bar functionalities shared between different types of progress bars.
4pub trait BarExt {
5    /// Clear current progress bar display.
6    ///
7    /// Returns `Err`, if writing to handle fails.
8    fn clear(&mut self) -> Result<()>;
9
10    /// Take input via progress bar (without overlaping with bar(s)).
11    ///
12    /// Returns `Err`, if reading from stdin handle fails.
13    fn input<T: Into<String>>(&mut self, text: T) -> Result<String>;
14
15    /// Force refresh current progress bar display.
16    ///
17    /// Returns `Err`, if writing to handle fails.
18    fn refresh(&mut self) -> Result<()>;
19
20    /// Render progress bar text.
21    fn render(&mut self) -> String;
22
23    /// Resets counter to 0 for repeated use.
24    ///
25    /// Consider combining with `leave = true`.
26    fn reset(&mut self, total: Option<usize>);
27
28    /// Manually update the progress bar, useful for streams such as reading files.
29    ///
30    /// Returns whether an update was triggered or not depending on constraints.
31    /// Returns `Err`, if writing to handle fails.
32    fn update(&mut self, n: usize) -> Result<bool>;
33
34    /// Set counter value instead of incrementing counter through [update](Self::update) method.
35    ///
36    /// Returns wheter a update was triggered or not depending on constraints.
37    /// Returns `Err`, if writing to handle fails.
38    fn update_to(&mut self, n: usize) -> Result<bool>;
39
40    /// Print a message via progress bar (without overlaping with bar(s)).
41    ///
42    /// Returns `Err`, if writing to handle fails.
43    fn write<T: Into<String>>(&mut self, text: T) -> Result<()>;
44
45    /// Write progress bar rendered text to a writer (useful for writing files).
46    ///
47    /// If `n` is supplied then this method behaves like [update](Self::update) method.
48    ///
49    /// Returns whether a update was triggered or not depending on constraints.
50    /// Returns `Err`, if writing to handle fails.
51    ///
52    /// # Example
53    ///
54    /// Using [write_to](Self::write_to) as [update_to](Self::update_to).
55    ///
56    /// ```
57    /// use kdam::{tqdm, BarExt};
58    /// use std::{fs::File, io::Write};
59    ///
60    /// let mut pb = tqdm!(total = 100, animation = "ascii");
61    /// let mut f = File::create("kdam-logs.txt").unwrap();
62    ///
63    /// for i in 1..101 {
64    ///     pb.counter = i;
65    ///     pb.write_to(&mut f, Some(0));
66    /// }
67    /// ```
68    fn write_to<T: Write>(&mut self, writer: &mut T, n: Option<usize>) -> Result<bool>;
69}