strides 1.0.0-rc.2

Async-first terminal UI spinners and progress bars
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
//! Composable, ordered layout for progress output.
//!
//! A [`Layout`] is an ordered list of [`Segment`]s joined by a separator. Each segment pulls its
//! value from a [`RenderContext`] supplied by the call site; segments with nothing to show
//! contribute no output and are skipped when joining, so spacing stays correct without manual
//! padding.
//!
//! [`Layout::DEFAULT`] reproduces the built-in look (spinner, elapsed time, label, bar, message).
//! Attach a custom layout to a [`Theme`](crate::Theme) with
//! [`Theme::with_layout`](crate::Theme::with_layout):
//!
//! ```rust
//! use strides::layout::{Layout, Segment};
//!
//! let layout = Layout::new(&[])
//!     .with_segment(Segment::spinner())
//!     .with_segment(Segment::elapsed().with_border("[", "]"))
//!     .with_segment(Segment::bar())
//!     .with_segment(Segment::message());
//! ```

use std::borrow::Cow;
use std::fmt::Write as _;
use std::time::Duration;

use owo_colors::{OwoColorize as _, Style};

use crate::bar::Bar;

/// Values available to a [`Segment`] at render time.
///
/// Call sites fill this in once per frame; segments read from it. Fields that hold an [`Option`]
/// signal absence — the corresponding segment then renders nothing.
pub struct RenderContext<'a> {
    /// Current spinner character, if the spinner has ticked at least once.
    pub spinner: Option<char>,
    /// Time elapsed since rendering started.
    pub elapsed: Duration,
    /// Whether the elapsed time should be rendered at all.
    pub show_elapsed: bool,
    /// Bar style used by [`Segment::Bar`].
    pub bar: &'a Bar<'a>,
    /// Bar width in characters.
    pub bar_width: usize,
    /// Current progress fraction, or `None` when no progress is tracked.
    pub progress: Option<f64>,
    /// Cumulative bytes transferred. `0` until the first byte-tracking update.
    pub bytes_done: u64,
    /// Total bytes expected, if known. Used by [`Segment::Bytes`] and [`Segment::Eta`].
    pub bytes_total: Option<u64>,
    /// Smoothed transfer rate in bytes per second, if enough samples are available.
    pub rate: Option<f64>,
    /// Static label text, if any.
    pub label: Option<&'a str>,
    /// Dynamic message text, if any.
    pub message: Option<&'a str>,
    /// Fallback style for [`Segment::Spinner`] when it carries no explicit style.
    pub spinner_style: Style,
    /// Fallback style for [`Segment::Label`] when it carries no explicit style.
    pub annotation_style: Style,
}

/// A single renderable element of a [`Layout`].
///
/// Construct segments with the associated functions ([`Segment::spinner`], [`Segment::elapsed`],
/// …) and refine them with the `with_*` builders. A `with_*` builder applied to a segment it does
/// not affect returns that segment unchanged.
#[derive(Clone)]
pub enum Segment {
    /// The spinner character.
    Spinner {
        /// Explicit style; falls back to [`RenderContext::spinner_style`] when `None`.
        style: Option<Style>,
    },
    /// Elapsed time, rendered as `1.23s` with an optional border such as `[` … `]`.
    Elapsed {
        /// Style applied to the whole token, border included.
        style: Option<Style>,
        /// Optional `(left, right)` border. `None` renders the bare value.
        border: Option<(Cow<'static, str>, Cow<'static, str>)>,
        /// Digits after the decimal point.
        precision: u8,
    },
    /// The progress bar, using the [`Bar`] and width from the [`RenderContext`].
    Bar,
    /// Static label text.
    Label {
        /// Explicit style; falls back to [`RenderContext::annotation_style`] when `None`.
        style: Option<Style>,
        /// Fixed field width in characters; text is padded or truncated to fit.
        width: Option<usize>,
    },
    /// Dynamic message text, rendered unstyled.
    Message,
    /// Cumulative bytes transferred, optionally with the known total: `1.23 MiB / 5.00 MiB` when
    /// a total is set, `1.23 MiB` otherwise. Renders nothing while no bytes have been transferred
    /// and no total is known.
    Bytes,
    /// Smoothed transfer rate, formatted as e.g. `1.23 MiB/s`. Renders nothing until enough
    /// samples are available to derive a rate.
    Rate,
    /// Estimated time remaining, derived from `bytes_total`, `bytes_done` and `rate`. Renders
    /// nothing if any of those is missing or the rate is effectively zero.
    Eta,
    /// Fixed literal text, always rendered.
    Literal(Cow<'static, str>),
    /// Arbitrary user-supplied rendering. The function appends to the buffer; appending nothing
    /// makes the segment behave as absent.
    Custom(fn(&RenderContext, &mut String)),
}

impl Segment {
    /// A spinner segment with no explicit style.
    pub const fn spinner() -> Self {
        Segment::Spinner { style: None }
    }

    /// An elapsed-time segment: no border, two digits of precision.
    pub const fn elapsed() -> Self {
        Segment::Elapsed {
            style: None,
            border: None,
            precision: 2,
        }
    }

    /// A progress-bar segment.
    pub const fn bar() -> Self {
        Segment::Bar
    }

    /// A label segment with no explicit style and no fixed width.
    pub const fn label() -> Self {
        Segment::Label {
            style: None,
            width: None,
        }
    }

    /// A message segment.
    pub const fn message() -> Self {
        Segment::Message
    }

    /// A segment showing cumulative bytes transferred, with the total when known.
    pub const fn bytes() -> Self {
        Segment::Bytes
    }

    /// A segment showing the smoothed transfer rate.
    pub const fn rate() -> Self {
        Segment::Rate
    }

    /// A segment showing the estimated time remaining.
    pub const fn eta() -> Self {
        Segment::Eta
    }

    /// A fixed literal-text segment. Accepts a `&'static str` so the constructor is `const`;
    /// callers that need an owned string can build [`Segment::Literal`] directly with
    /// `Cow::Owned`.
    pub const fn literal(text: &'static str) -> Self {
        Segment::Literal(Cow::Borrowed(text))
    }

    /// A custom segment driven by `f`.
    pub fn custom(f: fn(&RenderContext, &mut String)) -> Self {
        Segment::Custom(f)
    }

    /// Set an explicit style on a [`Spinner`](Segment::Spinner), [`Elapsed`](Segment::Elapsed) or
    /// [`Label`](Segment::Label) segment. Other segments are returned unchanged.
    pub fn with_style(self, style: Style) -> Self {
        match self {
            Segment::Spinner { .. } => Segment::Spinner { style: Some(style) },
            Segment::Elapsed {
                border, precision, ..
            } => Segment::Elapsed {
                style: Some(style),
                border,
                precision,
            },
            Segment::Label { width, .. } => Segment::Label {
                style: Some(style),
                width,
            },
            other => other,
        }
    }

    /// Wrap an [`Elapsed`](Segment::Elapsed) segment with `left` and `right` border strings, for
    /// example `"["` and `"]"`. Other segments are returned unchanged.
    pub fn with_border(
        self,
        left: impl Into<Cow<'static, str>>,
        right: impl Into<Cow<'static, str>>,
    ) -> Self {
        match self {
            Segment::Elapsed {
                style, precision, ..
            } => Segment::Elapsed {
                style,
                border: Some((left.into(), right.into())),
                precision,
            },
            other => other,
        }
    }

    /// Set the decimal precision of an [`Elapsed`](Segment::Elapsed) segment. Other segments are
    /// returned unchanged.
    pub fn with_precision(self, precision: u8) -> Self {
        match self {
            Segment::Elapsed { style, border, .. } => Segment::Elapsed {
                style,
                border,
                precision,
            },
            other => other,
        }
    }

    /// Set a fixed field width on a [`Label`](Segment::Label) segment; text is padded with spaces
    /// or truncated to fit. Other segments are returned unchanged.
    pub fn with_width(self, width: usize) -> Self {
        match self {
            Segment::Label { style, .. } => Segment::Label {
                style,
                width: Some(width),
            },
            other => other,
        }
    }

    fn render(&self, ctx: &RenderContext, buf: &mut String) {
        match self {
            Segment::Spinner { style } => {
                if let Some(ch) = ctx.spinner {
                    let style = style.unwrap_or(ctx.spinner_style);
                    let _ = write!(buf, "{}", ch.style(style));
                }
            }
            Segment::Elapsed {
                style,
                border,
                precision,
            } => {
                if !ctx.show_elapsed {
                    return;
                }
                match style {
                    Some(style) => {
                        // Styling wraps the whole token, borders included, so it has to be
                        // materialized before it can be styled.
                        let mut token = String::new();
                        if let Some((left, _)) = border {
                            token.push_str(left);
                        }
                        let _ = write!(
                            token,
                            "{:.*}s",
                            *precision as usize,
                            ctx.elapsed.as_secs_f64()
                        );
                        if let Some((_, right)) = border {
                            token.push_str(right);
                        }
                        let _ = write!(buf, "{}", token.style(*style));
                    }
                    None => {
                        if let Some((left, _)) = border {
                            buf.push_str(left);
                        }
                        let _ = write!(
                            buf,
                            "{:.*}s",
                            *precision as usize,
                            ctx.elapsed.as_secs_f64()
                        );
                        if let Some((_, right)) = border {
                            buf.push_str(right);
                        }
                    }
                }
            }
            Segment::Bar => {
                if let Some(progress) = ctx.progress {
                    ctx.bar.render_into(buf, ctx.bar_width, progress);
                }
            }
            Segment::Label { style, width } => {
                if let Some(label) = ctx.label {
                    let style = style.unwrap_or(ctx.annotation_style);
                    match width {
                        Some(width) => {
                            // Width pads to `width` chars, precision truncates to `width` chars.
                            let width = *width;
                            let _ = write!(
                                buf,
                                "{}",
                                format_args!("{label:<width$.width$}").style(style)
                            );
                        }
                        None => {
                            let _ = write!(buf, "{}", label.style(style));
                        }
                    }
                }
            }
            Segment::Message => {
                if let Some(message) = ctx.message {
                    buf.push_str(message);
                }
            }
            Segment::Bytes => {
                if ctx.bytes_done == 0 && ctx.bytes_total.is_none() {
                    return;
                }
                format_bytes_iec(ctx.bytes_done, buf);
                if let Some(total) = ctx.bytes_total {
                    buf.push_str(" / ");
                    format_bytes_iec(total, buf);
                }
            }
            Segment::Rate => {
                if let Some(rate) = ctx.rate {
                    format_bytes_iec(rate.max(0.0) as u64, buf);
                    buf.push_str("/s");
                }
            }
            Segment::Eta => {
                if let (Some(total), Some(rate)) = (ctx.bytes_total, ctx.rate) {
                    if rate > 0.0 && total > ctx.bytes_done {
                        let remaining = (total - ctx.bytes_done) as f64 / rate;
                        format_eta_secs(remaining, buf);
                    }
                }
            }
            Segment::Literal(text) => buf.push_str(text),
            Segment::Custom(f) => f(ctx, buf),
        }
    }
}

const BYTE_UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];

fn format_bytes_iec(n: u64, buf: &mut String) {
    if n < 1024 {
        let _ = write!(buf, "{n} B");
        return;
    }
    let mut value = n as f64;
    let mut unit = 0;
    while value >= 1024.0 && unit < BYTE_UNITS.len() - 1 {
        value /= 1024.0;
        unit += 1;
    }
    let _ = write!(buf, "{:.2} {}", value, BYTE_UNITS[unit]);
}

fn format_eta_secs(secs: f64, buf: &mut String) {
    if !secs.is_finite() || secs < 0.0 {
        return;
    }
    let total = secs as u64;
    let hours = total / 3600;
    let minutes = (total % 3600) / 60;
    let seconds = total % 60;
    buf.push_str("eta ");
    if hours > 0 {
        let _ = write!(buf, "{hours}h{minutes:02}m{seconds:02}s");
    } else if minutes > 0 {
        let _ = write!(buf, "{minutes}m{seconds:02}s");
    } else {
        let _ = write!(buf, "{seconds}s");
    }
}

/// An ordered, composable description of how progress output is rendered.
///
/// A `Layout` holds a sequence of [`Segment`]s and a separator. [`render`](Layout::render) walks
/// the segments, drops the ones that produce no output, and joins the rest with the separator.
#[derive(Clone)]
pub struct Layout {
    segments: Cow<'static, [Segment]>,
    separator: Cow<'static, str>,
}

static DEFAULT_SEGMENTS: [Segment; 5] = [
    Segment::Spinner { style: None },
    Segment::Elapsed {
        style: None,
        border: None,
        precision: 2,
    },
    Segment::Label {
        style: None,
        width: None,
    },
    Segment::Bar,
    Segment::Message,
];

impl Layout {
    /// The default layout: elapsed time, spinner, label, bar and message, joined by a single
    /// space.
    pub const DEFAULT: Layout = Layout::new(&DEFAULT_SEGMENTS);

    /// Create a layout from a static slice of segments, joined by a single space. Pass `&[]` to
    /// start empty and build up with [`with_segment`](Self::with_segment).
    pub const fn new(segments: &'static [Segment]) -> Self {
        Self {
            segments: Cow::Borrowed(segments),
            separator: Cow::Borrowed(" "),
        }
    }

    /// Append `segment`, switching to owned storage.
    pub fn with_segment(mut self, segment: Segment) -> Self {
        self.segments.to_mut().push(segment);
        self
    }

    /// Set the separator inserted between non-empty segments. Defaults to a single space.
    pub fn with_separator(mut self, separator: impl Into<Cow<'static, str>>) -> Self {
        self.separator = separator.into();
        self
    }

    /// Render the layout for `ctx`, appending to `buf`.
    ///
    /// Segments that produce no output are skipped, and the separator is inserted only between
    /// segments that do produce output.
    pub fn render(&self, ctx: &RenderContext, buf: &mut String) {
        let mut first = true;

        for segment in self.segments.iter() {
            let rollback = buf.len();
            if !first {
                buf.push_str(&self.separator);
            }
            let after_separator = buf.len();

            segment.render(ctx, buf);

            // A segment that appended nothing is treated as absent: undo the separator we
            // optimistically pushed and leave `first` untouched.
            if buf.len() == after_separator {
                buf.truncate(rollback);
            } else {
                first = false;
            }
        }
    }
}

impl Default for Layout {
    fn default() -> Self {
        Layout::DEFAULT
    }
}

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

    use crate::bar::Bar;

    fn context() -> RenderContext<'static> {
        RenderContext {
            spinner: None,
            elapsed: Duration::from_millis(1500),
            show_elapsed: false,
            bar: EMPTY_BAR,
            bar_width: 10,
            progress: None,
            bytes_done: 0,
            bytes_total: None,
            rate: None,
            label: None,
            message: None,
            spinner_style: Style::new(),
            annotation_style: Style::new(),
        }
    }

    static EMPTY_BAR: &Bar<'static> = &Bar::empty();

    #[test]
    fn skips_empty_segments_and_their_separators() {
        let layout = Layout::new(&[])
            .with_segment(Segment::elapsed())
            .with_segment(Segment::message())
            .with_segment(Segment::bar())
            .with_segment(Segment::literal("done"));

        let mut ctx = context();
        ctx.message = Some("hello");

        let mut buf = String::new();
        layout.render(&ctx, &mut buf);

        // elapsed hidden and bar untracked → only message and literal remain.
        assert_eq!(buf, "hello done");
    }

    #[test]
    fn elapsed_border_and_precision() {
        let layout = Layout::new(&[])
            .with_segment(Segment::elapsed().with_border("[", "]").with_precision(1));

        let mut ctx = context();
        ctx.show_elapsed = true;

        let mut buf = String::new();
        layout.render(&ctx, &mut buf);

        assert_eq!(buf, "[1.5s]");
    }

    #[test]
    fn custom_separator() {
        let layout = Layout::new(&[])
            .with_segment(Segment::literal("a"))
            .with_segment(Segment::literal("b"))
            .with_separator(" | ");

        let mut buf = String::new();
        layout.render(&context(), &mut buf);

        assert_eq!(buf, "a | b");
    }

    fn render(segment: Segment, ctx: &RenderContext) -> String {
        let mut buf = String::new();
        Layout::new(&[]).with_segment(segment).render(ctx, &mut buf);
        buf
    }

    #[test]
    fn bytes_segment_skips_when_zero_and_no_total() {
        assert_eq!(render(Segment::bytes(), &context()), "");
    }

    #[test]
    fn bytes_segment_renders_done_only() {
        let mut ctx = context();
        ctx.bytes_done = 1500;
        assert_eq!(render(Segment::bytes(), &ctx), "1.46 KiB");
    }

    #[test]
    fn bytes_segment_renders_done_and_total() {
        let mut ctx = context();
        ctx.bytes_done = 1024 * 1024;
        ctx.bytes_total = Some(5 * 1024 * 1024);
        assert_eq!(render(Segment::bytes(), &ctx), "1.00 MiB / 5.00 MiB");
    }

    #[test]
    fn bytes_segment_renders_zero_when_total_known() {
        let mut ctx = context();
        ctx.bytes_total = Some(2048);
        assert_eq!(render(Segment::bytes(), &ctx), "0 B / 2.00 KiB");
    }

    #[test]
    fn rate_segment_skips_without_sample() {
        assert_eq!(render(Segment::rate(), &context()), "");
    }

    #[test]
    fn rate_segment_renders_with_unit_suffix() {
        let mut ctx = context();
        ctx.rate = Some(800.0 * 1024.0);
        assert_eq!(render(Segment::rate(), &ctx), "800.00 KiB/s");
    }

    #[test]
    fn eta_segment_skips_without_total_or_rate() {
        let mut ctx = context();
        ctx.rate = Some(1024.0);
        assert_eq!(render(Segment::eta(), &ctx), "");

        ctx.rate = None;
        ctx.bytes_total = Some(2048);
        assert_eq!(render(Segment::eta(), &ctx), "");
    }

    #[test]
    fn eta_segment_seconds() {
        let mut ctx = context();
        ctx.bytes_done = 0;
        ctx.bytes_total = Some(1024 * 50);
        ctx.rate = Some(1024.0 * 10.0);
        assert_eq!(render(Segment::eta(), &ctx), "eta 5s");
    }

    #[test]
    fn eta_segment_minutes_and_hours() {
        let mut ctx = context();
        ctx.bytes_done = 0;
        ctx.bytes_total = Some(125);
        ctx.rate = Some(1.0);
        assert_eq!(render(Segment::eta(), &ctx), "eta 2m05s");

        ctx.bytes_total = Some(3725);
        assert_eq!(render(Segment::eta(), &ctx), "eta 1h02m05s");
    }

    #[test]
    fn eta_segment_skips_when_complete() {
        let mut ctx = context();
        ctx.bytes_done = 4096;
        ctx.bytes_total = Some(4096);
        ctx.rate = Some(1024.0);
        assert_eq!(render(Segment::eta(), &ctx), "");
    }
}