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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
use crate::_private::NonExhaustive;
use crate::selection::{CellSelection, RowSelection, RowSetSelection};
use crate::textdata::{Row, TextTableData};
use crate::{TableData, TableSelection};
use rat_event::util::MouseFlags;
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Flex, Layout, Position, Rect};
use ratatui::prelude::BlockExt;
use ratatui::style::{Style, Styled};
use ratatui::widgets::{Block, StatefulWidget, Widget};
use std::cmp::{max, min};
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::rc::Rc;

/// FTable widget.
///
/// Can be used like a ratatui::Table, but the benefits only
/// show if you use [FTable::data] to set the table data.
///
/// See [FTable::data] for a sample.
#[derive(Debug, Default, Clone)]
pub struct FTable<'a, Selection> {
    data: DataRepr<'a>,

    header: Option<Row<'a>>,
    footer: Option<Row<'a>>,

    widths: Vec<Constraint>,
    flex: Flex,
    column_spacing: u16,
    layout_width: Option<u16>,

    block: Option<Block<'a>>,

    style: Style,

    select_row_style: Style,
    select_column_style: Style,
    select_cell_style: Style,
    select_header_style: Style,
    select_footer_style: Style,
    focus_style: Style,

    focus: bool,

    _phantom: PhantomData<Selection>,
}

#[derive(Clone)]
enum DataRepr<'a> {
    Text(TextTableData<'a>),
    Ref(&'a dyn TableData<'a>),
}

/// Combined style.
#[derive(Debug)]
pub struct FTableStyle {
    pub style: Style,
    pub select_row_style: Style,
    pub select_column_style: Style,
    pub select_cell_style: Style,
    pub select_header_style: Style,
    pub select_footer_style: Style,
    pub focus_style: Style,
    pub non_exhaustive: NonExhaustive,
}

/// FTable state.
#[derive(Debug, Clone)]
pub struct FTableState<Selection> {
    /// Total area.
    pub area: Rect,

    /// Total header area.
    pub header_area: Rect,
    /// Total table area.
    pub table_area: Rect,
    /// Area per visible row.
    pub row_areas: Vec<Rect>,
    /// Area per visible column, also contains the following spacer if any.
    pub column_areas: Vec<Rect>,
    /// Total footer area.
    pub footer_area: Rect,

    /// Row count.
    pub rows: usize,
    /// Column count.
    pub columns: usize,

    /// Current row offset. Automatically capped at rows-1.
    pub row_offset: usize,
    /// Current column offset. Automatically capped at columns-1.
    pub col_offset: usize,

    /// Current page len as items.
    pub row_page_len: usize,
    /// Current page width as columns.
    pub col_page_len: usize,

    /// Maximum offset for row-scrolling. Can be set higher, but this
    /// offset guarantees a full page display.
    pub max_row_offset: usize,
    /// Maximum offset for column-scrolling. Can be set higher, but this
    /// offset guarantees a full page display.
    pub max_col_offset: usize,

    /// Selection data.
    pub selection: Selection,

    /// Helper for mouse interactions.
    pub mouse: MouseFlags,

    pub non_exhaustive: NonExhaustive,
}

impl<'a> Debug for DataRepr<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Data").finish()
    }
}

impl<'a> Default for DataRepr<'a> {
    fn default() -> Self {
        Self::Text(TextTableData::default())
    }
}

impl<'a, Selection> FTable<'a, Selection> {
    /// Create a new FTable with preformatted data. For compatibility
    /// with ratatui.
    ///
    /// Use of [FTable::data] is preferred.
    pub fn new<R, C>(rows: R, widths: C) -> Self
    where
        R: IntoIterator,
        R::Item: Into<Row<'a>>,
        C: IntoIterator,
        C::Item: Into<Constraint>,
        Selection: Default,
    {
        let widths = widths.into_iter().map(|v| v.into()).collect::<Vec<_>>();
        let data = TextTableData {
            columns: widths.len(),
            rows: rows.into_iter().map(|v| v.into()).collect(),
        };
        Self {
            data: DataRepr::Text(data),
            widths,
            ..Default::default()
        }
    }

    /// Set preformatted row-data. For compatibility with ratatui.
    ///
    /// Use of [FTable::data] is preferred.
    pub fn rows<T>(mut self, rows: T) -> Self
    where
        T: IntoIterator<Item = Row<'a>>,
    {
        let rows = rows.into_iter().collect();
        match &mut self.data {
            DataRepr::Text(d) => {
                d.rows = rows;
            }
            DataRepr::Ref(_) => {
                unimplemented!("doesn't work that way");
            }
        }
        self
    }

    /// Set a reference to the TableData facade to your data.
    ///
    /// The way to go is to define a small struct that contains just a
    /// reference to your data. Then implement TableData for this struct.
    ///
    /// ```rust ignore
    /// use ratatui::buffer::Buffer;
    /// use ratatui::layout::Rect;
    /// use ratatui::prelude::Style;
    /// use ratatui::text::Span;
    /// use ratatui::widgets::Widget;
    /// use rat_ftable::{FTable, FTableState, TableData};
    ///
    /// # struct SampleRow;
    /// # let area = Rect::default();
    /// # let mut buf = Buffer::empty(area);
    /// # let buf = &mut buf;
    ///
    /// struct Data1<'a>(&'a [SampleRow]);
    ///
    /// impl<'a> TableData<'a> for Data1<'a> {
    ///     // returns (cols, rows)
    ///     fn size(&self) -> (usize, usize) {
    ///         (5, self.0.len())
    ///     }
    ///
    ///     fn row_height(&self, row: usize) -> u16 {
    ///         // to some calculations ...
    ///         1
    ///     }
    ///
    ///     fn row_style(&self, row: usize) -> Style {
    ///         // to some calculations ...
    ///         Style::default()
    ///     }
    ///
    ///     fn render_cell(&self, column: usize, row: usize, area: Rect, buf: &mut Buffer) {
    ///         if let Some(data) = self.0.get(row) {
    ///             let rend = match column {
    ///                 0 => Span::from("column1"),
    ///                 1 => Span::from("column2"),
    ///                 2 => Span::from("column3"),
    ///                 _ => return
    ///             };
    ///             rend.render(area, buf);
    ///         }
    ///     }
    /// }
    ///
    /// // When you are creating the table widget you hand over a reference
    /// // to the facade struct.
    ///
    /// let my_data_somewhere_else = vec![SampleRow;999999];
    /// let mut table_state_somewhere_else = FTableState::default();
    ///
    /// // ...
    ///
    /// let tabledata1 = Data1(&my_data_somewhere_else);
    /// let table1 = FTable::default().data(&tabledata1);
    /// table1.render(area, buf, &mut table_state_somewhere_else);
    /// ```
    #[inline]
    pub fn data(mut self, data: &'a dyn TableData<'a>) -> Self {
        self.data = DataRepr::Ref(data);
        self
    }

    /// Set the table-header.
    #[inline]
    pub fn header(mut self, header: Row<'a>) -> Self {
        self.header = Some(header);
        self
    }

    /// Set the table-footer.
    #[inline]
    pub fn footer(mut self, footer: Row<'a>) -> Self {
        self.footer = Some(footer);
        self
    }

    /// Column widths as Constraints.
    pub fn widths<I>(mut self, widths: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<Constraint>,
    {
        self.widths = widths.into_iter().map(|v| v.into()).collect();

        match &mut self.data {
            DataRepr::Text(v) => {
                v.columns = self.widths.len();
            }
            DataRepr::Ref(_) => {}
        }

        self
    }

    /// Flex for layout.
    #[inline]
    pub fn flex(mut self, flex: Flex) -> Self {
        self.flex = flex;
        self
    }

    /// Spacing between columns.
    #[inline]
    pub fn column_spacing(mut self, spacing: u16) -> Self {
        self.column_spacing = spacing;
        self
    }

    /// Overrides the width of the rendering area for layout purposes.
    /// Layout uses this width, even if it means that some columns are
    /// not visible.
    #[inline]
    pub fn layout_width(mut self, width: u16) -> Self {
        self.layout_width = Some(width);
        self
    }

    /// Draws a block around the table widget.
    #[inline]
    pub fn block(mut self, block: Block<'a>) -> Self {
        self.block = Some(block);
        self
    }

    /// Set all styles as a bundle.
    #[inline]
    pub fn styles(mut self, styles: FTableStyle) -> Self {
        self.style = styles.style;
        self.select_row_style = styles.select_row_style;
        self.select_column_style = styles.select_column_style;
        self.select_cell_style = styles.select_cell_style;
        self.select_header_style = styles.select_header_style;
        self.select_footer_style = styles.select_footer_style;
        self.focus_style = styles.focus_style;
        self
    }

    /// Base style for the table.
    #[inline]
    pub fn style<S: Into<Style>>(mut self, style: S) -> Self {
        self.style = style.into();
        self
    }

    /// Style for a selected row. The chosen selection must support
    /// row-selection for this to take effect.
    #[inline]
    pub fn select_row_style<S: Into<Style>>(mut self, select_style: S) -> Self {
        self.select_row_style = select_style.into();
        self
    }

    /// Style for a selected column. The chosen selection must support
    /// column-selection for this to take effect.
    #[inline]
    pub fn select_column_style<S: Into<Style>>(mut self, select_style: S) -> Self {
        self.select_column_style = select_style.into();
        self
    }

    /// Style for a selected cell. The chosen selection must support
    /// cell-selection for this to take effect.
    #[inline]
    pub fn select_cell_style<S: Into<Style>>(mut self, select_style: S) -> Self {
        self.select_cell_style = select_style.into();
        self
    }

    /// Style for a selected header cell. The chosen selection must
    /// support column-selection for this to take effect.
    #[inline]
    pub fn select_header_style<S: Into<Style>>(mut self, select_style: S) -> Self {
        self.select_header_style = select_style.into();
        self
    }

    /// Style for a selected footer cell. The chosen selection must
    /// support column-selection for this to take effect.
    #[inline]
    pub fn select_footer_style<S: Into<Style>>(mut self, select_style: S) -> Self {
        self.select_footer_style = select_style.into();
        self
    }

    /// This style will be patched onto the selection to indicate that
    /// the widget has the input focus.
    ///
    /// The selection must support some kind of selection for this to
    /// be effective.
    #[inline]
    pub fn focus_style<S: Into<Style>>(mut self, focus_style: S) -> Self {
        self.focus_style = focus_style.into();
        self
    }

    /// Indicates that this widget has the input focus.
    #[inline]
    pub fn focus(mut self, focus: bool) -> Self {
        self.focus = focus;
        self
    }
}

impl<'a, Selection> FTable<'a, Selection> {
    /// Returns a reference to the `TableData`.
    #[inline]
    #[allow(clippy::borrow_deref_ref)]
    pub fn data_ref(&self) -> &dyn TableData<'a> {
        match &self.data {
            DataRepr::Text(v) => &*v,
            DataRepr::Ref(v) => *v,
        }
    }

    /// Does this table need scrollbars?
    /// Returns (horizontal, vertical)
    pub fn need_scroll(&self, area: Rect) -> (bool, bool) {
        //
        // Attention: This must be kept in sync with the actual rendering.
        //

        let data = self.data_ref();
        let (columns, rows) = data.size();

        // vertical layout
        let inner_area = self.block.inner_if_some(area);
        let l_rows = self.layout_areas(inner_area);
        let table_area = l_rows[1];

        // horizontal layout
        let (l_columns, _) = self.layout_columns(columns, table_area.width);

        // maximum offsets
        let vertical = 'f: {
            let mut page_height = 0;
            for r in 0..rows {
                let row_height = data.row_height(r);
                if page_height + row_height >= table_area.height {
                    break 'f true;
                }
                page_height += row_height;
            }
            false
        };
        let horizontal = 'f: {
            for c in 0..columns {
                if l_columns[c].right() >= table_area.width {
                    break 'f true;
                }
            }
            false
        };

        (horizontal, vertical)
    }

    // area_width or layout_width
    #[inline]
    fn total_width(&self, area_width: u16) -> u16 {
        if let Some(layout_width) = self.layout_width {
            layout_width
        } else {
            area_width
        }
    }

    // Do the column-layout. Fill in missing columns, if necessary.
    #[inline]
    fn layout_columns(&self, columns: usize, width: u16) -> (Rc<[Rect]>, Rc<[Rect]>) {
        let mut widths;
        let widths = if self.widths.is_empty() {
            widths = vec![Constraint::Fill(1); columns];
            widths.as_slice()
        } else if self.widths.len() != columns {
            widths = self.widths.clone();
            while widths.len() < columns {
                widths.push(Constraint::Fill(1));
            }
            widths.as_slice()
        } else {
            self.widths.as_slice()
        };

        let area = Rect::new(0, 0, self.total_width(width), 0);

        Layout::horizontal(widths)
            .flex(self.flex)
            .spacing(self.column_spacing)
            .split_with_spacers(area)
    }

    // Layout header/table/footer
    #[inline]
    fn layout_areas(&self, area: Rect) -> Rc<[Rect]> {
        let heights = vec![
            Constraint::Length(self.header.as_ref().map(|v| v.height).unwrap_or(0)),
            Constraint::Fill(1),
            Constraint::Length(self.footer.as_ref().map(|v| v.height).unwrap_or(0)),
        ];

        Layout::vertical(heights).split(area)
    }
}

impl<'a, Selection> Styled for FTable<'a, Selection> {
    type Item = Self;

    fn style(&self) -> Style {
        self.style
    }

    fn set_style<S: Into<Style>>(mut self, style: S) -> Self::Item {
        self.style = style.into();
        self
    }
}

impl<'a, Selection> StatefulWidget for FTable<'a, Selection>
where
    Selection: TableSelection,
{
    type State = FTableState<Selection>;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        let data = self.data_ref();
        let (columns, rows) = data.size();

        // limits
        if state.row_offset >= rows {
            state.row_offset = rows.saturating_sub(1);
        }
        if state.col_offset >= columns {
            state.col_offset = columns.saturating_sub(1);
        }

        // state
        state.rows = rows;
        state.columns = columns;
        state.area = area;

        // vertical layout
        let inner_area = self.block.inner_if_some(area);
        let l_rows = self.layout_areas(inner_area);
        state.header_area = l_rows[0];
        state.table_area = l_rows[1];
        state.footer_area = l_rows[2];

        // horizontal layout
        let (l_columns, l_spacers) = self.layout_columns(columns, state.table_area.width);

        // maximum offsets
        {
            state.max_row_offset = 0;
            let mut page_height = 0;
            for r in (0..rows).rev() {
                let row_height = data.row_height(r);
                if page_height + row_height >= state.table_area.height {
                    state.max_row_offset = r;
                    break;
                }
                page_height += row_height;
            }
        }
        {
            state.max_col_offset = 0;
            let max_right = l_columns.last().map(|v| v.right()).unwrap_or(0);
            for c in (0..columns).rev() {
                if max_right - l_columns[c].left() >= state.table_area.width {
                    state.max_col_offset = c;
                    break;
                }
            }
        }

        // column areas
        {
            state.column_areas.clear();
            state.col_page_len = 0;

            let mut col = state.col_offset;
            loop {
                if col >= columns {
                    break;
                }

                let column_area = Rect::new(
                    state.table_area.x + l_columns[col].x - l_columns[state.col_offset].x,
                    state.table_area.y,
                    l_columns[col].width + l_spacers[col + 1].width,
                    state.table_area.height,
                )
                .intersection(state.table_area);

                // let space_area = Rect::new(
                //     state.table_area.x + l_spacers[col + 1].x - l_columns[state.col_offset].x,
                //     state.table_area.y,
                //     l_spacers[col + 1].width,
                //     state.table_area.height,
                // )
                // .intersection(state.table_area);

                state.column_areas.push(column_area);
                state.col_page_len += 1;

                if column_area.right() >= state.table_area.right() {
                    break;
                }

                col += 1;
            }
        }

        // render block
        self.block.render(area, buf);

        // render header
        if let Some(header) = &self.header {
            let header_style = if header.style != Style::default() {
                header.style
            } else {
                self.style
            };
            if header_style != Style::default() {
                buf.set_style(state.header_area, header_style);
            }

            let mut col = state.col_offset;
            loop {
                if col >= columns {
                    break;
                }

                let cell_area = Rect::new(
                    state.header_area.x + l_columns[col].x - l_columns[state.col_offset].x,
                    state.header_area.y,
                    l_columns[col].width,
                    state.header_area.height,
                )
                .intersection(state.header_area);

                let space_area = Rect::new(
                    state.header_area.x + l_spacers[col + 1].x - l_columns[state.col_offset].x,
                    state.header_area.y,
                    l_spacers[col + 1].width,
                    state.header_area.height,
                )
                .intersection(state.header_area);

                let mut selected_style = if state.selection.is_selected_column(col) {
                    self.select_header_style
                } else {
                    Style::default()
                };
                if self.focus {
                    selected_style = selected_style.patch(self.focus_style);
                }
                if selected_style != Style::default() {
                    buf.set_style(cell_area, selected_style);
                    buf.set_style(space_area, selected_style);
                }

                if let Some(cell) = header.cells.get(col) {
                    if cell.style != Style::default() {
                        buf.set_style(cell_area, cell.style);
                    }
                    cell.content.clone().render(cell_area, buf);
                }

                if cell_area.right() >= state.header_area.right() {
                    break;
                }

                col += 1;
            }
        }

        // render footer
        if let Some(footer) = &self.footer {
            let footer_style = if footer.style != Style::default() {
                footer.style
            } else {
                self.style
            };
            if footer_style != Style::default() {
                buf.set_style(state.footer_area, footer_style);
            }

            let mut col = state.col_offset;
            loop {
                if col >= columns {
                    break;
                }

                let cell_area = Rect::new(
                    state.footer_area.x + l_columns[col].x - l_columns[state.col_offset].x,
                    state.footer_area.y,
                    l_columns[col].width,
                    state.footer_area.height,
                )
                .intersection(state.footer_area);

                let space_area = Rect::new(
                    state.footer_area.x + l_spacers[col + 1].x - l_columns[state.col_offset].x,
                    state.footer_area.y,
                    l_spacers[col + 1].width,
                    state.footer_area.height,
                )
                .intersection(state.footer_area);

                let mut selected_style = if state.selection.is_selected_column(col) {
                    self.select_footer_style
                } else {
                    Style::default()
                };
                if self.focus {
                    selected_style = selected_style.patch(self.focus_style);
                }
                if selected_style != Style::default() {
                    buf.set_style(cell_area, selected_style);
                    buf.set_style(space_area, selected_style);
                }

                if let Some(cell) = footer.cells.get(col) {
                    if cell.style != Style::default() {
                        buf.set_style(cell_area, cell.style);
                    }
                    cell.content.clone().render(cell_area, buf);
                }

                if cell_area.right() >= state.footer_area.right() {
                    break;
                }

                col += 1;
            }
        }

        // render table
        {
            let table_style = self.style;
            buf.set_style(state.table_area, table_style);

            state.row_areas.clear();
            state.row_page_len = 0;

            let mut row = state.row_offset;
            let mut row_y = state.table_area.y;
            loop {
                if row >= rows {
                    break;
                }

                let row_area = Rect::new(
                    state.table_area.x,
                    row_y,
                    state.table_area.width,
                    data.row_height(row),
                )
                .intersection(state.table_area);

                if data.row_style(row) != Style::default() {
                    buf.set_style(row_area, data.row_style(row));
                }

                state.row_areas.push(row_area);
                state.row_page_len += 1;

                let mut col = state.col_offset;
                loop {
                    if col >= columns {
                        break;
                    }

                    let cell_area = Rect::new(
                        row_area.x + l_columns[col].x - l_columns[state.col_offset].x,
                        row_area.y,
                        l_columns[col].width,
                        row_area.height,
                    )
                    .intersection(state.table_area);

                    let space_area = Rect::new(
                        row_area.x + l_spacers[col + 1].x - l_columns[state.col_offset].x,
                        row_area.y,
                        l_spacers[col + 1].width,
                        row_area.height,
                    )
                    .intersection(state.table_area);

                    let mut select_style = if state.selection.is_selected_cell(col, row) {
                        self.select_cell_style
                    } else if state.selection.is_selected_row(row) {
                        self.select_row_style
                    } else if state.selection.is_selected_column(col) {
                        self.select_column_style
                    } else {
                        Style::default()
                    };
                    if self.focus {
                        select_style = select_style.patch(self.focus_style);
                    }
                    if select_style != Style::default() {
                        buf.set_style(cell_area, select_style);
                        buf.set_style(space_area, select_style);
                    }

                    data.render_cell(col, row, cell_area, buf);

                    if cell_area.right() >= state.table_area.right() {
                        break;
                    }
                    col += 1;
                }

                if row_area.bottom() >= state.table_area.bottom() {
                    break;
                }

                row += 1;
                row_y += row_area.height;
            }
        }
    }
}

impl Default for FTableStyle {
    fn default() -> Self {
        Self {
            style: Default::default(),
            select_row_style: Default::default(),
            select_column_style: Default::default(),
            select_cell_style: Default::default(),
            select_header_style: Default::default(),
            select_footer_style: Default::default(),
            focus_style: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl<Selection: Default> Default for FTableState<Selection> {
    fn default() -> Self {
        Self {
            area: Default::default(),
            header_area: Default::default(),
            table_area: Default::default(),
            footer_area: Default::default(),
            row_areas: Default::default(),
            column_areas: Default::default(),
            rows: 0,
            columns: 0,
            row_offset: 0,
            col_offset: 0,
            row_page_len: 0,
            col_page_len: 0,
            max_row_offset: 0,
            max_col_offset: 0,
            selection: Default::default(),
            mouse: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl<Selection: TableSelection> FTableState<Selection> {
    /// Cell at given position.
    pub fn cell_at_clicked(&self, pos: Position) -> Option<(usize, usize)> {
        let col = self.column_at_clicked(pos);
        let row = self.row_at_clicked(pos);

        match (col, row) {
            (Some(col), Some(row)) => Some((col, row)),
            _ => None,
        }
    }

    /// Column at given position.
    pub fn column_at_clicked(&self, pos: Position) -> Option<usize> {
        rat_event::util::column_at_clicked(&self.column_areas, pos.x).map(|v| self.col_offset + v)
    }

    /// Row at given position.
    pub fn row_at_clicked(&self, pos: Position) -> Option<usize> {
        rat_event::util::row_at_clicked(&self.row_areas, pos.y).map(|v| self.row_offset + v)
    }

    /// Cell when dragging. Can go outside the area.
    pub fn cell_at_drag(&self, pos: Position) -> (usize, usize) {
        let col = self.column_at_drag(pos);
        let row = self.row_at_drag(pos);

        (col, row)
    }

    /// Row when dragging. Can go outside the area.
    pub fn row_at_drag(&self, pos: Position) -> usize {
        match rat_event::util::row_at_drag(self.table_area, &self.row_areas, pos.y) {
            Ok(v) => self.row_offset + v,
            Err(v) if v <= 0 => self.row_offset.saturating_sub((-v) as usize),
            Err(v) => self.row_offset + self.row_areas.len() + v as usize,
        }
    }

    /// Column when dragging. Can go outside the area.
    pub fn column_at_drag(&self, pos: Position) -> usize {
        match rat_event::util::column_at_drag(self.table_area, &self.column_areas, pos.x) {
            Ok(v) => self.col_offset + v,
            Err(v) if v <= 0 => self.col_offset.saturating_sub(1),
            Err(_v) => self.col_offset + self.column_areas.len() + 1,
        }
    }
}

impl<Selection: TableSelection> FTableState<Selection> {
    /// Maximum offset that is accessible with scrolling.
    ///
    /// This is shorter than the length of the content by whatever fills the last page.
    /// This is the base for the scrollbar content_length.
    pub fn vertical_max_offset(&self) -> usize {
        self.max_row_offset
    }

    /// Current vertical offset.
    pub fn vertical_offset(&self) -> usize {
        self.row_offset
    }

    /// Vertical page-size at the current offset.
    pub fn vertical_page(&self) -> usize {
        self.row_page_len
    }

    /// Suggested scroll per scroll-event.
    pub fn vertical_scroll(&self) -> usize {
        max(self.vertical_page() / 10, 1)
    }

    /// Maximum offset that is accessible with scrolling.
    ///
    /// This is shorter than the length of the content by whatever fills the last page.
    /// This is the base for the scrollbar content_length.
    pub fn horizontal_max_offset(&self) -> usize {
        self.max_col_offset
    }

    /// Current horizontal offset.
    pub fn horizontal_offset(&self) -> usize {
        self.col_offset
    }

    /// Horizontal page-size at the current offset.
    pub fn horizontal_page(&self) -> usize {
        self.col_page_len
    }

    /// Suggested scroll per scroll-event.
    pub fn horizontal_scroll(&self) -> usize {
        max(self.horizontal_page() / 10, 1)
    }

    /// Change the vertical offset.
    ///
    /// Due to overscroll it's possible that this is an invalid offset for the widget.
    /// The widget must deal with this situation.
    ///
    /// The widget returns true if the offset changed at all.
    pub fn set_vertical_offset(&mut self, offset: usize) -> bool {
        let old_offset = self.row_offset;
        if offset >= self.rows {
            self.row_offset = self.rows;
        } else {
            self.row_offset = offset;
        }
        old_offset != self.row_offset
    }

    /// Change the horizontal offset.
    ///
    /// Due to overscroll it's possible that this is an invalid offset for the widget.
    /// The widget must deal with this situation.
    ///
    /// The widget returns true if the offset changed at all.
    pub fn set_horizontal_offset(&mut self, offset: usize) -> bool {
        let old_offset = self.col_offset;
        if offset >= self.columns {
            self.col_offset = self.columns;
        } else {
            self.col_offset = offset;
        }
        old_offset != self.col_offset
    }

    /// Scroll up by n items.
    /// The widget returns true if the offset changed at all.
    pub fn scroll_up(&mut self, n: usize) -> bool {
        self.set_vertical_offset(self.vertical_offset().saturating_sub(n))
    }

    /// Scroll down by n items.
    /// The widget returns true if the offset changed at all.
    pub fn scroll_down(&mut self, n: usize) -> bool {
        self.set_vertical_offset(min(self.vertical_offset() + n, self.vertical_max_offset()))
    }

    /// Scroll up by n items.
    /// The widget returns true if the offset changed at all.
    pub fn scroll_left(&mut self, n: usize) -> bool {
        self.set_horizontal_offset(self.horizontal_offset().saturating_sub(n))
    }

    /// Scroll down by n items.
    /// The widget returns true if the offset changed at all.
    pub fn scroll_right(&mut self, n: usize) -> bool {
        self.set_horizontal_offset(min(
            self.horizontal_offset() + n,
            self.horizontal_max_offset(),
        ))
    }

    /// Scroll to selected.
    pub fn scroll_to_selected(&mut self) {
        if let Some((selected_col, selected_row)) = self.selection.lead_selection() {
            if self.row_offset + self.row_page_len <= selected_row {
                self.set_vertical_offset(selected_row - self.row_page_len + 1);
            }
            if self.row_offset > selected_row {
                self.set_vertical_offset(selected_row);
            }

            if self.col_offset + self.col_page_len <= selected_col {
                self.set_horizontal_offset(selected_col - self.col_page_len + 1);
            }
            if self.col_offset > selected_col {
                self.set_horizontal_offset(selected_col);
            }
        }
    }
}

impl FTableState<RowSelection> {
    #[inline]
    pub fn selected(&self) -> Option<usize> {
        self.selection.selected()
    }

    #[inline]
    pub fn select(&mut self, row: Option<usize>) {
        self.selection.select(row);
    }

    /// Select a row, clamp between 0 and maximum.
    #[inline]
    pub fn select_clamped(&mut self, select: usize, maximum: usize) {
        self.selection.select_clamped(select, maximum);
    }
}

impl FTableState<RowSetSelection> {
    #[inline]
    pub fn selected(&self) -> HashSet<usize> {
        self.selection.selected()
    }

    #[inline]
    pub fn set_lead(&mut self, lead: Option<usize>, extend: bool) {
        self.selection.set_lead(lead, extend);
    }

    /// Set a new lead, at the same time limit the lead to max.
    #[inline]
    pub fn set_lead_clamped(&mut self, lead: usize, max: usize, extend: bool) {
        self.selection.set_lead_clamped(lead, max, extend);
    }

    /// Current lead.
    #[inline]
    pub fn lead(&self) -> Option<usize> {
        self.selection.lead()
    }

    /// Current anchor.
    #[inline]
    pub fn anchor(&self) -> Option<usize> {
        self.selection.anchor()
    }

    /// Clear the selection.
    #[inline]
    pub fn clear_selection(&mut self) {
        self.selection.clear();
    }

    /// Add to selection.
    #[inline]
    pub fn add_selected(&mut self, idx: usize) {
        self.selection.add(idx);
    }

    /// Remove from selection. Only works for retired selections, not for the
    /// active anchor-lead range.
    #[inline]
    pub fn remove_selected(&mut self, idx: usize) {
        self.selection.remove(idx);
    }
}

impl FTableState<CellSelection> {
    /// Selected cell.
    #[inline]
    pub fn selected(&self) -> Option<(usize, usize)> {
        self.selection.selected()
    }

    /// Select a cell.
    #[inline]
    pub fn select_cell(&mut self, select: Option<(usize, usize)>) -> bool {
        self.selection.select_cell(select)
    }

    /// Select a row. Column stays the same.
    #[inline]
    pub fn select_row(&mut self, select: Option<usize>) -> bool {
        self.selection.select_row(select)
    }

    /// Select a column, row stays the same.
    #[inline]
    pub fn select_column(&mut self, select: Option<usize>) -> bool {
        self.selection.select_column(select)
    }

    /// Select a cell, clamp between 0 and maximum.
    #[inline]
    pub fn select_clamped(&mut self, select: (usize, usize), maximum: (usize, usize)) -> bool {
        self.selection.select_clamped(select, maximum)
    }
}