tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets 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
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
//! Table widget.

use std::sync::Arc;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::{Element, ElementKind};
use crate::core::event::{KeyEvent, MouseEvent};
use crate::style::{BorderStyle, Length, Padding, ScrollbarConfig, Style, StyleSlot};
use crate::utils::gradient::{ColorGradient, GradientRange};
use crate::widgets::scroll::{ScrollKeymap, scroll_action_from_key};

/// A table selection event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TableEvent {
    /// Selected row index.
    pub index: usize,
}

/// Semantic role of a table row.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TableRowRole {
    /// Regular data row.
    #[default]
    Normal,
    /// Section header row, usually spanning key/value groups.
    Section,
    /// Visual separator row.
    Separator,
}

/// Disclosure marker state for inspector-like rows.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TableDisclosureState {
    /// Collapsed branch marker.
    Collapsed,
    /// Expanded branch marker.
    Expanded,
}

/// A cell in a table.
#[derive(Clone, Debug, Default)]
pub struct TableCell {
    pub(crate) content: Arc<str>,
    pub(crate) style: Style,
}

impl TableCell {
    /// Create a new table cell.
    pub fn new(content: impl Into<Arc<str>>) -> Self {
        Self {
            content: content.into(),
            style: Style::default(),
        }
    }

    /// Set cell style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Map a numeric value to foreground color using a gradient.
    pub fn heat_fg(
        mut self,
        value: u64,
        gradient: ColorGradient,
        range: impl Into<GradientRange>,
    ) -> Self {
        let color = gradient.color_for(value, range);
        self.style = self.style.patch(Style::new().fg(color));
        self
    }

    /// Map a numeric value to background color using a gradient.
    pub fn heat_bg(
        mut self,
        value: u64,
        gradient: ColorGradient,
        range: impl Into<GradientRange>,
    ) -> Self {
        let color = gradient.color_for(value, range);
        self.style = self.style.patch(Style::new().bg(color));
        self
    }
}

impl<T: Into<Arc<str>>> From<T> for TableCell {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

/// A row in a table.
#[derive(Clone, Debug, Default)]
pub struct TableRow {
    pub(crate) cells: Vec<TableCell>,
    pub(crate) style: Style,
    pub(crate) height: u16,
    pub(crate) bottom_margin: u16,
    pub(crate) role: TableRowRole,
    pub(crate) depth: u16,
    pub(crate) disclosure: Option<TableDisclosureState>,
}

impl TableRow {
    /// Create a new table row.
    pub fn new(cells: impl IntoIterator<Item = impl Into<TableCell>>) -> Self {
        Self {
            cells: cells.into_iter().map(Into::into).collect(),
            style: Style::default(),
            height: 1,
            bottom_margin: 0,
            role: TableRowRole::Normal,
            depth: 0,
            disclosure: None,
        }
    }

    /// Create a key/value row optimized for inspector-style tables.
    pub fn key_value(key: impl Into<TableCell>, value: impl Into<TableCell>) -> Self {
        Self::new([key.into(), value.into()])
    }

    /// Create a section row.
    pub fn section(title: impl Into<TableCell>) -> Self {
        Self::new([title.into()]).role(TableRowRole::Section)
    }

    /// Create a separator row.
    pub fn separator() -> Self {
        Self::new(std::iter::empty::<TableCell>()).role(TableRowRole::Separator)
    }

    /// Set row style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set row height.
    pub fn height(mut self, height: u16) -> Self {
        self.height = height;
        self
    }

    /// Automatically size the row height based on content line count.
    pub fn auto_height(mut self) -> Self {
        self.height = 0;
        self
    }

    /// Set bottom margin.
    pub fn bottom_margin(mut self, margin: u16) -> Self {
        self.bottom_margin = margin;
        self
    }

    /// Set semantic row role.
    pub fn role(mut self, role: TableRowRole) -> Self {
        self.role = role;
        self
    }

    /// Set indentation depth for inspector rendering.
    pub fn depth(mut self, depth: u16) -> Self {
        self.depth = depth;
        self
    }

    /// Set disclosure marker state for inspector rendering.
    pub fn disclosure(mut self, disclosure: TableDisclosureState) -> Self {
        self.disclosure = Some(disclosure);
        self
    }
}

pub(crate) fn resolved_row_height(row: &TableRow) -> u16 {
    if row.height > 0 {
        return row.height;
    }
    let mut max_lines = 1u16;
    for cell in &row.cells {
        let lines = cell.content.as_ref().lines().count().max(1) as u16;
        max_lines = max_lines.max(lines);
    }
    max_lines
}

pub(crate) fn resolved_row_total_height(row: &TableRow) -> u16 {
    resolved_row_height(row).saturating_add(row.bottom_margin)
}

pub(crate) fn table_header_reserved_height(
    header: Option<&TableRow>,
    rows_len: usize,
    row_gap: u16,
) -> u16 {
    header
        .map(resolved_row_total_height)
        .unwrap_or(0)
        .saturating_add(if header.is_some() && rows_len > 0 {
            row_gap
        } else {
            0
        })
}

pub(crate) fn row_index_at_visual_offset(
    rows: &[TableRow],
    offset: usize,
    visual_y: u16,
    row_gap: u16,
) -> Option<usize> {
    if rows.is_empty() || offset >= rows.len() {
        return None;
    }

    let mut remaining = visual_y;
    for (index, row) in rows.iter().enumerate().skip(offset) {
        let row_h = resolved_row_total_height(row).max(1);
        if remaining < row_h {
            return Some(index);
        }
        remaining = remaining.saturating_sub(row_h);

        if index + 1 < rows.len() {
            if remaining < row_gap {
                return None;
            }
            remaining = remaining.saturating_sub(row_gap);
        }
    }

    None
}

pub(crate) fn visible_rows_for_height(
    rows: &[TableRow],
    offset: usize,
    available_height: u16,
    row_gap: u16,
) -> usize {
    if available_height == 0 || rows.is_empty() || offset >= rows.len() {
        return 0;
    }

    let mut used = 0u16;
    let mut count = 0usize;
    for (idx, row) in rows.iter().enumerate().skip(offset) {
        let row_h = resolved_row_total_height(row).max(1);
        let gap_before = if count > 0 && idx > offset {
            row_gap
        } else {
            0
        };
        let needed = gap_before.saturating_add(row_h);
        if used.saturating_add(needed) > available_height {
            break;
        }
        used = used.saturating_add(needed);
        count = count.saturating_add(1);
    }

    if count == 0 { 1 } else { count }
}

impl<I, C> From<I> for TableRow
where
    I: IntoIterator<Item = C>,
    C: Into<TableCell>,
{
    fn from(iter: I) -> Self {
        Self::new(iter)
    }
}

/// Column width constraint.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ColumnWidth {
    /// Fixed width in cells.
    Fixed(u16),
    /// Percentage of total width.
    Percent(u16),
    /// Minimum width (Auto).
    Min(u16),
    /// Maximum width.
    Max(u16),
    /// Proportional fill.
    Fill(u16),
}

/// A table widget.
#[derive(Clone)]
pub struct Table {
    pub(crate) rows: Arc<[TableRow]>,
    pub(crate) header: Option<TableRow>,
    pub(crate) widths: Vec<ColumnWidth>,
    pub(crate) column_styles: Vec<Style>,
    pub(crate) row_styles: Vec<Style>,
    pub(crate) selected: usize,
    pub(crate) column_spacing: u16,
    pub(crate) row_gap: u16,
    pub(crate) style: Style,
    pub(crate) hover_style: StyleSlot,
    pub(crate) item_hover_style: StyleSlot,
    pub(crate) alternating_row_style: Option<Style>,
    pub(crate) row_style_full_width: bool,
    pub(crate) selection_style: StyleSlot,
    pub(crate) selection_symbol: Option<Arc<str>>,
    pub(crate) selection_symbol_style: Option<Style>,
    pub(crate) unselected_symbol: Option<Arc<str>>,
    pub(crate) border: bool,
    pub(crate) border_style: BorderStyle,
    pub(crate) padding: Padding,

    // Scrolling support
    pub(crate) scrollbar: bool,
    pub(crate) scrollbar_config: ScrollbarConfig,
    pub(crate) scroll_keys: ScrollKeymap,
    pub(crate) scroll_wheel: bool,

    // Layout
    pub(crate) width: Length,
    pub(crate) height: Length,

    // Events
    pub(crate) on_select: Option<Callback<TableEvent>>,
    pub(crate) on_activate: Option<Callback<TableEvent>>,
    pub(crate) on_click: Option<Callback<MouseEvent>>,
    pub(crate) on_scroll_to: Option<Callback<usize>>,
    pub(crate) on_key: Option<KeyHandler>,

    pub(crate) disabled: bool,
    pub(crate) disabled_style: Style,
    pub(crate) focusable: bool,
    pub(crate) show_scroll_indicators: bool,
    pub(crate) scroll_indicator_style: Style,

    // Inspector-style configuration.
    pub(crate) inspector: bool,
    pub(crate) inspector_key_style: Style,
    pub(crate) inspector_value_style: Style,
    pub(crate) inspector_section_style: Style,
    pub(crate) inspector_separator_style: Style,
    pub(crate) inspector_indent_size: u16,
    pub(crate) inspector_collapsed_symbol: Arc<str>,
    pub(crate) inspector_expanded_symbol: Arc<str>,
    pub(crate) inspector_separator_char: char,
}

impl Default for Table {
    fn default() -> Self {
        Self {
            rows: Arc::new([]),
            header: None,
            widths: Vec::new(),
            column_styles: Vec::new(),
            row_styles: Vec::new(),
            selected: 0,
            column_spacing: 1,
            row_gap: 0,
            style: Style::default(),
            hover_style: StyleSlot::Inherit,
            item_hover_style: StyleSlot::Inherit,
            alternating_row_style: None,
            row_style_full_width: false,
            selection_style: StyleSlot::Inherit,
            selection_symbol: None,
            selection_symbol_style: None,
            unselected_symbol: None,
            border: false,
            border_style: BorderStyle::Plain,
            padding: Padding::default(),
            scrollbar: false,
            scrollbar_config: ScrollbarConfig::default(),
            scroll_keys: ScrollKeymap::default(),
            scroll_wheel: true,
            width: Length::Flex(1),
            height: Length::Flex(1),
            on_select: None,
            on_activate: None,
            on_click: None,
            on_scroll_to: None,
            on_key: None,
            disabled: false,
            disabled_style: Style::default(),
            focusable: true,
            show_scroll_indicators: false,
            scroll_indicator_style: Style::default(),
            inspector: false,
            inspector_key_style: Style::default(),
            inspector_value_style: Style::default(),
            inspector_section_style: Style::default(),
            inspector_separator_style: Style::default(),
            inspector_indent_size: 2,
            inspector_collapsed_symbol: Arc::from("â–¸"),
            inspector_expanded_symbol: Arc::from("â–¾"),
            inspector_separator_char: '─',
        }
    }
}

impl Table {
    /// Create a new table.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set table rows.
    pub fn rows(mut self, rows: impl IntoIterator<Item = impl Into<TableRow>>) -> Self {
        self.rows = rows.into_iter().map(Into::into).collect::<Vec<_>>().into();
        self
    }

    /// Add a row.
    pub fn row(mut self, row: impl Into<TableRow>) -> Self {
        let mut rows = self.rows.to_vec();
        rows.push(row.into());
        self.rows = rows.into();
        self
    }

    /// Set header row.
    pub fn header(mut self, header: impl Into<TableRow>) -> Self {
        self.header = Some(header.into());
        self
    }

    /// Set header row style.
    pub fn header_style(mut self, style: Style) -> Self {
        if let Some(header) = &mut self.header {
            header.style = header.style.patch(style);
        }
        self
    }

    /// Set base style for all rows.
    pub fn row_style(mut self, style: Style) -> Self {
        let mut rows = self.rows.to_vec();
        for row in &mut rows {
            row.style = row.style.patch(style);
        }
        self.rows = rows.into();
        self
    }

    /// Patch the style for one zero-based column.
    ///
    /// Missing entries are filled with `Style::default()`. The supplied style is patched over any
    /// existing style at `index` and applies to header and data cells in that column.
    pub fn column_style(mut self, index: usize, style: Style) -> Self {
        if self.column_styles.len() <= index {
            self.column_styles
                .resize(index.saturating_add(1), Style::default());
        }
        self.column_styles[index] = self.column_styles[index].patch(style);
        self
    }

    /// Replace the positional column style list.
    ///
    /// Styles are matched by zero-based column index and apply to header and data cells.
    pub fn column_styles(mut self, styles: impl IntoIterator<Item = Style>) -> Self {
        self.column_styles = styles.into_iter().collect();
        self
    }

    /// Patch the style for one zero-based data row by absolute row index.
    ///
    /// Missing entries are filled with `Style::default()`. The supplied style is patched over any
    /// existing style at `index`; header rows are not affected.
    pub fn row_style_at(mut self, index: usize, style: Style) -> Self {
        if self.row_styles.len() <= index {
            self.row_styles
                .resize(index.saturating_add(1), Style::default());
        }
        self.row_styles[index] = self.row_styles[index].patch(style);
        self
    }

    /// Replace the positional data-row style list.
    ///
    /// Styles are matched by zero-based absolute row index and do not affect the header row.
    pub fn row_styles(mut self, styles: impl IntoIterator<Item = Style>) -> Self {
        self.row_styles = styles.into_iter().collect();
        self
    }

    /// Set column widths.
    pub fn widths(mut self, widths: impl IntoIterator<Item = ColumnWidth>) -> Self {
        self.widths = widths.into_iter().collect();
        self
    }

    /// Set selected row index.
    pub fn selected(mut self, selected: usize) -> Self {
        self.selected = selected;
        self
    }

    /// Set column spacing.
    pub fn column_spacing(mut self, spacing: u16) -> Self {
        self.column_spacing = spacing;
        self
    }

    /// Set blank terminal rows inserted between rendered table rows.
    ///
    /// The gap is additive with `TableRow::bottom_margin`, applies between the
    /// header and first data row when both are present, and is not added after
    /// the final data row or after a header-only table.
    pub fn row_gap(mut self, gap: u16) -> Self {
        self.row_gap = gap;
        self
    }

    /// Set base style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set style when table is hovered.
    pub fn hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's hover style with additional fields.
    pub fn extend_hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit hover style from the active theme.
    pub fn inherit_hover_style(mut self) -> Self {
        self.hover_style = StyleSlot::Inherit;
        self
    }

    /// Set style for hovered rows.
    pub fn item_hover_style(mut self, style: Style) -> Self {
        self.item_hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's item hover style with additional fields.
    pub fn extend_item_hover_style(mut self, style: Style) -> Self {
        self.item_hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit item hover style from the active theme.
    pub fn inherit_item_hover_style(mut self) -> Self {
        self.item_hover_style = StyleSlot::Inherit;
        self
    }

    /// Set alternating style for odd data rows.
    pub fn alternating_row_style(mut self, style: Style) -> Self {
        self.alternating_row_style = Some(style);
        self
    }

    /// Set whether row-level styles span the full content width.
    ///
    /// When enabled, alternating row style, hover style, and selected-row style
    /// are rendered across the entire row width, not just table cell content.
    pub fn row_style_full_width(mut self, full_width: bool) -> Self {
        self.row_style_full_width = full_width;
        self
    }

    /// Set highlight style.
    pub fn selection_style(mut self, style: Style) -> Self {
        self.selection_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's selection style with additional fields.
    pub fn extend_selection_style(mut self, style: Style) -> Self {
        self.selection_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit selection style from the active theme.
    pub fn inherit_selection_style(mut self) -> Self {
        self.selection_style = StyleSlot::Inherit;
        self
    }

    /// Set highlight symbol.
    pub fn selection_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.selection_symbol = symbol.map(Into::into);
        self
    }

    /// Set style for the highlight symbol.
    pub fn selection_symbol_style(mut self, style: Style) -> Self {
        self.selection_symbol_style = Some(style);
        self
    }

    /// Set symbol for unselected rows.
    pub fn unselected_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
        self.unselected_symbol = symbol.map(Into::into);
        self
    }

    /// Enable border.
    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }

    /// Set border style.
    pub fn border_style(mut self, style: BorderStyle) -> Self {
        self.border_style = style;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
        self.padding = padding.into();
        self
    }

    /// Enable scrollbar.
    pub fn scrollbar(mut self, scrollbar: bool) -> Self {
        self.scrollbar = scrollbar;
        self
    }

    /// Set scrollbar configuration.
    pub fn scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.scrollbar_config = config;
        self
    }

    /// Set scroll keys.
    pub fn scroll_keys(mut self, keys: ScrollKeymap) -> Self {
        self.scroll_keys = keys;
        self
    }

    /// Set width.
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Set height.
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    /// Set on-select callback.
    pub fn on_select(mut self, cb: Callback<TableEvent>) -> Self {
        self.on_select = Some(cb);
        self
    }

    /// Set on-activate callback.
    pub fn on_activate(mut self, cb: Callback<TableEvent>) -> Self {
        self.on_activate = Some(cb);
        self
    }

    /// Set on-click callback.
    pub fn on_click(mut self, cb: Callback<MouseEvent>) -> Self {
        self.on_click = Some(cb);
        self
    }

    /// Set on-scroll-to callback.
    pub fn on_scroll_to(mut self, cb: Callback<usize>) -> Self {
        self.on_scroll_to = Some(cb);
        self
    }

    /// Set on-key handler.
    pub fn on_key(mut self, handler: KeyHandler) -> Self {
        self.on_key = Some(handler);
        self
    }

    /// Set disabled.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set focusable.
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    /// Enable scroll indicators when rows are hidden.
    pub fn show_scroll_indicators(mut self, show: bool) -> Self {
        self.show_scroll_indicators = show;
        self
    }

    /// Set style for scroll indicators.
    pub fn scroll_indicator_style(mut self, style: Style) -> Self {
        self.scroll_indicator_style = style;
        self
    }

    /// Enable inspector-style row rendering conventions.
    pub fn inspector(mut self, enabled: bool) -> Self {
        self.inspector = enabled;
        self
    }

    /// Apply inspector defaults and key/value-friendly column widths.
    pub fn inspector_preset(mut self) -> Self {
        self.inspector = true;
        if self.widths.is_empty() {
            self.widths = vec![ColumnWidth::Min(24), ColumnWidth::Fill(1)];
        }
        self
    }

    /// Style for the key column when inspector mode is enabled.
    pub fn inspector_key_style(mut self, style: Style) -> Self {
        self.inspector_key_style = style;
        self
    }

    /// Style for value columns when inspector mode is enabled.
    pub fn inspector_value_style(mut self, style: Style) -> Self {
        self.inspector_value_style = style;
        self
    }

    /// Style for section rows when inspector mode is enabled.
    pub fn inspector_section_style(mut self, style: Style) -> Self {
        self.inspector_section_style = style;
        self
    }

    /// Style for separator rows when inspector mode is enabled.
    pub fn inspector_separator_style(mut self, style: Style) -> Self {
        self.inspector_separator_style = style;
        self
    }

    /// Set indentation width (in cells) for inspector mode.
    pub fn inspector_indent_size(mut self, size: u16) -> Self {
        self.inspector_indent_size = size.max(1);
        self
    }

    /// Set disclosure symbols used by inspector rows with disclosure metadata.
    pub fn inspector_disclosure_symbols(
        mut self,
        collapsed: impl Into<Arc<str>>,
        expanded: impl Into<Arc<str>>,
    ) -> Self {
        self.inspector_collapsed_symbol = collapsed.into();
        self.inspector_expanded_symbol = expanded.into();
        self
    }

    /// Set separator character used by inspector separator rows.
    pub fn inspector_separator_char(mut self, separator_char: char) -> Self {
        self.inspector_separator_char = separator_char;
        self
    }

    pub(crate) fn next_selection(
        selected: usize,
        len: usize,
        key: &KeyEvent,
        scroll_keys: ScrollKeymap,
    ) -> Option<usize> {
        let action = scroll_action_from_key(key, scroll_keys)?;
        crate::widgets::list::List::selection_for_action_in_len(selected, len, action)
    }
}

impl From<Table> for Element {
    fn from(value: Table) -> Self {
        Element::new(ElementKind::Table(Box::new(value)))
    }
}

impl crate::layout::hash::LayoutHash for Table {
    fn layout_hash(
        &self,
        hasher: &mut impl std::hash::Hasher,
        _recurse: &dyn Fn(&Element) -> Option<u64>,
    ) -> Option<()> {
        use std::hash::Hash;
        self.width.hash(hasher);
        self.height.hash(hasher);
        self.border.hash(hasher);
        self.border_style.hash(hasher);
        self.padding.hash(hasher);
        self.row_gap.hash(hasher);

        let needs_content = matches!(self.height, Length::Auto);
        if needs_content {
            self.rows.len().hash(hasher);
            if let Some(header) = &self.header {
                header.height.hash(hasher);
                header.bottom_margin.hash(hasher);
            }
            for row in self.rows.iter() {
                row.height.hash(hasher);
                row.bottom_margin.hash(hasher);
            }
        }

        self.header.is_some().hash(hasher);
        self.column_spacing.hash(hasher);
        self.scrollbar.hash(hasher);
        self.scrollbar_config.gap.hash(hasher);
        self.show_scroll_indicators.hash(hasher);
        self.widths.hash(hasher);
        self.selected.hash(hasher);
        Some(())
    }
}

mod layout;
mod node;
mod reconcile;
mod shared;

pub(crate) use layout::measure_table;
pub(crate) use node::TableNode;
pub(crate) use reconcile::reconcile_table;
pub(crate) use shared::{
    TableBorderLineKind, distribute_extra_width, shrink_widths_to_fit, table_border_glyphs,
    table_border_line, table_fixed_chars, table_render_width,
};