ratcn 0.0.1

Themeable terminal UI components and interaction runtime for 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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
use ratatui::{
    buffer::Buffer,
    layout::{Direction, Rect},
    style::{Color, Modifier, Style},
    symbols,
    text::Line,
    widgets::{Bar, BarChart, BarGroup, Widget},
};

use crate::Theme;

/// Colors for a [`BarChartWidget`].
///
/// A chart has no interaction states, so unlike the other style structs there is
/// one color per role rather than one per role per state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BarChartStyle {
    /// Default text color for anything not covered by the roles below.
    pub foreground: Color,
    /// Fill behind the whole chart.
    pub background: Color,
    /// The bars themselves.
    pub bar: Color,
    /// The value printed inside each bar, so it must contrast with `bar`.
    pub value_foreground: Color,
    /// Vertical bar labels and group labels. Ratatui's horizontal renderer does
    /// not apply its chart-level label style to ordinary bar labels; style those
    /// labels directly on their [`Line`](ratatui::text::Line) or spans.
    pub label_foreground: Color,
}

impl BarChartStyle {
    /// A neutral style using plain ANSI colors, for painting without a
    /// [`Theme`]. Prefer [`from_theme`](Self::from_theme) when one is available.
    #[must_use]
    pub const fn fallback() -> Self {
        Self {
            foreground: Color::Reset,
            background: Color::Reset,
            bar: Color::Cyan,
            value_foreground: Color::Reset,
            label_foreground: Color::DarkGray,
        }
    }

    /// Derive chart colors from `theme`. Bars use the primary color, with the
    /// value drawn in the primary foreground so it stays legible on top.
    #[must_use]
    pub const fn from_theme(theme: &Theme) -> Self {
        Self {
            foreground: theme.foreground,
            background: theme.field,
            bar: theme.primary,
            value_foreground: theme.primary_foreground,
            label_foreground: theme.muted_foreground,
        }
    }
}

/// A themed adapter over ratatui's [`BarChart`], adding grouping and a value
/// display switch.
///
/// It is not a component in the sense the rest of this crate uses the word.
/// Every bar, label, and value is a ratatui type, the drawing is ratatui's, and
/// what this adds on top is three things:
///
/// - theme colors, from a [`Theme`] via [`themed`](Self::themed) or an explicit
///   [`BarChartStyle`]
/// - [`grouped`](Self::grouped) charts held as [`BarChartGroup`], so
///   widget-level options reach grouped bars too
/// - [`show_values`](Self::show_values), one switch for whether bars print
///   their value
///
/// Anything else you want from a bar chart, you configure on the ratatui
/// [`Bar`]s you pass in — including per-bar color, see
/// [Per-bar colors](#per-bar-colors) below.
///
/// Charts take no focus and handle no events, so there is no interactive half.
///
/// **Usable in any ratatui app.** Nothing here depends on
/// [`Ratcn`](crate::runtime::Ratcn) or the component layer — it is an ordinary
/// [`Widget`], so `frame.render_widget(...)` is all it needs.
///
/// # Per-bar colors
///
/// Bars are passed through to ratatui untouched, so ratatui's own
/// [`Bar::style`] works: it patches over the chart-wide bar color, which is how
/// you color one bar, or one series inside a [`grouped`](Self::grouped) chart.
///
/// ```
/// # use ratatui::{style::{Color, Style}, widgets::Bar};
/// # use ratcn::BarChartWidget;
/// let bars = vec![
///     Bar::default().value(12),
///     Bar::default().value(18).style(Style::default().fg(Color::Red)),
/// ];
/// # let _ = BarChartWidget::new(bars);
/// ```
///
/// The value text printed inside a bar is not covered by this: it keeps the
/// [`value_foreground`](BarChartStyle::value_foreground) on the chart-wide
/// [`bar`](BarChartStyle::bar) background, so a recolored bar shows its value
/// against the chart's bar color rather than its own. Set
/// [`Bar::value_style`] on that bar to match, or turn values off with
/// [`show_values`](Self::show_values).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BarChartWidget<'a> {
    data: BarChartData<'a>,
    style: BarChartStyle,
    max_value: Option<u64>,
    direction: Direction,
    bar_width: u16,
    bar_gap: u16,
    group_gap: u16,
    bar_set: symbols::bar::Set<'a>,
    show_values: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum BarChartData<'a> {
    Bars(Vec<Bar<'a>>),
    Groups(Vec<BarChartGroup<'a>>),
}

/// One group of bars for [`BarChartWidget::grouped`]. Mirrors ratatui's
/// [`BarGroup`] but keeps its bars readable, so widget-level options such as
/// [`BarChartWidget::show_values`] apply to grouped bars too.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BarChartGroup<'a> {
    label: Option<Line<'a>>,
    bars: Vec<Bar<'a>>,
}

impl<'a> BarChartGroup<'a> {
    /// A group of bars with no group label.
    #[must_use]
    pub fn new(bars: impl IntoIterator<Item = Bar<'a>>) -> Self {
        Self {
            label: None,
            bars: bars.into_iter().collect(),
        }
    }

    /// Label the group as a whole — the category name under a cluster, where
    /// each bar inside carries its own series label.
    #[must_use]
    pub fn label(mut self, label: impl Into<Line<'a>>) -> Self {
        self.label = Some(label.into());
        self
    }
}

impl<'a> BarChartWidget<'a> {
    /// A vertical chart of `bars`, using [`BarChartStyle::fallback`].
    #[must_use]
    pub fn new(bars: impl IntoIterator<Item = Bar<'a>>) -> Self {
        Self::with_data(BarChartData::Bars(bars.into_iter().collect()))
    }

    /// The same as [`new`](Self::new), named for when the direction matters to
    /// the reader.
    #[must_use]
    pub fn vertical(bars: impl IntoIterator<Item = Bar<'a>>) -> Self {
        Self::new(bars)
    }

    /// A chart whose bars run left to right. Useful when labels are long, since
    /// a horizontal bar has a whole row for its label.
    #[must_use]
    pub fn horizontal(bars: impl IntoIterator<Item = Bar<'a>>) -> Self {
        Self::new(bars).direction(Direction::Horizontal)
    }

    /// A chart of clustered bars, for comparing several series across
    /// categories. See [`BarChartGroup`]. Use
    /// [`direction`](Self::direction) to make a grouped chart horizontal. A
    /// horizontal group label needs a non-zero [`group_gap`](Self::group_gap).
    #[must_use]
    pub fn grouped(groups: impl IntoIterator<Item = BarChartGroup<'a>>) -> Self {
        Self::with_data(BarChartData::Groups(groups.into_iter().collect()))
    }

    fn with_data(data: BarChartData<'a>) -> Self {
        Self {
            data,
            style: BarChartStyle::fallback(),
            max_value: None,
            direction: Direction::Vertical,
            bar_width: 3,
            bar_gap: 1,
            group_gap: 0,
            bar_set: symbols::bar::NINE_LEVELS,
            show_values: true,
        }
    }

    /// Take colors from `theme`.
    #[must_use]
    pub const fn themed(mut self, theme: &Theme) -> Self {
        self.style = BarChartStyle::from_theme(theme);
        self
    }

    /// Use these exact colors, ignoring any theme.
    #[must_use]
    pub const fn style(mut self, style: BarChartStyle) -> Self {
        self.style = style;
        self
    }

    /// Pin the value a full-height bar represents.
    ///
    /// By default the tallest bar fills the chart, which rescales the whole
    /// chart whenever the data changes — fine for a snapshot, misleading for
    /// something updating live. Set this to keep the scale steady across
    /// frames, or to compare two charts against each other.
    #[must_use]
    pub const fn max_value(mut self, max_value: u64) -> Self {
        self.max_value = Some(max_value);
        self
    }

    /// Whether bars run up (`Vertical`, the default) or across (`Horizontal`).
    ///
    /// This takes ratatui's [`ratatui::layout::Direction`] — the layout axis.
    /// The runtime's own `Forward`/`Backward` enum for stepping through items
    /// is [`Step`](crate::runtime::Step), a different type under a different
    /// name.
    #[must_use]
    pub const fn direction(mut self, direction: Direction) -> Self {
        self.direction = direction;
        self
    }

    /// Whether each bar prints its value inside it. On by default; turn it off
    /// for narrow bars where the number will not fit.
    ///
    /// Applies to grouped charts too, which is why groups are held as
    /// [`BarChartGroup`] rather than ratatui's `BarGroup`.
    #[must_use]
    pub const fn show_values(mut self, show_values: bool) -> Self {
        self.show_values = show_values;
        self
    }

    /// Minimum width for a vertical chart's bar grouping axis, in cells.
    ///
    /// Horizontal charts size their bar lengths from the supplied paint area, so
    /// this returns zero for them. Group labels and value text can require more
    /// space on the other axis and are intentionally not guessed here.
    #[must_use]
    pub fn width(&self) -> u16 {
        if self.direction == Direction::Vertical {
            self.grouping_span()
        } else {
            0
        }
    }

    /// Minimum height for a horizontal chart's bar grouping axis, in rows.
    ///
    /// Vertical charts size their bar lengths from the supplied paint area, so
    /// this returns zero for them. Group labels and value text can require more
    /// space on the other axis and are intentionally not guessed here.
    #[must_use]
    pub fn height(&self) -> u16 {
        if self.direction == Direction::Horizontal {
            self.grouping_span()
        } else {
            0
        }
    }

    /// Cells across each bar. Defaults to 3, which leaves room for a two-digit
    /// value.
    #[must_use]
    pub const fn bar_width(mut self, width: u16) -> Self {
        self.bar_width = width;
        self
    }

    /// Blank cells between adjacent bars. Defaults to 1.
    #[must_use]
    pub const fn bar_gap(mut self, gap: u16) -> Self {
        self.bar_gap = gap;
        self
    }

    /// Extra blank cells between groups, on top of [`bar_gap`](Self::bar_gap).
    /// Defaults to 0, and only matters for a [`grouped`](Self::grouped) chart.
    /// Horizontal group labels are drawn in this gap, so they require a value
    /// greater than 0.
    #[must_use]
    pub const fn group_gap(mut self, gap: u16) -> Self {
        self.group_gap = gap;
        self
    }

    /// The glyphs used to draw vertical bars.
    ///
    /// A vertical bar rarely ends exactly on a cell boundary, so its top cell
    /// is drawn with a partial block. The default (`NINE_LEVELS`) gives the
    /// smoothest result; coarser sets exist for terminals whose fonts lack
    /// those glyphs. Horizontal bars use only whole `full` and `empty` cells.
    #[must_use]
    pub fn bar_set(mut self, bar_set: symbols::bar::Set<'a>) -> Self {
        self.bar_set = bar_set;
        self
    }

    fn grouping_span(&self) -> u16 {
        let group_spans: Vec<u16> = match &self.data {
            BarChartData::Bars(bars) => vec![bar_span(bars.len(), self.bar_width, self.bar_gap)],
            BarChartData::Groups(groups) => groups
                .iter()
                .map(|group| bar_span(group.bars.len(), self.bar_width, self.bar_gap))
                .collect(),
        };
        group_spans
            .into_iter()
            .enumerate()
            .fold(0, |span, (index, group)| {
                span.saturating_add(group).saturating_add(if index > 0 {
                    self.group_gap
                } else {
                    0
                })
            })
    }
}

fn bar_span(count: usize, width: u16, gap: u16) -> u16 {
    let count = u16::try_from(count).unwrap_or(u16::MAX);
    count
        .saturating_mul(width)
        .saturating_add(count.saturating_sub(1).saturating_mul(gap))
}

impl<'a> Widget for BarChartWidget<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let show_values = self.show_values;
        let strip_values = move |bars: Vec<Bar<'a>>| -> Vec<Bar<'a>> {
            if show_values {
                bars
            } else {
                bars.into_iter().map(|bar| bar.text_value("")).collect()
            }
        };
        let mut chart = match self.data {
            BarChartData::Bars(bars) => BarChart::new(strip_values(bars)),
            BarChartData::Groups(groups) => {
                let groups: Vec<BarGroup<'a>> = groups
                    .into_iter()
                    .map(|group| {
                        let bars = strip_values(group.bars);
                        match group.label {
                            Some(label) => BarGroup::with_label(label, bars),
                            None => BarGroup::new(bars),
                        }
                    })
                    .collect();
                BarChart::grouped(groups)
            }
        };
        chart = chart
            .style(
                Style::default()
                    .fg(self.style.foreground)
                    .bg(self.style.background),
            )
            .bar_style(
                Style::default()
                    .fg(self.style.bar)
                    .bg(self.style.background),
            )
            .value_style(
                Style::default()
                    .fg(self.style.value_foreground)
                    .bg(self.style.bar)
                    .add_modifier(Modifier::BOLD),
            )
            .label_style(
                Style::default()
                    .fg(self.style.label_foreground)
                    .bg(self.style.background),
            )
            .bar_width(self.bar_width)
            .bar_gap(self.bar_gap)
            .group_gap(self.group_gap)
            .bar_set(self.bar_set)
            .direction(self.direction);
        if let Some(max_value) = self.max_value {
            chart = chart.max(max_value);
        }
        chart.render(area, buf);
    }
}

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

    /// The constructors take an iterator, as `List`, `Select`, and `Tabs` do,
    /// so mapped or filtered data goes straight in.
    #[test]
    fn constructors_accept_any_iterator_of_bars() {
        let readings = [3u64, 0, 7];
        let from_iterator = BarChartWidget::new(
            readings
                .iter()
                .filter(|value| **value > 0)
                .map(|value| Bar::default().value(*value)),
        );
        let from_vec = BarChartWidget::new(vec![Bar::default().value(3), Bar::default().value(7)]);
        assert_eq!(
            from_iterator.width(),
            from_vec.width(),
            "the filtered iterator yields the same two bars a vec would"
        );

        let grouped = BarChartWidget::grouped(
            ["a", "b"]
                .into_iter()
                .map(|label| BarChartGroup::new([Bar::default().value(1)]).label(label)),
        );
        assert_eq!(
            grouped.width(),
            BarChartWidget::grouped(vec![
                BarChartGroup::new([Bar::default().value(1)]).label("a"),
                BarChartGroup::new([Bar::default().value(1)]).label("b"),
            ])
            .width()
        );
    }

    #[test]
    fn themed_bars_paint_the_primary_color() {
        let theme = Theme::default_dark();
        let area = Rect::new(0, 0, 3, 3);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![Bar::default().value(9)])
            .themed(&theme)
            .show_values(false)
            .render(area, &mut buffer);

        // The themed style paints bars in `primary` on the inset `field` well.
        let cell = buffer.cell((0, 2)).expect("bar cell");
        assert_eq!(cell.fg, theme.primary);
        assert_eq!(cell.bg, theme.field);
    }

    #[test]
    fn explicit_style_overrides_paint_exact_colors() {
        let style = BarChartStyle {
            foreground: Color::White,
            background: Color::Black,
            bar: Color::Magenta,
            value_foreground: Color::Yellow,
            label_foreground: Color::Gray,
        };
        let area = Rect::new(0, 0, 1, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![Bar::default().value(1)])
            .style(style)
            .max_value(1)
            .show_values(false)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut buffer);

        let cell = buffer.cell((0, 0)).expect("bar cell");
        assert_eq!(cell.fg, style.bar);
        assert_eq!(cell.bg, style.background);
    }

    #[test]
    fn per_bar_style_patches_over_the_chart_bar_color() {
        // Per-bar color is documented as already reachable through ratatui's
        // own `Bar::style`, so the pass-through must stay intact: bars are
        // handed to ratatui unmodified, and its per-bar style wins.
        let style = BarChartStyle {
            bar: Color::Magenta,
            ..BarChartStyle::fallback()
        };
        let area = Rect::new(0, 0, 2, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![
            Bar::default().value(1),
            Bar::default()
                .value(1)
                .style(Style::default().fg(Color::Red)),
        ])
        .style(style)
        .max_value(1)
        .show_values(false)
        .bar_width(1)
        .bar_gap(0)
        .render(area, &mut buffer);

        assert_eq!(
            buffer.cell((0, 0)).expect("chart-colored bar").fg,
            style.bar
        );
        assert_eq!(buffer.cell((1, 0)).expect("per-bar color").fg, Color::Red);
    }

    #[test]
    fn a_recolored_bar_keeps_the_chart_wide_value_background() {
        // The documented caveat to per-bar color: the value text is painted
        // with the chart's value style, so it does not follow `Bar::style`.
        let style = BarChartStyle {
            bar: Color::Magenta,
            value_foreground: Color::Yellow,
            ..BarChartStyle::fallback()
        };
        let area = Rect::new(0, 0, 1, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![
            Bar::default()
                .value(1)
                .style(Style::default().fg(Color::Red)),
        ])
        .style(style)
        .max_value(1)
        .bar_width(1)
        .bar_gap(0)
        .render(area, &mut buffer);

        let cell = buffer.cell((0, 0)).expect("value cell");
        assert_eq!(cell.fg, style.value_foreground);
        assert_eq!(cell.bg, style.bar);
    }

    #[test]
    fn max_value_pins_the_scale_so_a_sub_max_bar_leaves_headroom() {
        // Without `max_value` the tallest bar always fills the chart; pinning
        // the maximum is what keeps the scale steady across frames.
        let area = Rect::new(0, 0, 1, 2);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![Bar::default().value(1)])
            .max_value(2)
            .show_values(false)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((0, 0)).expect("headroom cell").symbol(), " ");
        assert_eq!(buffer.cell((0, 1)).expect("bar cell").symbol(), "");
    }

    #[test]
    fn horizontal_bars_render_left_to_right() {
        let area = Rect::new(0, 0, 4, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::horizontal(vec![Bar::default().value(1)])
            .max_value(1)
            .show_values(false)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((3, 0)).expect("bar end cell").symbol(), "");
    }

    #[test]
    fn horizontal_ordinary_labels_keep_their_own_style() {
        let mut style = BarChartStyle::fallback();
        style.foreground = Color::White;
        style.label_foreground = Color::Yellow;
        style.value_foreground = Color::Green;
        let area = Rect::new(0, 0, 10, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::horizontal(vec![Bar::default().label("Plain").value(1)])
            .style(style)
            .max_value(1)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut buffer);

        assert_eq!(
            buffer.cell((0, 0)).expect("label cell").fg,
            style.foreground,
            "ratatui does not apply chart label_style to ordinary horizontal labels"
        );
        assert_eq!(
            buffer.cell((6, 0)).expect("value cell").fg,
            style.value_foreground
        );
    }

    #[test]
    fn direction_builder_changes_the_chart_orientation() {
        let area = Rect::new(0, 0, 4, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![Bar::default().value(1)])
            .direction(Direction::Horizontal)
            .max_value(1)
            .show_values(false)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((3, 0)).expect("bar end cell").symbol(), "");
    }

    #[test]
    fn custom_bar_set_controls_rendered_symbols() {
        let area = Rect::new(0, 0, 1, 1);
        let mut buffer = Buffer::empty(area);
        let bar_set = symbols::bar::Set {
            full: "x",
            seven_eighths: "x",
            three_quarters: "x",
            five_eighths: "x",
            half: "x",
            three_eighths: "x",
            one_quarter: "x",
            one_eighth: "x",
            empty: ".",
        };

        BarChartWidget::new(vec![Bar::default().value(1)])
            .max_value(1)
            .show_values(false)
            .bar_width(1)
            .bar_gap(0)
            .bar_set(bar_set)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((0, 0)).expect("bar cell").symbol(), "x");
    }

    #[test]
    fn group_gap_separates_two_groups() {
        let area = Rect::new(0, 0, 3, 1);
        let mut buffer = Buffer::empty(area);
        let groups = vec![
            BarChartGroup::new(vec![Bar::default().value(1)]),
            BarChartGroup::new(vec![Bar::default().value(1)]),
        ];

        BarChartWidget::grouped(groups)
            .max_value(1)
            .bar_width(1)
            .bar_gap(0)
            .group_gap(1)
            .show_values(false)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((0, 0)).expect("first bar").symbol(), "");
        assert_eq!(buffer.cell((1, 0)).expect("group gap").symbol(), " ");
        assert_eq!(buffer.cell((2, 0)).expect("second bar").symbol(), "");
    }

    #[test]
    fn grouped_horizontal_charts_render_bars_and_group_labels() {
        let area = Rect::new(0, 0, 4, 4);
        let mut buffer = Buffer::empty(area);
        let groups = vec![
            BarChartGroup::new(vec![Bar::default().value(1)]).label("G1"),
            BarChartGroup::new(vec![Bar::default().value(1)]).label("G2"),
        ];

        BarChartWidget::grouped(groups)
            .direction(Direction::Horizontal)
            .max_value(1)
            .bar_width(1)
            .bar_gap(0)
            .group_gap(1)
            .show_values(false)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((3, 0)).expect("first bar end").symbol(), "");
        assert_eq!(
            buffer.cell((0, 1)).expect("first group label").symbol(),
            "G"
        );
        assert_eq!(buffer.cell((3, 2)).expect("second bar end").symbol(), "");
        assert_eq!(
            buffer.cell((0, 3)).expect("second group label").symbol(),
            "G"
        );
    }

    #[test]
    fn grouped_horizontal_labels_distinguish_bar_and_group_style() {
        use ratatui::style::Stylize;

        let mut style = BarChartStyle::fallback();
        style.foreground = Color::White;
        style.label_foreground = Color::Yellow;
        let area = Rect::new(0, 0, 10, 3);
        let mut buffer = Buffer::empty(area);
        let groups = vec![
            BarChartGroup::new(vec![
                Bar::default().label("Plain").value(1),
                Bar::default().label(Line::from("Red").red()).value(1),
            ])
            .label("G"),
        ];

        BarChartWidget::grouped(groups)
            .style(style)
            .direction(Direction::Horizontal)
            .max_value(1)
            .bar_width(1)
            .bar_gap(0)
            .group_gap(1)
            .show_values(false)
            .render(area, &mut buffer);

        assert_eq!(
            buffer.cell((0, 0)).expect("plain bar label").fg,
            style.foreground
        );
        assert_eq!(
            buffer.cell((0, 1)).expect("explicit bar label").fg,
            Color::Red
        );
        assert_eq!(
            buffer.cell((6, 2)).expect("group label").fg,
            style.label_foreground
        );
    }

    #[test]
    fn custom_text_value_is_preserved() {
        let area = Rect::new(0, 0, 2, 1);
        let mut buffer = Buffer::empty(area);

        BarChartWidget::new(vec![Bar::default().value(1).text_value("ok")])
            .max_value(1)
            .bar_width(2)
            .bar_gap(0)
            .render(area, &mut buffer);

        assert_eq!(buffer.cell((0, 0)).expect("value start").symbol(), "o");
        assert_eq!(buffer.cell((1, 0)).expect("value end").symbol(), "k");
    }

    #[test]
    fn show_values_false_hides_standalone_bar_values() {
        // Values paint by default; `show_values(false)` strips them so the
        // cell shows the bar glyph instead of the value text.
        let area = Rect::new(0, 0, 1, 2);
        let mut with_values = Buffer::empty(area);
        let mut without_values = Buffer::empty(area);
        let bars = || vec![Bar::default().value(9)];

        BarChartWidget::new(bars())
            .max_value(9)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut with_values);
        BarChartWidget::new(bars())
            .max_value(9)
            .bar_width(1)
            .bar_gap(0)
            .show_values(false)
            .render(area, &mut without_values);

        assert_eq!(with_values.cell((0, 1)).expect("value cell").symbol(), "9");
        assert_eq!(without_values.cell((0, 1)).expect("bar cell").symbol(), "");
    }

    #[test]
    fn vertical_width_measures_exactly_what_paints() {
        // Three 2-wide bars with 1-cell gaps: width() must be 2+1+2+1+2 = 8,
        // and a buffer of exactly that width must hold the last bar whole.
        let chart = || {
            BarChartWidget::new(vec![
                Bar::default().value(1),
                Bar::default().value(1),
                Bar::default().value(1),
            ])
            .max_value(1)
            .show_values(false)
            .bar_width(2)
            .bar_gap(1)
        };
        let width = chart().width();
        assert_eq!(width, 8);

        let area = Rect::new(0, 0, width, 1);
        let mut buffer = Buffer::empty(area);
        chart().render(area, &mut buffer);
        // The rightmost cell of the final bar is painted: nothing is truncated
        // when the area matches the measurement.
        assert_eq!(
            buffer.cell((width - 1, 0)).expect("last bar end").symbol(),
            ""
        );

        // One cell narrower and the final bar no longer fits whole, so the cell
        // where its right edge would land is not a bar cell — the measurement
        // is exact, not merely sufficient.
        let narrow = Rect::new(0, 0, width - 1, 1);
        let mut narrow_buffer = Buffer::empty(narrow);
        chart().render(narrow, &mut narrow_buffer);
        assert_ne!(
            narrow_buffer.cell((width - 2, 0)).expect("cell").symbol(),
            ""
        );
    }

    #[test]
    fn grouped_horizontal_height_measures_exactly_what_paints() {
        // Two groups of two 1-row bars with a 1-row group gap: height() must be
        // 2+1+2 = 5, and a buffer of exactly that height holds the last bar.
        let chart = || {
            BarChartWidget::grouped(vec![
                BarChartGroup::new(vec![Bar::default().value(1), Bar::default().value(1)]),
                BarChartGroup::new(vec![Bar::default().value(1), Bar::default().value(1)]),
            ])
            .direction(Direction::Horizontal)
            .max_value(1)
            .show_values(false)
            .bar_width(1)
            .bar_gap(0)
            .group_gap(1)
        };
        let height = chart().height();
        assert_eq!(height, 5);

        let area = Rect::new(0, 0, 4, height);
        let mut buffer = Buffer::empty(area);
        chart().render(area, &mut buffer);
        // The bottom cell of the final bar is painted on the last measured row.
        assert_eq!(
            buffer.cell((3, height - 1)).expect("last bar end").symbol(),
            ""
        );

        // One row shorter loses the final bar entirely.
        let bar_cells = |buffer: &Buffer| {
            buffer
                .content()
                .iter()
                .filter(|cell| cell.symbol() == "")
                .count()
        };
        let short = Rect::new(0, 0, 4, height - 1);
        let mut short_buffer = Buffer::empty(short);
        chart().render(short, &mut short_buffer);
        assert!(bar_cells(&short_buffer) < bar_cells(&buffer));
    }

    #[test]
    fn show_values_false_hides_grouped_bar_values() {
        // `show_values` must mean the same thing in every mode: hiding values
        // on a grouped chart strips the value text from every grouped bar,
        // exactly as it does for standalone bars.
        let area = Rect::new(0, 0, 1, 2);
        let mut with_values = Buffer::empty(area);
        let mut without_values = Buffer::empty(area);
        let group = || BarChartGroup::new(vec![Bar::default().value(9)]);

        BarChartWidget::grouped(vec![group()])
            .max_value(9)
            .bar_width(1)
            .bar_gap(0)
            .render(area, &mut with_values);
        BarChartWidget::grouped(vec![group()])
            .max_value(9)
            .bar_width(1)
            .bar_gap(0)
            .show_values(false)
            .render(area, &mut without_values);

        assert_eq!(with_values.cell((0, 1)).expect("value cell").symbol(), "9");
        assert_eq!(without_values.cell((0, 1)).expect("bar cell").symbol(), "");
    }
}