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
use crate::progress::Bar;
use crate::term::Colorizer;

/// Renderable columns for [RichProgress](crate::RichProgress).
///
/// These columns may differ in name as of `rich.progress`.
#[derive(Debug, Clone)]
pub enum Column {
    /// Progress bar column.
    /// If progress.pb.n || progress.pb.total == 0, then an pulsating animation
    /// else rich style animation.
    Bar,
    /// Progress counter i.e. `sel.pb.n`.
    Count,
    /// Formatted counter i.e. `progress.pb.n / progress.pb.total`
    CountTotal,
    /// Progress elapsed time
    ElapsedTime,
    /// Progress percentage done, with precision.
    Percentage(usize),
    /// Progress update rate.
    Rate,
    /// Progress remaining time / ETA.
    RemainingTime,
    /// Spinner for progress. See more styles at [rich repository](https://github.com/Textualize/rich/blob/master/rich/_spinners.py).
    /// - first argument is Vec<String> of frames.
    /// - second argument is interval of frames.
    /// - third argument is speed of frames.
    ///
    /// # Example
    ///
    /// ```rust
    /// use kdam::Column;
    ///
    /// Column::Spinner(
    ///     "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
    ///     .chars()
    ///     .map(|x| x.to_string())
    ///     .collect::<Vec<String>>(),
    ///     80.0,
    ///     1.0
    /// );
    /// ````
    Spinner(Vec<String>, f32, f32),
    /// Text column.
    ///
    /// # Example`
    ///
    /// ```rust
    /// use kdam::Column;
    ///
    /// Column::Text("•".to_owned());
    /// Column::Text("[bold red]Downloading".to_owned());
    /// ```
    Text(String),
    /// Progress total i.e. `progress.pb.total`.
    Total,
}

impl Column {
    /// Text column.
    ///
    /// # Example`
    ///
    /// ```rust
    /// use kdam::Column;
    ///
    /// Column::text("•");
    /// Column::Text("•".to_owned());
    ///
    /// Column::text("[bold red]Downloading");
    /// Column::Text("[bold red]Downloading".to_owned());
    /// ```
    pub fn text(text: &str) -> Self {
        Self::Text(text.to_owned())
    }
}

/// An implementation [rich.progress](https://rich.readthedocs.io/en/latest/progress.html) using [Bar](crate::Bar).
///
/// # Example
///
/// ```rust
/// use kdam::prelude::*;
/// use kdam::{Column, RichProgress};
///
/// fn main() {
///     let mut pb = RichProgress::new(
///         tqdm!(total = 100),
///         vec![Column::Bar, Column::Percentage(2)]
///     );
///
///     for _ in 0..100 {
///         pb.update(1);
///     }
///
///     eprint!("\n");
/// }
/// ```
#[derive(Debug)]
pub struct RichProgress {
    /// Instance of [Bar](crate::Bar) to render [RichProgress](crate::RichProgress).
    pub pb: Bar,
    /// Vector of renderable columns.
    pub columns: Vec<Column>,
}

impl RichProgress {
    /// Create a new instance of [RichProgress](crate::RichProgress).
    pub fn new(pb: Bar, columns: Vec<Column>) -> Self {
        Self { pb, columns }
    }

    /// Replace a column value at specific index.
    pub fn replace(&mut self, index: usize, col: Column) {
        let _ = std::mem::replace(&mut self.columns[index], col);
    }
}

crate::_impl_bar_methods!(RichProgress, render);

fn render(progress: &mut RichProgress) -> String {
    let mut bar_text = vec![];
    let mut bar_length = 0;
    let mut progress_bar_index = None;
    let et = progress.pb.elapsed_time();

    for col in progress.columns.clone() {
        match col {
            Column::Bar => {
                progress_bar_index = Some(bar_text.len());
                bar_text.push("".to_owned());
            }

            Column::Count => {
                let fmt_progress = progress.pb.fmt_counter();
                bar_length += fmt_progress.chars().count();
                bar_text.push(fmt_progress.colorize("green"));
            }

            Column::CountTotal => {
                let fmt_progress = format!(
                    "{}/{}",
                    progress.pb.fmt_counter(),
                    progress.pb.fmt_total()
                );
                bar_length += fmt_progress.chars().count();
                bar_text.push(fmt_progress.colorize("green"));
            }

            Column::ElapsedTime => {
                let elapsed_time = progress.pb.fmt_elapsed_time();
                bar_length += elapsed_time.chars().count();
                bar_text.push(elapsed_time.colorize("cyan"));
            }

            Column::Percentage(precision) => {
                let percentage = format!("{:.1$}%", progress.pb.percentage() * 100., precision);
                bar_length += percentage.chars().count();
                bar_text.push(percentage.colorize("magenta"));
            }

            Column::Rate => {
                let speed = progress.pb.fmt_rate();
                bar_length += speed.chars().count();
                bar_text.push(speed.colorize("red"));
            }

            Column::RemainingTime => {
                let remaining_time = progress.pb.fmt_remaining_time();
                bar_length += remaining_time.chars().count();
                bar_text.push(remaining_time.colorize("cyan"));
            }

            Column::Spinner(frames, interval, speed) => {
                let frame_no = (progress.pb.elapsed_time() * speed) / (interval / 1000.0);
                let frame = frames.get(frame_no as usize % frames.len()).unwrap();
                bar_length += frame.chars().count();
                bar_text.push(frame.colorize("green"));
            }

            Column::Text(text) => {
                let color = match (text.find("["), text.find("]")) {
                    (Some(start), Some(end)) => {
                        if start == 0 {
                            Some(&text[(start + 1)..(end)])
                        } else {
                            None
                        }
                    }
                    _ => None,
                };

                if let Some(code) = color {
                    let text = text.replace(&format!("[{}]", code), "");
                    bar_length += text.chars().count();
                    bar_text.push(text.colorize(code));
                } else {
                    bar_length += text.chars().count();
                    bar_text.push(text);
                }
            }

            Column::Total => {
                let fmt_progress = progress.pb.fmt_total();
                bar_length += fmt_progress.chars().count();
                bar_text.push(fmt_progress.colorize("green"));
            }
        }
    }

    bar_length += bar_text.len() - 1;

    if progress_bar_index.is_some() {
        progress.pb.adjust_ncols(bar_length as i16);
        let pb;

        if progress.pb.indefinite() || !progress.pb.started() {
            pb = crate::styles::rich::pulse(progress.pb.get_ncols(), et);
        } else {
            pb = crate::styles::rich::bar(
                progress.pb.percentage() as f32,
                progress.pb.get_ncols(),
            );
        }

        let _ = std::mem::replace(&mut bar_text[progress_bar_index.unwrap()], pb);
    }

    progress.pb.set_bar_length(bar_length as i16 + progress.pb.get_ncols());
    bar_text.join(" ")
}