1#![doc = include_str!("../readme.md")]
2
3mod cellselection;
4pub mod edit;
5mod noselection;
6mod rowselection;
7mod rowsetselection;
8mod table;
9pub mod textdata;
10mod util;
11
12use crate::textdata::Row;
13use ratatui_core::buffer::Buffer;
14use ratatui_core::layout::{Constraint, Rect};
15use ratatui_core::style::Style;
16
17#[derive(Debug)]
19pub struct TableContext {
20 pub focus: bool,
22
23 pub selected_cell: bool,
25 pub selected_row: bool,
27 pub selected_column: bool,
29
30 pub style: Style,
32 pub row_style: Option<Style>,
34 pub select_style: Option<Style>,
36
37 pub space_area: Rect,
40 pub row_area: Rect,
42
43 pub non_exhaustive: NonExhaustive,
45}
46
47pub trait TableData<'a> {
53 fn rows(&self) -> usize;
55
56 fn header(&self) -> Option<Row<'a>> {
59 None
60 }
61
62 fn footer(&self) -> Option<Row<'a>> {
65 None
66 }
67
68 #[allow(unused_variables)]
70 fn row_height(&self, row: usize) -> u16 {
71 1
72 }
73
74 #[allow(unused_variables)]
76 fn row_style(&self, row: usize) -> Option<Style> {
77 None
78 }
79
80 fn widths(&self) -> Vec<Constraint> {
82 Vec::default()
83 }
84
85 fn render_cell(
88 &self,
89 ctx: &TableContext,
90 column: usize,
91 row: usize,
92 area: Rect,
93 buf: &mut Buffer,
94 );
95}
96
97impl<'a> TableData<'a> for Box<dyn TableData<'a> + 'a> {
98 fn rows(&self) -> usize {
99 (**self).rows()
100 }
101
102 fn header(&self) -> Option<Row<'a>> {
103 (**self).header()
104 }
105
106 fn footer(&self) -> Option<Row<'a>> {
107 (**self).footer()
108 }
109
110 fn row_height(&self, row: usize) -> u16 {
111 (**self).row_height(row)
112 }
113
114 fn row_style(&self, row: usize) -> Option<Style> {
115 (**self).row_style(row)
116 }
117
118 fn widths(&self) -> Vec<Constraint> {
119 (**self).widths()
120 }
121
122 fn render_cell(
123 &self,
124 ctx: &TableContext,
125 column: usize,
126 row: usize,
127 area: Rect,
128 buf: &mut Buffer,
129 ) {
130 (**self).render_cell(ctx, column, row, area, buf)
131 }
132}
133
134pub trait TableDataIter<'a> {
138 fn cloned(&self) -> Option<Box<dyn TableDataIter<'a> + 'a>> {
142 None
143 }
144
145 fn rows(&self) -> Option<usize>;
153
154 fn header(&self) -> Option<Row<'a>> {
157 None
158 }
159
160 fn footer(&self) -> Option<Row<'a>> {
163 None
164 }
165
166 fn nth(&mut self, n: usize) -> bool;
169
170 fn row_height(&self) -> u16 {
172 1
173 }
174
175 fn row_style(&self) -> Option<Style> {
177 None
178 }
179
180 fn widths(&self) -> Vec<Constraint> {
182 Vec::default()
183 }
184
185 fn render_cell(&self, ctx: &TableContext, column: usize, area: Rect, buf: &mut Buffer);
188}
189
190pub trait TableSelection {
192 fn count(&self) -> usize;
194
195 fn is_selected_row(&self, row: usize) -> bool;
197
198 fn is_selected_column(&self, column: usize) -> bool;
200
201 fn is_selected_cell(&self, column: usize, row: usize) -> bool;
203
204 fn lead_selection(&self) -> Option<(usize, usize)>;
206
207 #[allow(unused_variables)]
209 fn validate_rows(&mut self, rows: usize) {}
210
211 #[allow(unused_variables)]
213 fn validate_cols(&mut self, cols: usize) {}
214
215 #[allow(unused_variables)]
217 fn items_added(&mut self, pos: usize, n: usize) {}
218
219 #[allow(unused_variables)]
221 fn items_removed(&mut self, pos: usize, n: usize, rows: usize) {}
222}
223
224use crate::_private::NonExhaustive;
225
226pub use table::{Table, TableState, TableStyle, handle_doubleclick_events};
227
228pub mod selection {
230 pub use crate::cellselection::CellSelection;
231 pub mod cellselection {
232 pub use crate::cellselection::{handle_events, handle_mouse_events};
233 }
234 pub use crate::noselection::NoSelection;
235 pub mod noselection {
236 pub use crate::noselection::{handle_events, handle_mouse_events};
237 }
238 pub use crate::rowselection::RowSelection;
239 pub mod rowselection {
240 pub use crate::rowselection::{handle_events, handle_mouse_events};
241 }
242 pub use crate::rowsetselection::RowSetSelection;
243 pub mod rowsetselection {
244 pub use crate::rowsetselection::{handle_events, handle_mouse_events};
245 }
246}
247
248pub mod event {
250 pub use rat_event::*;
251
252 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
254 #[non_exhaustive]
255 pub enum TableOutcome {
256 Continue,
258 Unchanged,
260 Changed,
262 Selected,
264 }
265
266 impl ConsumedEvent for TableOutcome {
267 fn is_consumed(&self) -> bool {
268 *self != TableOutcome::Continue
269 }
270 }
271
272 impl From<TableOutcome> for Outcome {
273 fn from(value: TableOutcome) -> Self {
274 match value {
275 TableOutcome::Continue => Outcome::Continue,
276 TableOutcome::Unchanged => Outcome::Unchanged,
277 TableOutcome::Changed => Outcome::Changed,
278 TableOutcome::Selected => Outcome::Changed,
279 }
280 }
281 }
282
283 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
285 pub enum DoubleClickOutcome {
286 Continue,
288 Unchanged,
291 Changed,
296 ClickClick(usize, usize),
298 }
299
300 impl From<DoubleClickOutcome> for Outcome {
301 fn from(value: DoubleClickOutcome) -> Self {
302 match value {
303 DoubleClickOutcome::Continue => Outcome::Continue,
304 DoubleClickOutcome::Unchanged => Outcome::Unchanged,
305 DoubleClickOutcome::Changed => Outcome::Changed,
306 DoubleClickOutcome::ClickClick(_, _) => Outcome::Changed,
307 }
308 }
309 }
310
311 impl From<Outcome> for DoubleClickOutcome {
312 fn from(value: Outcome) -> Self {
313 match value {
314 Outcome::Continue => DoubleClickOutcome::Continue,
315 Outcome::Unchanged => DoubleClickOutcome::Unchanged,
316 Outcome::Changed => DoubleClickOutcome::Changed,
317 }
318 }
319 }
320
321 impl ConsumedEvent for DoubleClickOutcome {
322 fn is_consumed(&self) -> bool {
323 !matches!(self, DoubleClickOutcome::Continue)
324 }
325 }
326
327 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
329 pub enum EditOutcome {
330 Continue,
332 Unchanged,
335 Changed,
340 Cancel,
342 Commit,
344 CommitAndAppend,
346 CommitAndEdit,
348 Insert,
350 Remove,
352 Edit,
354 Append,
357 }
358
359 impl From<Outcome> for EditOutcome {
360 fn from(value: Outcome) -> Self {
361 match value {
362 Outcome::Continue => EditOutcome::Continue,
363 Outcome::Unchanged => EditOutcome::Unchanged,
364 Outcome::Changed => EditOutcome::Changed,
365 }
366 }
367 }
368
369 impl From<EditOutcome> for Outcome {
370 fn from(value: EditOutcome) -> Self {
371 match value {
372 EditOutcome::Continue => Outcome::Continue,
373 EditOutcome::Unchanged => Outcome::Unchanged,
374 EditOutcome::Changed => Outcome::Changed,
375 EditOutcome::Insert => Outcome::Unchanged,
376 EditOutcome::Remove => Outcome::Unchanged,
377 EditOutcome::Edit => Outcome::Unchanged,
378 EditOutcome::Append => Outcome::Unchanged,
379 EditOutcome::Cancel => Outcome::Unchanged,
380 EditOutcome::Commit => Outcome::Unchanged,
381 EditOutcome::CommitAndAppend => Outcome::Unchanged,
382 EditOutcome::CommitAndEdit => Outcome::Unchanged,
383 }
384 }
385 }
386
387 impl ConsumedEvent for EditOutcome {
388 fn is_consumed(&self) -> bool {
389 !matches!(self, EditOutcome::Continue)
390 }
391 }
392}
393
394mod _private {
395 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
396 pub struct NonExhaustive;
397}