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
//!
//! Implements a Row and a Cell struct that are compatible to ratatui.
//! You only need these if you use preformatted data.
//!

use crate::_private::NonExhaustive;
use crate::{TableContext, TableData};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::{Style, Text};
use ratatui::style::Styled;
use ratatui::widgets::Widget;

/// Internal impl for TableData using pre-rendered Cells.
#[derive(Debug, Default, Clone)]
pub(crate) struct TextTableData<'a> {
    pub(crate) rows: Vec<Row<'a>>,
}

/// Rows of the table.
#[derive(Debug, Clone)]
pub struct Row<'a> {
    pub cells: Vec<Cell<'a>>,
    pub top_margin: u16,
    pub height: u16,
    pub bottom_margin: u16,
    pub style: Option<Style>,

    pub non_exhaustive: NonExhaustive,
}

/// A single cell of the table.
#[derive(Debug, Clone)]
pub struct Cell<'a> {
    pub content: Text<'a>,
    pub style: Option<Style>,

    pub non_exhaustive: NonExhaustive,
}

impl<'a> TableData<'a> for TextTableData<'a> {
    fn rows(&self) -> usize {
        self.rows.len()
    }

    fn row_height(&self, r: usize) -> u16 {
        if let Some(row) = self.rows.get(r) {
            row.top_margin + row.height + row.bottom_margin
        } else {
            0
        }
    }

    fn row_style(&self, r: usize) -> Option<Style> {
        if let Some(row) = self.rows.get(r) {
            row.style
        } else {
            None
        }
    }

    fn render_cell(&self, _ctx: &TableContext, c: usize, r: usize, area: Rect, buf: &mut Buffer) {
        if let Some(row) = self.rows.get(r) {
            if let Some(cell) = row.cell(c) {
                if let Some(style) = cell.style {
                    buf.set_style(area, style);
                }
                cell.content.clone().render(area, buf);
            }
        }
    }
}

impl<'a> Default for Row<'a> {
    fn default() -> Self {
        Self {
            cells: Default::default(),
            top_margin: 0,
            height: 0,
            bottom_margin: 0,
            style: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl<'a> Styled for Row<'a> {
    type Item = Self;

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

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

impl<'a, Item> FromIterator<Item> for Row<'a>
where
    Item: Into<Cell<'a>>,
{
    fn from_iter<T: IntoIterator<Item = Item>>(cells: T) -> Self {
        Self::new(cells)
    }
}

impl<'a> Row<'a> {
    /// New row of data cells.
    pub fn new<T>(cells: T) -> Self
    where
        T: IntoIterator,
        T::Item: Into<Cell<'a>>,
    {
        let mut s = Self {
            cells: cells.into_iter().map(|v| v.into()).collect(),
            height: 1,
            ..Default::default()
        };
        // content heigth
        if let Some(height) = s.cells.iter().map(|v| v.content.height()).max() {
            s.height = height as u16;
        }
        s
    }

    /// Set the data cells for the row.
    pub fn cells<T>(mut self, cells: T) -> Self
    where
        T: IntoIterator,
        T::Item: Into<Cell<'a>>,
    {
        self.cells = cells.into_iter().map(Into::into).collect();
        self
    }

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

    /// Add some margin.
    pub fn top_margin(mut self, margin: u16) -> Self {
        self.top_margin = margin;
        self
    }

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

    /// Rowstyle.
    pub fn style(mut self, style: Option<Style>) -> Self {
        self.style = style;
        self
    }

    /// Access to the cell.
    pub fn cell<'b: 'a>(&'b self, c: usize) -> Option<&'a Cell<'a>> {
        if let Some(t) = self.cells.get(c) {
            Some(t)
        } else {
            None
        }
    }
}

impl<'a> Default for Cell<'a> {
    fn default() -> Self {
        Self {
            content: Default::default(),
            style: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl<'a, T> From<T> for Cell<'a>
where
    T: Into<Text<'a>>,
{
    fn from(value: T) -> Self {
        Self {
            content: value.into(),
            style: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl<'a> Styled for Cell<'a> {
    type Item = Self;

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

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

impl<'a> Cell<'a> {
    /// New Cell.
    pub fn new<T>(content: T) -> Self
    where
        T: Into<Text<'a>>,
    {
        Self {
            content: content.into(),
            style: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }

    /// Set the cell content.
    pub fn content<T>(mut self, content: T) -> Self
    where
        T: Into<Text<'a>>,
    {
        self.content = content.into();
        self
    }

    /// Cell style.
    pub fn style(mut self, style: Option<Style>) -> Self {
        self.style = style;
        self
    }
}