tuile 0.1.0

Textual-grade widgets, design system, animation and app runtime on top of ratatui
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
//! Progress bars, rings and step indicators for long-running tasks.
//!
//! ```no_run
//! use tuile::prelude::*;
//! # let area = Rect::new(0, 0, 40, 3);
//! # let mut buf = Buffer::empty(area);
//! # let now = Instant::now();
//! # let mut state = ProgressState::default();
//! ProgressBar::new().show_percentage(true).show_eta(true).render(area, &mut buf, &mut state);
//! state.set(0.75, now, Duration::from_millis(300));
//! ```

use std::time::{Duration, Instant};

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::{StatefulWidget, Widget};

use crate::anim::{Tween, elapsed};
use crate::core::{Interactive, Outcome};
use crate::draw::{put, st};
use crate::theme::{self, Rgb, Theme, Variant};

/// State for a progress bar: current value, animation, and ETA calculation.
#[derive(Clone, Debug)]
pub struct ProgressState {
    pub target: f32,
    anim: Tween,
    started: Option<Instant>,
    samples: Vec<(Instant, f32)>,
    indeterminate_phase: f32,
}

impl Default for ProgressState {
    fn default() -> Self {
        Self {
            target: 0.0,
            anim: Tween::default(),
            started: None,
            samples: Vec::new(),
            indeterminate_phase: 0.0,
        }
    }
}

impl ProgressState {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn set(&mut self, v: f32, now: Instant, duration: Duration) {
        let _current = self.anim.value(now);
        self.target = v.clamp(0.0, 1.0);
        self.anim.go(self.target, now, duration);
        if self.started.is_none() && v > 0.0 {
            self.started = Some(now);
        }
        self.samples.push((now, self.target));
        if self.samples.len() > 10 {
            self.samples.remove(0);
        }
    }

    pub fn value(&self, now: Instant) -> f32 {
        self.anim.value(now).clamp(0.0, 1.0)
    }

    pub fn animating(&self, now: Instant) -> bool {
        self.anim.active(now)
    }

    fn eta(&self, now: Instant, current: f32) -> Option<Duration> {
        if current >= 1.0 || current <= 0.0 {
            return None;
        }
        let started = self.started?;
        if self.samples.len() < 2 {
            return None;
        }
        let elapsed_s = now.saturating_duration_since(started).as_secs_f32();
        if elapsed_s < 1.0 {
            return None;
        }
        let rate = current / elapsed_s;
        if rate <= 0.0 {
            return None;
        }
        let remaining = (1.0 - current) / rate;
        Some(Duration::from_secs_f32(remaining))
    }

    pub fn tick_indeterminate(&mut self, now: Instant, speed: f32) {
        if let Some(s) = self.started {
            self.indeterminate_phase = elapsed(s, now) * speed;
        }
    }
}

impl Interactive for ProgressState {
    fn handle_key(&mut self, _key: ratatui::crossterm::event::KeyEvent) -> Outcome {
        Outcome::Ignored
    }

    fn handle_mouse(&mut self, _m: ratatui::crossterm::event::MouseEvent) -> Outcome {
        Outcome::Ignored
    }
}

/// Textual ProgressBar: `━` bar with `╸` half-end, `NN%` and ETA columns.
pub struct ProgressBar {
    label: Option<String>,
    show_bar: bool,
    show_percentage: bool,
    show_eta: bool,
    variant: Variant,
    indeterminate: bool,
    compact: bool,
    width_hint: Option<u16>,
    background: Option<Rgb>,
    now: Option<Instant>,
    theme: Option<Theme>,
}
impl ProgressBar {
    pub fn new() -> Self {
        Self {
            label: None,
            show_bar: true,
            show_percentage: true,
            show_eta: true,
            variant: Variant::Primary,
            indeterminate: false,
            compact: false,
            width_hint: None,
            now: None,
            theme: None,
            background: None,
        }
    }
    pub fn label(mut self, s: &str) -> Self {
        self.label = Some(s.to_string());
        self
    }

    pub fn show_bar(mut self, v: bool) -> Self {
        self.show_bar = v;
        self
    }

    pub fn show_percentage(mut self, v: bool) -> Self {
        self.show_percentage = v;
        self
    }

    pub fn show_eta(mut self, v: bool) -> Self {
        self.show_eta = v;
        self
    }

    pub fn variant(mut self, v: Variant) -> Self {
        self.variant = v;
        self
    }

    pub fn indeterminate(mut self) -> Self {
        self.indeterminate = true;
        self
    }

    pub fn compact(mut self, v: bool) -> Self {
        self.compact = v;
        self
    }

    pub fn width_hint(mut self, w: u16) -> Self {
        self.width_hint = Some(w);
        self
    }

    pub fn now(mut self, n: Instant) -> Self {
        self.now = Some(n);
        self
    }

    pub fn theme(mut self, th: &Theme) -> Self {
        self.theme = Some(*th);
        self
    }

    pub fn background(mut self, bg: Rgb) -> Self {
        self.background = Some(bg);
        self
    }
}

impl Default for ProgressBar {
    fn default() -> Self {
        Self::new()
    }
}

impl StatefulWidget for ProgressBar {
    type State = ProgressState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if area.is_empty() {
            return;
        }
        let th = self.theme.unwrap_or_else(theme::current);
        let now = self.now.unwrap_or_else(Instant::now);
        let bg = self.background.unwrap_or(th.surface);

        let pct_w = if self.show_percentage && !self.compact {
            5
        } else {
            0
        };
        let eta_w = if self.show_eta && !self.compact {
            10
        } else {
            0
        };
        let bar_w = area
            .width
            .saturating_sub(pct_w + eta_w + if pct_w > 0 || eta_w > 0 { 1 } else { 0 });

        if bar_w < 4 {
            return;
        }

        let y = area.y;
        let x = area.x;

        if self.show_bar {
            let track = bg.blend(th.foreground, 0.15);
            for dx in 0..bar_w {
                put(buf, x + dx, y, "━", 1, st(track, bg));
            }

            if self.indeterminate {
                state.tick_indeterminate(now, 30.0);
                let width = bar_w as f32;
                let hl = 0.25 * width;
                let total = width + hl;
                let mut start = state.indeterminate_phase % (2.0 * total);
                if start > total {
                    start = 2.0 * total - start;
                }
                start -= hl;
                let end = start + hl;
                let color = th.variant(self.variant);
                for dx in 0..bar_w {
                    let fx = dx as f32;
                    if fx >= start && fx < end {
                        put(buf, x + dx, y, "━", 1, st(color, bg));
                    }
                }
            } else {
                let p = state.value(now);
                let color = if p >= 1.0 {
                    th.success
                } else {
                    th.variant(self.variant)
                };
                let cells = p * bar_w as f32;
                let full = cells.floor() as u16;
                for dx in 0..full {
                    put(buf, x + dx, y, "━", 1, st(color, bg));
                }
                if full < bar_w && cells - full as f32 >= 0.5 {
                    put(buf, x + full, y, "╸", 1, st(color, bg));
                }
            }
        }

        let mut text_x = x + bar_w;
        if pct_w > 0 {
            if self.indeterminate {
                put(buf, text_x, y, " --% ", pct_w, st(th.text_muted, bg));
            } else {
                let p = state.value(now);
                let pct = format!(" {:>3}%", (p * 100.0).round() as u32);
                put(buf, text_x, y, &pct, pct_w, st(th.foreground, bg));
            }
            text_x += pct_w + 1;
        }

        if eta_w > 0 {
            if self.indeterminate {
                put(buf, text_x, y, " --:--:--", eta_w, st(th.text_muted, bg));
            } else {
                let p = state.value(now);
                let eta_text = if let Some(d) = state.eta(now, p) {
                    let s = d.as_secs();
                    format!(
                        "{:>9}",
                        format!("{:02}:{:02}:{:02}", s / 3600, (s / 60) % 60, s % 60)
                    )
                } else {
                    format!("{:>9}", "--:--:--")
                };
                put(buf, text_x, y, &eta_text, eta_w, st(th.text_muted, bg));
            }
        }
    }
}

/// Step progress: `●●●○○` dots for N steps.
pub struct StepProgress {
    total: usize,
    current: usize,
    filled: &'static str,
    empty: &'static str,
    theme: Option<Theme>,
}

impl StepProgress {
    pub fn new(total: usize) -> Self {
        Self {
            total,
            current: 0,
            filled: "●",
            empty: "○",
            theme: None,
        }
    }

    pub fn current(mut self, n: usize) -> Self {
        self.current = n;
        self
    }

    pub fn filled(mut self, g: &'static str) -> Self {
        self.filled = g;
        self
    }

    pub fn empty(mut self, g: &'static str) -> Self {
        self.empty = g;
        self
    }

    pub fn theme(mut self, th: &Theme) -> Self {
        self.theme = Some(*th);
        self
    }
}

impl Widget for StepProgress {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.is_empty() || self.total == 0 {
            return;
        }
        let th = self.theme.unwrap_or_else(theme::current);
        let fg = th.primary;
        let dim = th.text_muted;
        let bg = th.background;

        for (x, i) in (area.x..).zip(0..self.total.min(area.width as usize)) {
            if x >= area.right() {
                break;
            }
            let glyph = if i < self.current {
                self.filled
            } else {
                self.empty
            };
            let color = if i < self.current { fg } else { dim };
            put(buf, x, area.y, glyph, 1, st(color, bg));
        }
    }
}

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

    #[test]
    fn progress_state_animates() {
        let mut state = ProgressState::new();
        let now = Instant::now();
        state.set(0.5, now, Duration::from_millis(100));
        assert!(state.animating(now));
        assert!(state.value(now + Duration::from_millis(200)) > 0.49);
    }

    #[test]
    fn eta_calculation() {
        let mut state = ProgressState::new();
        let now = Instant::now();
        state.started = Some(now - Duration::from_secs(5));
        state.samples.push((now - Duration::from_secs(5), 0.0));
        state.samples.push((now, 0.5));
        state.target = 0.5;
        let eta = state.eta(now, 0.5);
        assert!(eta.is_some());
        let d = eta.unwrap();
        assert!(d.as_secs() >= 4 && d.as_secs() <= 6);
    }

    #[test]
    fn step_progress_renders() {
        let mut buf = Buffer::empty(Rect::new(0, 0, 10, 1));
        StepProgress::new(5).current(2).render(buf.area, &mut buf);
        assert_eq!(buf[(0, 0)].symbol(), "●");
        assert_eq!(buf[(3, 0)].symbol(), "○");
    }
}