termint 0.9.0

Library for colored printing and Terminal User Interfaces
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
use std::{cell::Cell, rc::Rc};

use crate::{
    buffer::Buffer,
    enums::Color,
    prelude::{MouseButton, MouseEvent, Rect, Vec2},
    style::{Style, Styleable},
    term::backend::MouseEventKind,
    text::Text,
    widgets::{Element, EventResult, LayoutNode, Widget},
};

type ProgressBarHandler<M> = Box<dyn Fn(f64) -> M>;

/// A widget that displays a horizontal progress bar.
///
/// The [`ProgressBar`] visually represents a percentage value in the range
/// `0.0` to `1.0`. It can be styled and configured to use custom characters.
///
/// # Terminology
///
/// - Thumb = the part of the bar representing completed progress.
/// - Track = the empty part of the bar.
///
/// # Default settings
///
/// By default, the thumb chars are `'▏', '▎', '▍', '▌', '▋', '▊', '▉', '█'`.
/// This ensures that the progress bar feels smooth. The track is set to `' '`
/// (space).
///
/// # Example
/// ```rust
/// use termint::prelude::*;
/// use std::{cell::Cell, rc::Rc};
///
/// let state = Rc::new(Cell::new(0.69));
/// let pb = ProgressBar::new(state.clone())
///     .thumb_chars(['▎', '▌', '▊', '█'])
///     .blue()
///     .track_char('=')
///     .track_style(Color::White)
///     .on_click(|p| format!("Clicked at {p}!"));
///
/// // Setting general style can be done also using `Stylize` trait
/// let pb = ProgressBar::<()>::new(state.clone())
///     .red()
///     .on_black()
///     .underline();
/// ```
pub struct ProgressBar<M> {
    state: Rc<Cell<f64>>,
    thumb_chars: Vec<char>,
    style: Style,
    track_char: char,
    track_style: Style,
    label: Option<ProgressLabel>,
    handlers: Vec<(MouseButton, ProgressBarHandler<M>)>,
}

/// Represents the [`ProgressBar`] label.
pub enum ProgressLabel {
    /// Static text widget.
    Static(Box<dyn Text>),
    /// Closure that generates a new text widget based on the progress.
    Dynamic(Box<dyn Fn(f64) -> Box<dyn Text>>),
}

impl<M> ProgressBar<M> {
    /// Creates a new [`ProgressBar`] with given percentage state.
    ///
    /// The `state` should contain a value between `0.0` and `1.0`. The value
    /// will get clamped if outside of this range.
    ///
    /// # Example
    /// ```rust
    /// use termint::prelude::*;
    /// use std::{cell::Cell, rc::Rc};
    ///
    /// let state = Rc::new(Cell::new(0.69));
    /// let pb = ProgressBar::<()>::new(state.clone());
    /// ```
    #[must_use]
    pub fn new(state: Rc<Cell<f64>>) -> Self {
        Self {
            state,
            style: Default::default(),
            thumb_chars: vec!['', '', '', '', '', '', '', ''],
            track_char: ' ',
            track_style: Default::default(),
            label: None,
            handlers: vec![],
        }
    }

    /// Sets the thumb characters used for rendering the progress.
    ///
    /// You should provide a sequence of characters representing increasing
    /// levels of progress ("fullness"). The last character is used for the
    /// fully filled sections, while the others are used for the fractional
    /// part at the tip.
    ///
    /// The default is `vec!['▏', '▎', '▍', '▌', '▋', '▊', '▉', '█']`.
    ///
    /// # Example
    /// ```rust
    /// use termint::prelude::*;
    /// use std::{cell::Cell, rc::Rc};
    ///
    /// // The progress bar bellow will look like this (incrementing progress):
    /// // `█`
    /// // `█▎`
    /// // `█▌`
    /// // `█▊`
    /// // `██`
    /// let pb = ProgressBar::<()>::new(Default::default())
    ///     .thumb_chars(['▎', '▌', '▊', '█']);
    /// ```
    #[must_use]
    pub fn thumb_chars<C>(mut self, chars: C) -> Self
    where
        C: IntoIterator<Item = char>,
    {
        self.thumb_chars = chars.into_iter().collect();
        self
    }

    /// Sets the style of the [`ProgressBar`]'s thumb.
    ///
    /// You can provide any type convertible to [`Style`].
    #[must_use]
    pub fn style<S>(mut self, style: S) -> Self
    where
        S: Into<Style>,
    {
        self.style = style.into();
        self
    }

    /// Sets the character used for the track of the [`ProgressBar`].
    ///
    /// The default is a space (`' '`).
    #[must_use]
    pub fn track_char(mut self, track: char) -> Self {
        self.track_char = track;
        self
    }

    /// Sets the base style of the [`ProgressBar`].
    ///
    /// You can provide any type convertible to [`Style`].
    #[must_use]
    pub fn track_style<S>(mut self, style: S) -> Self
    where
        S: Into<Style>,
    {
        self.track_style = style.into();
        self
    }

    /// Sets a [`ProgressBar`] label to the given text widget.
    ///
    /// `text` can be any type convertible into `Box<dyn Text>>`.
    pub fn label<T>(mut self, text: T) -> Self
    where
        T: Into<Box<dyn Text>>,
    {
        self.label = Some(ProgressLabel::Static(text.into()));
        self
    }

    /// Sets a [`ProgressBar`] label to the given dynamic label.
    ///
    /// `builder` is a closure, which accepts the current progress (`f64`) and
    /// returns the label text widget.
    pub fn dyn_label<F>(mut self, builder: F) -> Self
    where
        F: Fn(f64) -> Box<dyn Text> + 'static,
    {
        self.label = Some(ProgressLabel::Dynamic(Box::new(builder)));
        self
    }

    /// Sets the message to return when the left mouse button is clicked.
    ///
    /// The `response` is a closure that receives the progress at the click
    /// (in range 0.0-1.0) and returns the corresponding message.
    ///
    /// If a handler for the left mouse button already exists, it will be
    /// replaced.
    ///
    /// This is a convenience wrapper around [`ProgressBar::on_press`].
    ///
    /// **Note:** This requires mouse capture to be enabled. You can do that by
    /// calling [`Term::with_mouse`](crate::term::Term::with_mouse) on
    /// [`Term`](crate::term::Term) struct or
    /// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not
    /// using  the [`Term`](crate::term::Term).
    #[must_use]
    pub fn on_click<F>(self, response: F) -> Self
    where
        F: Fn(f64) -> M + 'static,
    {
        self.on_press(MouseButton::Left, response)
    }

    /// Sets the message to return when the given [`MouseButton`] is clicked.
    ///
    /// The `response` is a closure that receives the progress at the click
    /// (in range 0.0-1.0) and returns the corresponding message.
    ///
    /// If a handler for the given mouse button already exists, it will be
    /// replaced.
    ///
    /// **Note:** This requires mouse capture to be enabled. You can do that by
    /// calling [`Term::with_mouse`](crate::term::Term::with_mouse) on
    /// [`Term`](crate::term::Term) struct or
    /// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not
    /// using  the [`Term`](crate::term::Term).
    ///
    /// # Example
    ///
    /// ```rust
    /// use termint::prelude::*;
    ///
    /// let pb = ProgressBar::new(Default::default())
    ///     .on_press(MouseButton::Middle, |p| format!("Clicked at {p}!"));
    /// ```
    #[must_use]
    pub fn on_press<F>(mut self, button: MouseButton, response: F) -> Self
    where
        F: Fn(f64) -> M + 'static,
    {
        self.handlers.retain(|(b, _)| *b != button);
        self.handlers.push((button, Box::new(response)));
        self
    }
}

impl<M: Clone + 'static> Widget<M> for ProgressBar<M> {
    fn render(&self, buffer: &mut Buffer, layout: &LayoutNode) {
        let rect = layout.area;
        if rect.is_empty() || self.thumb_chars.is_empty() {
            return;
        }

        let (full_cells, head_id) = self.calc_size(&rect);
        let mut rest_len = rect.width().saturating_sub(full_cells);

        let mut track_pos = Vec2::new(rect.x() + full_cells, rect.y());
        if head_id > 0 {
            rest_len = rest_len.saturating_sub(1);
            buffer[track_pos]
                .char(self.thumb_chars[head_id])
                .style(self.style);
            track_pos.x += 1;
        }

        let thumb = self.thumb_chars[self.thumb_chars.len() - 1];
        buffer.set_str_styled(
            thumb.to_string().repeat(full_cells),
            rect.pos(),
            self.style,
        );

        buffer.set_str_styled(
            self.track_char.to_string().repeat(rest_len),
            &track_pos,
            self.track_style,
        );

        self.render_label(buffer, rect, full_cells);
    }

    fn height(&self, _size: &Vec2) -> usize {
        1
    }

    fn width(&self, size: &Vec2) -> usize {
        size.x
    }

    fn on_event(&self, node: &LayoutNode, e: &MouseEvent) -> EventResult<M> {
        let area = node.area;
        if !area.contains_pos(&e.pos) {
            return EventResult::None;
        }

        match &e.kind {
            MouseEventKind::Down(button) => {
                let rx = e.pos.x.saturating_sub(area.x());
                let progress = (rx as f64 / area.width() as f64).clamp(0., 1.);
                self.handlers
                    .iter()
                    .find(|(b, _)| b == button)
                    .map(|(_, m)| EventResult::Response(m(progress)))
                    .unwrap_or(EventResult::None)
            }
            _ => EventResult::None,
        }
    }
}

impl<M> ProgressBar<M> {
    /// Calculates the size of full cells and head ID to get corresponding
    /// progress character with.
    fn calc_size(&self, rect: &Rect) -> (usize, usize) {
        let progress = self.state.get().clamp(0.0, 1.0);
        let len = rect.width() as f64 * progress;
        let full_cells = len.floor() as usize;

        let frac = len - full_cells as f64;
        let head_id = (frac * (self.thumb_chars.len() - 1) as f64).round();
        (full_cells, head_id as usize)
    }

    fn render_label(&self, buffer: &mut Buffer, rect: Rect, full: usize) {
        let mut render_text = |text: &Box<dyn Text>| {
            let mut lines = vec![];
            text.append_lines(&mut lines, rect.size(), None);
            let Some(line) = lines.first() else {
                return;
            };

            let align = text.get_align();
            line.render(buffer, rect, align);
            let offset = line.align_offset(&rect, align);
            self.recolor_label(buffer, &rect, line.width, offset, full);
        };

        match &self.label {
            Some(ProgressLabel::Static(text)) => {
                render_text(text);
            }
            Some(ProgressLabel::Dynamic(builder)) => {
                let progress = self.state.get().clamp(0.0, 1.0);
                render_text(&builder(progress));
            }
            _ => {}
        }
    }

    fn recolor_label(
        &self,
        buffer: &mut Buffer,
        rect: &Rect,
        width: usize,
        offset: usize,
        full: usize,
    ) {
        let (thumb_color, track_color) = self.label_colors();
        let mut set_cols = |x, bg, fg| {
            let pos = Vec2::new(x, rect.y());
            if let Some(c) = bg {
                buffer[pos].bg = c;
            }
            if let Some(c) = fg {
                buffer[pos].fg = c;
            }
        };

        let base_x = rect.x() + offset;
        for x in base_x..base_x + width {
            if x < rect.x() + full {
                let fg = track_color.unwrap_or(Color::Black);
                set_cols(x, thumb_color, Some(fg));
            } else {
                set_cols(x, track_color, thumb_color);
            }
        }
    }

    fn label_colors(&self) -> (Option<Color>, Option<Color>) {
        let full_thumb_char = self.thumb_chars.last().copied().unwrap_or(' ');
        let thumb_color = if full_thumb_char.is_whitespace() {
            self.style.bg.or(self.style.fg)
        } else {
            self.style.fg.or(self.style.bg)
        };

        let track_color = if self.track_char.is_whitespace() {
            self.track_style.bg.or(self.track_style.fg)
        } else {
            self.track_style.fg.or(self.track_style.bg)
        };

        (thumb_color, track_color)
    }
}

impl<M> Styleable for ProgressBar<M> {
    fn style_mut(&mut self) -> &mut Style {
        &mut self.style
    }
}

impl<M: Clone + 'static> From<ProgressBar<M>> for Element<M> {
    fn from(value: ProgressBar<M>) -> Self {
        Element::new(value)
    }
}

impl<M: Clone + 'static> From<ProgressBar<M>> for Box<dyn Widget<M>> {
    fn from(value: ProgressBar<M>) -> Self {
        Box::new(value)
    }
}