tinsel 0.1.0

Clean, pink-hued terminal output with a little sparkle: live step lists, spinners, tables, trees, prompts and more. Zero dependencies.
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
use std::fmt;

use crate::block::Block;
use crate::style::palette::{BERRY, BLUSH, LINE, MUTED, PETAL, RED, ROSE, SHINE, SOFT, TEXT, TRACK};
use crate::style::{Line, RAMP, Rgb, mix, paint, sample};
use crate::term::content_width;

const ARC: [&str; 6] = ["◜", "◠", "◝", "◞", "◡", "◟"];
const ORBIT: [&str; 8] = ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"];
const STARS: [&str; 4] = ["·", "⋆", "✧", "✦"];
const LEVELS: [&str; 8] = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];

/// Random-looking but repeatable 0..1 noise for a cell at a moment, so glitter flickers
/// without needing a random number generator.
pub(crate) fn noise(cell: u32, tick: u32) -> f32 {
    let mut x = cell.wrapping_mul(0x9E37_79B1) ^ tick.wrapping_mul(0x85EB_CA77);
    x ^= x >> 15;
    x = x.wrapping_mul(0x2C1B_3C6D);
    x ^= x >> 12;
    (x & 0xFFFF) as f32 / 65535.0
}

/// How a step looks while it's running. Every style keeps to the pink palette and
/// eases its brightness rather than snapping, so none of them feel mechanical.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Spinner {
    /// Three little stars twinkling out of step - the tinsel one.
    #[default]
    Sparkle,
    /// Three dots with a brightness wave rolling through them, like a typing indicator.
    Dots,
    /// A star gliding back and forth along a short track, leaving a fading trail.
    Comet,
    /// Five bars rising and falling like a live audio waveform, brighter as they peak.
    Wave,
    /// A single arc turning smoothly.
    Arc,
    /// A gap travelling around a braille ring, its colour drifting berry → blush.
    Orbit,
    /// One dot slowly breathing in size and colour, flashing a star at its brightest.
    Pulse,
}

impl Spinner {
    /// Every spinner, in the order the showcase lists them.
    pub const ALL: [Spinner; 7] = [
        Spinner::Sparkle,
        Spinner::Dots,
        Spinner::Comet,
        Spinner::Wave,
        Spinner::Arc,
        Spinner::Orbit,
        Spinner::Pulse,
    ];

    /// The spinner's lowercase name, as used by `--spinner` flags.
    pub fn name(self) -> &'static str {
        match self {
            Spinner::Sparkle => "sparkle",
            Spinner::Dots => "dots",
            Spinner::Comet => "comet",
            Spinner::Wave => "wave",
            Spinner::Arc => "arc",
            Spinner::Orbit => "orbit",
            Spinner::Pulse => "pulse",
        }
    }

    /// The spinner with this name, if there is one: handy for a `--spinner` flag.
    pub fn from_name(name: &str) -> Option<Spinner> {
        Spinner::ALL.into_iter().find(|s| s.name() == name)
    }

    /// Columns the spinner occupies; step marks are centred in the same space so labels never shift.
    pub fn width(self) -> usize {
        match self {
            Spinner::Sparkle | Spinner::Dots => 3,
            Spinner::Comet | Spinner::Wave => 5,
            _ => 1,
        }
    }

    /// What the spinner looks like `t` seconds in. Call it every frame with the time so far.
    pub fn frame(self, t: f32) -> Line {
        let rest = mix(TRACK, BERRY, 0.45);
        match self {
            Spinner::Sparkle => {
                const PHASE: [f32; 3] = [0.0, 2.1, 4.2];
                const SPEED: [f32; 3] = [4.6, 6.1, 3.9];
                (0..3).fold(Line::new(), |line, i| {
                    let b = ((t * SPEED[i] + PHASE[i]).sin() + 1.0) / 2.0;
                    // squared so each star rests dim and only briefly flares
                    let b = b * b;
                    line.push(STARS[((b * 4.0) as usize).min(3)], sample(&[rest, ROSE, SHINE], b))
                })
            }
            Spinner::Dots => (0..3).fold(Line::new(), |line, i| {
                let v = (t * 5.0 - i as f32 * 0.9).sin().max(0.0).powi(2);
                line.push("•", sample(&[rest, ROSE, PETAL], v))
            }),
            Spinner::Wave => (0..5).fold(Line::new(), |line, i| {
                let x = i as f32;
                // two waves at unrelated speeds, so the shape keeps changing like real audio
                let h = 0.5 + 0.3 * (t * 6.0 - x * 0.9).sin() + 0.2 * (t * 9.7 + x * 1.7).sin();
                let h = h.clamp(0.0, 1.0);
                line.push(
                    LEVELS[((h * 7.0).round() as usize).min(7)],
                    sample(&[rest, ROSE, SHINE], h * h),
                )
            }),
            Spinner::Comet => {
                // triangle wave across the track, eased at the ends so the turn feels soft
                let phase = (t * 0.9).rem_euclid(2.0);
                let x = if phase > 1.0 { 2.0 - phase } else { phase };
                let eased = x * x * (3.0 - 2.0 * x);
                let head = eased * 4.0;
                let dir = if phase > 1.0 { -1.0 } else { 1.0 };
                (0..5).fold(Line::new(), |line, i| {
                    let d = i as f32 - head;
                    let v = if d * dir <= 0.0 {
                        (-d.abs() / 1.1).exp()
                    } else {
                        (-d.abs() * 3.0).exp()
                    };
                    let glyph = if v > 0.9 {
                        "✦"
                    } else if v > 0.6 {
                        "●"
                    } else if v > 0.25 {
                        "•"
                    } else {
                        "·"
                    };
                    line.push(glyph, sample(&[rest, ROSE, SHINE], v))
                })
            }
            Spinner::Arc => Line::new().push(ARC[(t * 12.0) as usize % ARC.len()], ROSE),
            Spinner::Orbit => {
                let drift = ((t * 0.8).sin() + 1.0) / 2.0;
                Line::new().push(ORBIT[(t * 12.0) as usize % ORBIT.len()], sample(&RAMP, drift))
            }
            Spinner::Pulse => {
                let v = ((t * 3.2).sin() + 1.0) / 2.0;
                let glyph = if v > 0.92 {
                    "✦"
                } else if v > 0.66 {
                    "●"
                } else if v > 0.33 {
                    "•"
                } else {
                    "∙"
                };
                Line::new().push(glyph, sample(&[BERRY, ROSE, SHINE], v))
            }
        }
    }
}

/// A soft light passing slowly across the label, with a little glitter: letters near the
/// light now and then catch it and flash.
pub fn shimmer(label: &str, t: f32) -> Line {
    let n = label.chars().count() as f32;
    let pos = (t * 14.0).rem_euclid(n + 24.0) - 12.0;
    let tick = (t * 7.0) as u32;
    label.chars().enumerate().fold(Line::new(), |line, (j, c)| {
        let k = (-(j as f32 - pos).powi(2) / 12.0).exp();
        let glitter = noise(j as u32, tick) < 0.015 + 0.2 * k;
        let color = if glitter { SHINE } else { mix(SOFT, PETAL, k) };
        line.push(c.encode_utf8(&mut [0; 4]), color)
    })
}

/// The mark a step shows in the moment after it finishes: a quick twinkle that settles into
/// `●`. `None` once settled.
pub fn pop(age: f32) -> Option<Line> {
    let (glyph, color) = match age {
        a if a < 0.1 => ("✧", SHINE),
        a if a < 0.22 => ("✦", SHINE),
        a if a < 0.34 => ("✦", PETAL),
        a if a < 0.46 => ("✧", BLUSH),
        _ => return None,
    };
    Some(Line::new().push(glyph, color))
}

/// A thin line that fills berry → blush, with half-cell precision.
pub fn bar(fraction: f32, width: usize) -> Line {
    shiny_bar(fraction, width, None)
}

/// [`bar`] with a soft glint running along the filled part, for bars that are still moving.
pub fn live_bar(fraction: f32, width: usize, t: f32) -> Line {
    shiny_bar(fraction, width, Some(t))
}

fn shiny_bar(fraction: f32, width: usize, t: Option<f32>) -> Line {
    let halves = (fraction.clamp(0.0, 1.0) * width as f32 * 2.0).round() as usize;
    let filled = halves.div_ceil(2) as f32;
    let glint = t.map(|t| (t * 16.0).rem_euclid(filled + 10.0) - 5.0);
    (0..width).fold(Line::new(), |line, i| {
        let base = sample(&RAMP, i as f32 / width.saturating_sub(1).max(1) as f32);
        let c = glint.map_or(base, |g| mix(base, SHINE, (-(i as f32 - g).powi(2) / 3.0).exp() * 0.75));
        match halves.saturating_sub(i * 2) {
            0 => line.push("━", TRACK),
            1 => line.push("╸", c),
            _ => line.push("━", c),
        }
    })
}

/// How a progress bar is drawn, for [`Live::with_bar`](crate::Live::with_bar) and
/// [`Progress::with_bar`](crate::Progress::with_bar).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BarStyle {
    /// A thin line with a glint running along it: [`bar`] and [`live_bar`]. The default.
    #[default]
    Line,
    /// A row of bars like an audio track, lit up as it plays through: [`waveform`] and
    /// [`live_waveform`].
    Waveform,
}

impl BarStyle {
    /// The bar at `fraction`, `t` seconds into its animation.
    pub(crate) fn draw(self, fraction: f32, width: usize, t: f32) -> Line {
        match self {
            BarStyle::Line => live_bar(fraction, width, t),
            BarStyle::Waveform => live_waveform(fraction, width, t),
        }
    }
}

/// How tall the waveform is at column `i`, 0..1: a few slow waves at unrelated speeds plus a
/// little grain, so it looks like a recording but is the same shape every time it's drawn.
fn wave_height(i: usize) -> f32 {
    let x = i as f32;
    let h = 0.45 + 0.3 * (x * 0.55).sin() + 0.18 * (x * 1.7 + 1.3).sin() + 0.1 * (x * 0.13).sin();
    // squared, so most columns sit low and a few peak, like speech or music
    let h = (h + 0.3 * (noise(i as u32, 7) - 0.5)).clamp(0.0, 1.0);
    (h * h * 1.3).clamp(0.08, 1.0)
}

/// Progress as an audio waveform: played columns in berry → blush, the rest dim.
///
/// ```
/// println!("{}", tinsel::waveform(0.4, 30));
/// ```
pub fn waveform(fraction: f32, width: usize) -> Line {
    wave(fraction, width, None)
}

/// [`waveform`] still playing: the bars just behind the playhead bounce, and the playhead
/// itself glows.
pub fn live_waveform(fraction: f32, width: usize, t: f32) -> Line {
    wave(fraction, width, Some(t))
}

fn wave(fraction: f32, width: usize, t: Option<f32>) -> Line {
    let head = fraction.clamp(0.0, 1.0) * width as f32;
    (0..width).fold(Line::new(), |line, i| {
        let x = i as f32;
        let mut h = wave_height(i);
        // how much of this column has been played, so the edge moves smoothly between cells
        let played = (head - x).clamp(0.0, 1.0);
        if let Some(t) = t.filter(|_| played > 0.0 && head < width as f32) {
            // the last few played columns dance, fading with distance from the head
            let near = (-(head - x - 0.5).max(0.0) / 2.5).exp();
            h = (h + 0.3 * near * (t * 11.0 + x * 1.9).sin()).clamp(0.12, 1.0);
        }
        let glyph = LEVELS[((h * 7.0).round() as usize).min(7)];
        if played <= 0.0 {
            return line.push(glyph, TRACK);
        }
        let base = sample(&RAMP, x / width.saturating_sub(1).max(1) as f32);
        let lit = mix(TRACK, base, played);
        let glow = t.map_or(0.0, |_| (-(head - x - 0.5).abs() / 0.8).exp() * 0.7);
        line.push(glyph, mix(lit, SHINE, glow))
    })
}

/// A rounded box with a thin border, for summing up a run: a headline, then details.
///
/// ```
/// use tinsel::Card;
///
/// Card::success("deployed")
///     .aside("in 10.0s")
///     .kv("url", "https://my-app.example.com")
///     .kv("commands", "14 registered")
///     .print();
/// ```
#[derive(Clone, Debug, Default)]
pub struct Card {
    headline: Option<Line>,
    rows: Vec<Line>,
    width: Option<usize>,
}

impl Card {
    /// An empty card. Most cards start from [`Card::success`] or [`Card::failure`] instead.
    pub fn new() -> Self {
        Self::default()
    }

    /// A card headed `● headline` in rose.
    pub fn success(headline: &str) -> Self {
        Card::new().headline(Line::new().push("● ", ROSE).push(headline, TEXT))
    }

    /// A card headed `✕ headline` in red.
    pub fn failure(headline: &str) -> Self {
        Card::new().headline(Line::new().push("✕ ", RED).push(headline, TEXT))
    }

    /// Use any line as the headline. A blank line separates it from the rows below.
    pub fn headline(mut self, line: Line) -> Self {
        self.headline = Some(line);
        self
    }

    /// Quieter text after the headline, like how long something took.
    pub fn aside(mut self, text: &str) -> Self {
        let headline = self.headline.take().unwrap_or_default();
        self.headline = Some(headline.push(&format!(" {text}"), MUTED));
        self
    }

    /// A row of any styled line.
    pub fn line(mut self, line: Line) -> Self {
        self.rows.push(line);
        self
    }

    /// An empty row.
    pub fn blank(self) -> Self {
        self.line(Line::new())
    }

    /// A `key  value` row with the key muted.
    pub fn kv(self, key: &str, value: &str) -> Self {
        self.kv_with(key, value, TEXT)
    }

    /// A `key  value` row with the value in a colour of your choosing.
    pub fn kv_with(self, key: &str, value: &str, color: Rgb) -> Self {
        self.line(Line::kv(key, value, color))
    }

    /// Columns wide, border included (default: the content width).
    pub fn width(mut self, cols: usize) -> Self {
        self.width = Some(cols);
        self
    }

    /// The card as lines, with a blank line above so it sits apart from what came before.
    pub fn lines(&self) -> Block {
        let width = self.width.unwrap_or_else(|| content_width().saturating_sub(2));
        let mut out = Block::new();
        out.push("");
        out.extend(self.boxed(width).into_iter().map(|l| format!("  {l}")).collect());
        out
    }

    /// Just the box, `width` columns wide, with no margin or blank line around it.
    pub(crate) fn boxed(&self, width: usize) -> Block {
        let inner = width.saturating_sub(2);
        let rule = "─".repeat(inner);
        let side = paint(LINE, "│");
        let mut rows: Vec<&Line> = Vec::new();
        let blank = Line::new();
        if let Some(h) = &self.headline {
            rows.push(h);
            if !self.rows.is_empty() {
                rows.push(&blank);
            }
        }
        rows.extend(&self.rows);

        let mut out = Block::new();
        out.push(paint(LINE, &format!("╭{rule}╮")));
        for row in rows {
            let pad = inner.saturating_sub(2 + row.width());
            out.push(format!("{side}  {row}{}{side}", " ".repeat(pad)));
        }
        out.push(paint(LINE, &format!("╰{rule}╯")));
        out
    }

    /// The width set with [`width`](Card::width), if any.
    pub(crate) fn fixed_width(&self) -> Option<usize> {
        self.width
    }

    /// Print the card.
    pub fn print(&self) {
        self.lines().print();
    }
}

impl fmt::Display for Card {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.lines().fmt(f)
    }
}