tui-chart 0.2.0

Terminal chart widgets for ratatui: line, bar, candlestick, histogram, heatmap, gauge, radar, treemap, pie (via tui-piechart) and more, with themes, annotations and streaming
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
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
//! Concentric progress rings: [`RadialBar`] (value / max per ring) and
//! [`RingStack`] (percentages, 270° sweep). Drawn with braille dots.

#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,
    clippy::many_single_char_names,
    clippy::too_many_lines,
    clippy::cast_possible_wrap
)]

use crate::braille::Braille;
use crate::theme::{self, Theme};
use crate::PALETTE;
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::Span,
    widgets::Block,
};

fn text_width(s: &str) -> usize {
    Span::raw(s).width()
}

fn truncate(s: &str, max: usize) -> String {
    let mut out = String::new();
    let mut w = 0;
    for ch in s.chars() {
        let cw = text_width(ch.encode_utf8(&mut [0; 4]));
        if w + cw > max {
            break;
        }
        out.push(ch);
        w += cw;
    }
    out
}

/// One ring of a [`RadialBar`].
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RadialRing {
    /// Ring label (shown in the legend).
    pub label: String,
    /// Current value.
    pub value: f64,
    /// Value at which the ring is full.
    pub max: f64,
    /// Explicit color.
    pub color: Option<Color>,
}

impl RadialRing {
    /// A ring showing `value` out of `max`.
    pub fn new(label: impl Into<String>, value: f64, max: f64) -> Self {
        Self {
            label: label.into(),
            value,
            max,
            color: None,
        }
    }

    /// Explicit ring color (default: the theme / default palette, by position).
    pub fn color(mut self, c: Color) -> Self {
        self.color = Some(c);
        self
    }

    /// Fraction of the ring that is filled, `0..=1` (`0` for bad input).
    pub fn fraction(&self) -> f64 {
        if self.max.is_finite() && self.max > 0.0 && self.value.is_finite() {
            (self.value / self.max).clamp(0.0, 1.0)
        } else {
            0.0
        }
    }
}

/// Sweep of a [`RadialBar`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RadialSweep {
    /// 270° with the gap at the bottom (default).
    #[default]
    Arc,
    /// Full 360° rings starting at 12 o'clock.
    Full,
}

/// Concentric progress rings, outermost first, with an optional legend.
///
/// # Example
///
/// ```
/// use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
/// use tui_chart::{RadialBar, RadialRing};
///
/// let chart = RadialBar::new([
///     RadialRing::new("Move", 420.0, 600.0),
///     RadialRing::new("Exercise", 25.0, 30.0),
/// ]);
/// let area = Rect::new(0, 0, 50, 14);
/// let mut buf = Buffer::empty(area);
/// chart.render(area, &mut buf);
/// ```
#[derive(Debug, Clone, Default)]
pub struct RadialBar {
    rings: Vec<RadialRing>,
    sweep: RadialSweep,
    legend: bool,
    track: Option<Color>,
    theme: Option<Theme>,
    base: Option<Style>,
    block: Option<Block<'static>>,
}

impl RadialBar {
    /// Create a chart from rings, outermost first.
    pub fn new(rings: impl IntoIterator<Item = RadialRing>) -> Self {
        Self {
            rings: rings.into_iter().collect(),
            sweep: RadialSweep::Arc,
            legend: true,
            track: None,
            theme: None,
            base: None,
            block: None,
        }
    }

    setter!(
        /// 270° or full sweep.
        sweep: RadialSweep
    );
    setter!(
        /// Show the legend (default on, when there is room).
        legend: bool
    );
    opt_setter!(
        /// Color of the unfilled track (default: theme grid / dark gray).
        track => track: Color
    );
    theme_setter!(
        /// Apply a [`Theme`]; explicit ring colors win.
        theme
    );
    base_style_setter!(base);
    block_setter!(block, 'static);

    fn ring_color(&self, r: &RadialRing, i: usize) -> Color {
        r.color.unwrap_or_else(|| {
            self.theme
                .as_ref()
                .map_or(PALETTE[i % PALETTE.len()], |t| t.color(i))
        })
    }

    fn track_color(&self) -> Color {
        self.track
            .unwrap_or_else(|| self.theme.as_ref().map_or(Color::DarkGray, |t| t.grid))
    }

    fn draw_rings(&self, buf: &mut Buffer, area: Rect) {
        if self.rings.is_empty() || area.is_empty() {
            return;
        }
        let track = self.track_color();
        let mut g = Braille::new(area.width, area.height);
        let (w, h) = (g.dots_w(), g.dots_h());
        let c = (w / 2.0, h / 2.0);
        let r = w.min(h) / 2.0 - 0.5;
        let n = self.rings.len() as f64;
        let pitch = (r * 0.85) / n;
        if pitch >= 1.5 {
            let thick = (pitch - 1.2).max(1.5).min(pitch);
            let sw = match self.sweep {
                RadialSweep::Arc => (0.625, 0.75),
                RadialSweep::Full => (0.0, 1.0),
            };
            for (i, ring) in self.rings.iter().enumerate() {
                let outer = r - i as f64 * pitch;
                let inner = (outer - thick).max(0.0);
                let frac = ring.fraction();
                let color = self.ring_color(ring, i);
                g.ring(c, (inner, outer), sw, |pos| {
                    Some(if frac >= 1.0 || pos < frac {
                        (color, 1)
                    } else {
                        (track, 0)
                    })
                });
            }
        }
        g.blit(area, buf);
    }

    fn draw_legend(&self, buf: &mut Buffer, area: Rect) {
        let text = self
            .theme
            .as_ref()
            .map_or(Style::default(), |t| Style::default().fg(t.foreground));
        let label_w = area.width.saturating_sub(7) as usize;
        for (i, ring) in self.rings.iter().enumerate() {
            let y = area.y + i as u16;
            if y >= area.bottom() {
                break;
            }
            let color = self.ring_color(ring, i);
            buf.set_string(area.x, y, "■", Style::default().fg(color));
            let label = truncate(&ring.label, label_w);
            buf.set_string(area.x + 2, y, &label, text);
            let pct = format!("{:.0}%", ring.fraction() * 100.0);
            let px = area.right().saturating_sub(pct.len() as u16);
            if px > area.x + 2 + text_width(&label) as u16 {
                buf.set_string(
                    px,
                    y,
                    &pct,
                    Style::default().fg(color).add_modifier(Modifier::BOLD),
                );
            }
        }
    }

    fn render_into(&self, area: Rect, buf: &mut Buffer) {
        let area = area.intersection(buf.area);
        let inner = theme::begin(&self.block, &self.theme, self.base, area, buf);
        let (chart, legend) = split(inner, &self.rings, self.legend);
        self.draw_rings(buf, chart);
        if let Some(l) = legend {
            self.draw_legend(buf, l);
        }
    }
}

widget_impls!(RadialBar);

#[cfg(feature = "theme")]
impl tui_nuances::Themed for RadialBar {
    fn apply_theme(self, theme: &tui_nuances::Theme) -> Self {
        self.theme(theme)
    }
}

/// Splits `inner` into the rings area and an optional legend area.
fn split(inner: Rect, rings: &[RadialRing], legend: bool) -> (Rect, Option<Rect>) {
    if !legend || rings.is_empty() {
        return (inner, None);
    }
    let longest = rings
        .iter()
        .map(|r| text_width(&r.label))
        .max()
        .unwrap_or(0);
    let lw = (longest + 8).clamp(14, 32) as u16;
    // Keep at least a 12-column chart.
    if inner.width < lw + 12 {
        return (inner, None);
    }
    let chart_w = inner.width - lw - 1;
    let chart = Rect::new(inner.x, inner.y, chart_w, inner.height);
    let ly = inner.y + inner.height.saturating_sub(rings.len() as u16) / 2;
    let leg = Rect::new(
        inner.x + chart_w + 1,
        ly,
        lw,
        (rings.len() as u16).min(inner.bottom() - ly),
    );
    (chart, Some(leg))
}

/// Concentric 270° rings, one per `(label, percent)`, with a legend.
///
/// # Example
///
/// ```
/// use tui_chart::RingStack;
///
/// let stack = RingStack::new([("CPU", 72.0), ("RAM", 48.0), ("Disk", 91.0)]);
/// # let _ = stack;
/// ```
#[derive(Debug, Clone, Default)]
pub struct RingStack {
    items: Vec<(String, f64)>,
    colors: Vec<Color>,
    inner: RadialBar,
}

impl RingStack {
    /// Create a ring stack from `(label, percent)` pairs, outermost first.
    pub fn new<S: Into<String>>(items: impl IntoIterator<Item = (S, f64)>) -> Self {
        Self {
            items: items.into_iter().map(|(l, v)| (l.into(), v)).collect(),
            colors: Vec::new(),
            inner: RadialBar::new([]),
        }
    }

    /// Ring colors by position (the theme / palette fills in the rest).
    pub fn colors(mut self, colors: impl IntoIterator<Item = Color>) -> Self {
        self.colors = colors.into_iter().collect();
        self
    }

    /// Show or hide the legend.
    pub fn legend(mut self, show: bool) -> Self {
        self.inner = self.inner.legend(show);
        self
    }

    /// Color of the unfilled track.
    pub fn track(mut self, c: Color) -> Self {
        self.inner = self.inner.track(c);
        self
    }

    /// Apply a [`Theme`]; explicit colors win.
    #[cfg(feature = "theme")]
    pub fn theme(mut self, theme: impl Into<Theme>) -> Self {
        self.inner = self.inner.theme(theme);
        self
    }

    /// Base style painted over the whole area.
    pub fn base_style(mut self, style: Style) -> Self {
        self.inner = self.inner.base_style(style);
        self
    }

    /// Wrap the chart in a [`Block`].
    pub fn block(mut self, b: Block<'static>) -> Self {
        self.inner = self.inner.block(b);
        self
    }

    fn render_into(&self, area: Rect, buf: &mut Buffer) {
        let rings = self.items.iter().enumerate().map(|(i, (l, pct))| {
            let mut r = RadialRing::new(l.clone(), *pct, 100.0);
            r.color = self.colors.get(i).copied();
            r
        });
        let mut bar = self.inner.clone();
        bar.rings = rings.collect();
        bar.render_into(area, buf);
    }
}

widget_impls!(RingStack);

#[cfg(feature = "theme")]
impl tui_nuances::Themed for RingStack {
    fn apply_theme(self, theme: &tui_nuances::Theme) -> Self {
        self.theme(theme)
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::assert_is_empty, clippy::float_cmp)]
    use super::*;
    use ratatui::widgets::Widget;

    fn draw(w: impl Widget, width: u16, height: u16) -> Buffer {
        let a = Rect::new(0, 0, width, height);
        let mut b = Buffer::empty(a);
        w.render(a, &mut b);
        b
    }

    fn text(b: &Buffer) -> String {
        b.content()
            .iter()
            .map(ratatui::buffer::Cell::symbol)
            .collect()
    }

    fn rings() -> Vec<RadialRing> {
        vec![
            RadialRing::new("Move", 420.0, 600.0),
            RadialRing::new("Exercise", 25.0, 30.0).color(Color::Red),
            RadialRing::new("Stand", 9.0, 12.0),
        ]
    }

    #[test]
    fn fraction_guards() {
        assert_eq!(RadialRing::new("a", 5.0, 0.0).fraction(), 0.0);
        assert_eq!(RadialRing::new("a", f64::NAN, 10.0).fraction(), 0.0);
        assert_eq!(RadialRing::new("a", 50.0, 10.0).fraction(), 1.0);
        assert_eq!(RadialRing::new("a", -5.0, 10.0).fraction(), 0.0);
        assert!((RadialRing::new("a", 3.0, 12.0).fraction() - 0.25).abs() < 1e-12);
    }

    #[test]
    fn renders_rings_and_legend() {
        let b = draw(RadialBar::new(rings()), 50, 14);
        let t = text(&b);
        assert!(t.contains("Move") && t.contains("Exercise") && t.contains("70%"));
        assert!(b
            .content()
            .iter()
            .any(|c| c.fg == Color::Red && c.symbol() != " "));
    }

    #[test]
    fn legend_can_be_hidden() {
        let t = text(&draw(RadialBar::new(rings()).legend(false), 50, 14));
        assert!(!t.contains("Move"));
    }

    #[test]
    fn full_sweep_differs_from_arc() {
        let a = draw(RadialBar::new(rings()).legend(false), 30, 14);
        let b = draw(
            RadialBar::new(rings())
                .legend(false)
                .sweep(RadialSweep::Full),
            30,
            14,
        );
        assert_ne!(a, b);
    }

    #[test]
    fn value_changes_picture() {
        let lo = draw(
            RadialBar::new(vec![RadialRing::new("a", 10.0, 100.0)]).legend(false),
            20,
            10,
        );
        let hi = draw(
            RadialBar::new(vec![RadialRing::new("a", 90.0, 100.0)]).legend(false),
            20,
            10,
        );
        assert_ne!(lo, hi);
    }

    #[test]
    fn robust_to_odd_sizes_and_input() {
        for (w, h) in [(0, 0), (1, 1), (4, 2), (13, 3), (60, 30)] {
            let _ = draw(RadialBar::new(rings()), w, h);
            let _ = draw(RadialBar::new(Vec::new()), w, h);
            let _ = draw(
                RadialBar::new(vec![RadialRing::new("x", f64::NAN, f64::NAN)]),
                w,
                h,
            );
            let many: Vec<RadialRing> = (0..40)
                .map(|i| RadialRing::new("r", f64::from(i), 40.0))
                .collect();
            let _ = draw(RadialBar::new(many), w, h);
        }
    }

    #[test]
    fn ring_stack_renders_percentages() {
        let t = text(&draw(
            RingStack::new([("CPU", 72.0), ("RAM", 48.0)]).block(Block::bordered().title("Sys")),
            50,
            14,
        ));
        assert!(t.contains("CPU") && t.contains("72%") && t.contains("Sys"));
    }

    #[test]
    fn ring_stack_custom_colors() {
        let b = draw(
            RingStack::new([("A", 100.0)])
                .colors([Color::Magenta])
                .legend(false),
            20,
            10,
        );
        assert!(b
            .content()
            .iter()
            .any(|c| c.fg == Color::Magenta && c.symbol() != " "));
    }
}