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
/// Implements forward calls to table for a Table widget.
///
/// __Only useful if you want to implement a new Table widget__
macro_rules! derive_table_widget {
() => {
/// Set the table-header.
#[inline]
pub fn header<I>(mut self, header: I) -> Self
where
I: IntoIterator,
I::Item: Into<ratatui_core::text::Text<'a>>,
{
self.core = self.core.header(header);
self
}
/// Set the table-footer.
#[inline]
pub fn footer<I>(mut self, footer: I) -> Self
where
I: IntoIterator,
I::Item: Into<ratatui_core::text::Text<'a>>,
{
self.core = self.core.footer(footer);
self
}
/// Column widths as Constraints.
///
/// The constraints work in tandem with [Table::layout_width].
#[inline]
pub fn widths<I>(mut self, widths: I) -> Self
where
I: IntoIterator,
I::Item: Into<ratatui_core::layout::Constraint>,
{
self.core = self.core.widths(widths);
self
}
/// If your table uses varying row-heights for each row
/// all page calculations will fail (somewhat).
/// If you enable this flag, the row-heights for
/// each row will be stored in the state to allow
/// accurate page-calculations.
pub fn varying_heights(mut self, varying: bool) -> Self {
self.core = self.core.varying_heights(varying);
self
}
/// Flex-value for column layout.
#[inline]
pub fn flex(mut self, flex: ratatui_core::layout::Flex) -> Self {
self.core = self.core.flex(flex);
self
}
/// Spacing between columns.
#[inline]
pub fn column_spacing(mut self, spacing: u16) -> Self {
self.core = self.core.column_spacing(spacing);
self
}
/// Set the display width of the table.
///
/// Default
/// The width of the rendered area is used.
#[inline]
pub fn layout_width(mut self, width: $crate::LayoutWidth) -> Self {
self.core = self.core.layout_width(width);
self
}
/// Draws a block around the table widget.
#[inline]
pub fn block(mut self, block: ratatui_widgets::block::Block<'a>) -> Self {
self.core = self.core.block(block);
self
}
/// Sets the border-style for the Block, if any.
#[inline]
pub fn border_style(mut self, style: ratatui_core::style::Style) -> Self {
self.core = self.core.border_style(style);
self
}
/// Sets the title-style for the Block, if any.
#[inline]
pub fn title_style(mut self, style: ratatui_core::style::Style) -> Self {
self.core = self.core.title_style(style);
self
}
/// Scrollbars
#[inline]
pub fn scroll(mut self, scroll: rat_scrolled::Scroll<'a>) -> Self {
self.core = self.core.scroll(scroll);
self
}
/// Scrollbars
#[inline]
pub fn hscroll(mut self, scroll: rat_scrolled::Scroll<'a>) -> Self {
self.core = self.core.hscroll(scroll);
self
}
/// Scrollbars
#[inline]
pub fn vscroll(mut self, scroll: rat_scrolled::Scroll<'a>) -> Self {
self.core = self.core.vscroll(scroll);
self
}
/// Set all styles as a bundle.
#[inline]
pub fn styles(mut self, styles: $crate::TableStyle) -> Self {
self.core = self.core.styles(styles);
self
}
/// Base style for the table.
#[inline]
pub fn style(mut self, style: ratatui_core::style::Style) -> Self {
self.core = self.core.style(style);
self
}
/// Base style for the table.
#[inline]
pub fn header_style(mut self, style: Option<ratatui_core::style::Style>) -> Self {
self.core = self.core.header_style(style);
self
}
/// Base style for the table.
#[inline]
pub fn footer_style(mut self, style: Option<ratatui_core::style::Style>) -> Self {
self.core = self.core.footer_style(style);
self
}
/// Style for a selected row. The chosen selection must support
/// row-selection for this to take effect.
#[inline]
pub fn select_row_style(
mut self,
select_style: Option<ratatui_core::style::Style>,
) -> Self {
self.core = self.core.select_row_style(select_style);
self
}
/// Add the focus-style to the row-style if the table is focused.
#[inline]
pub fn show_row_focus(mut self, show: bool) -> Self {
self.core = self.core.show_row_focus(show);
self
}
/// Style for a selected column. The chosen selection must support
/// column-selection for this to take effect.
#[inline]
pub fn select_column_style(
mut self,
select_style: Option<ratatui_core::style::Style>,
) -> Self {
self.core = self.core.select_column_style(select_style);
self
}
/// Add the focus-style to the column-style if the table is focused.
#[inline]
pub fn show_column_focus(mut self, show: bool) -> Self {
self.core = self.core.show_column_focus(show);
self
}
/// Style for a selected cell. The chosen selection must support
/// cell-selection for this to take effect.
#[inline]
pub fn select_cell_style(
mut self,
select_style: Option<ratatui_core::style::Style>,
) -> Self {
self.core = self.core.select_cell_style(select_style);
self
}
/// Add the focus-style to the cell-style if the table is focused.
#[inline]
pub fn show_cell_focus(mut self, show: bool) -> Self {
self.core = self.core.show_cell_focus(show);
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(
mut self,
select_style: Option<ratatui_core::style::Style>,
) -> Self {
self.core = self.core.select_header_style(select_style);
self
}
/// Add the focus-style to the header-style if the table is focused.
#[inline]
pub fn show_header_focus(mut self, show: bool) -> Self {
self.core = self.core.show_header_focus(show);
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(
mut self,
select_style: Option<ratatui_core::style::Style>,
) -> Self {
self.core = self.core.select_footer_style(select_style);
self
}
/// Add the footer-style to the table-style if the table is focused.
#[inline]
pub fn show_footer_focus(mut self, show: bool) -> Self {
self.core = self.core.show_footer_focus(show);
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(mut self, focus_style: Option<ratatui_core::style::Style>) -> Self {
self.core = self.core.focus_style(focus_style);
self
}
/// Inherent width.
#[inline]
pub fn width(&self) -> u16 {
self.core.width()
}
/// Inherent height.
#[inline]
pub fn height(&self) -> u16 {
self.core.height()
}
};
(REF_T) => {
/// A closure that calculates the row-height for a given row-value.
#[inline]
pub fn row_height(mut self, row_height_fn: fn(&T, &$crate::TableContext) -> u8) -> Self {
self.core = self.core.row_height(row_height_fn);
self
}
/// A closure for row-level rendering.
/// The area covers the full row.
///
/// Return
///
/// Returns true if the individual cells should be rendered too.
///
#[inline]
pub fn render_row(
mut self,
render_row_fn: impl FnMut(&T, Rect, &mut Buffer, &$crate::TableContext) -> bool + 'a,
) -> Self {
self.core = self.core.render_row(render_row_fn);
self
}
/// A closure for rendering a single column.
///
/// The area covers the exact column. Extra information is
/// available in the TableContext.
///
/// The given Buffer covers the full row so you could
/// render something outside the cells bounds. Cells for a
/// given row are always rendered in ascending order.
#[inline]
pub fn render_cell(
mut self,
column: usize,
render_cell_fn: impl FnMut(&T, Rect, &mut Buffer, &$crate::TableContext) + 'a,
) -> Self {
self.core = self.core.render_cell(column, render_cell_fn);
self
}
};
}
/// Implements forward calls to table for a Table widget state struct.
/// The functions come in groups. See [TableState][crate::TableState]
/// for usage.
///
/// __Only useful if you want to implement a new Table widget__
macro_rules! derive_table_widget_state {
(BASE) => {
/// Number of rows.
#[inline]
pub fn rows(&self) -> usize {
self.table.rows()
}
/// Number of columns.
#[inline]
pub fn columns(&self) -> usize {
self.table.columns()
}
};
(AREAS) => {
/// Returns the whole row-area and the cell-areas for the
/// given row, if it is visible.
///
/// Attention: These areas might be 0-length if the column is scrolled
/// beyond the table-area.
#[inline]
pub fn row_cells(&self, row: usize) -> Option<(Rect, Vec<Rect>)> {
self.table.row_cells(row)
}
/// Cell at given position.
#[inline]
pub fn cell_at_clicked(&self, pos: (u16, u16)) -> Option<(usize, usize)> {
self.table.cell_at_clicked(pos)
}
/// Column at given position.
#[inline]
pub fn column_at_clicked(&self, pos: (u16, u16)) -> Option<usize> {
self.table.column_at_clicked(pos)
}
/// Row at given position.
#[inline]
pub fn row_at_clicked(&self, pos: (u16, u16)) -> Option<usize> {
self.table.row_at_clicked(pos)
}
/// Cell when dragging. Position can be outside the table area.
/// See [row_at_drag](Self::row_at_drag), [col_at_drag](Self::column_at_drag)
#[inline]
pub fn cell_at_drag(&self, pos: (u16, u16)) -> (usize, usize) {
self.table.cell_at_drag(pos)
}
/// Row when dragging. Position can be outside the table area.
/// If the position is above the table-area this returns offset - #rows.
/// If the position is below the table-area this returns offset + page_len + #rows.
///
/// This doesn't account for the row-height of the actual rows outside
/// the table area, just assumes '1'.
#[inline]
pub fn row_at_drag(&self, pos: (u16, u16)) -> usize {
self.table.row_at_drag(pos)
}
/// Column when dragging. Position can be outside the table area.
/// If the position is left of the table area this returns offset - 1.
/// If the position is right of the table area this returns offset + page_width + 1.
#[inline]
pub fn column_at_drag(&self, pos: (u16, u16)) -> usize {
self.table.column_at_drag(pos)
}
};
(OFFSET) => {
/// Sets both offsets to 0.
#[inline]
pub fn clear_offset(&mut self) {
self.table.clear_offset()
}
/// Maximum offset that is accessible with scrolling.
///
/// This is shorter than the length by whatever fills the last page.
/// This is the base for the scrollbar content_length.
#[inline]
pub fn row_max_offset(&self) -> usize {
self.table.row_max_offset()
}
/// Current vertical offset.
#[inline]
pub fn row_offset(&self) -> usize {
self.table.row_offset()
}
/// Change the vertical offset.
/// The offset may be beyond the number of rows.
///
/// The widget returns true if the offset changed at all.
#[inline]
pub fn set_row_offset(&mut self, offset: usize) -> bool {
self.table.set_row_offset(offset)
}
/// Vertical page-size at the current offset.
#[inline]
pub fn page_len(&self) -> usize {
self.table.page_len()
}
/// Suggested scroll per scroll-event.
#[inline]
pub fn row_scroll_by(&self) -> usize {
self.table.row_scroll_by()
}
/// 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.
#[inline]
pub fn x_max_offset(&self) -> usize {
self.table.x_max_offset()
}
/// Current horizontal offset.
#[inline]
pub fn x_offset(&self) -> usize {
self.table.x_offset()
}
/// Change the horizontal offset.
/// The offset may be beyond the table-width.
///
/// Returns true if the offset changed at all.
#[inline]
pub fn set_x_offset(&mut self, offset: usize) -> bool {
self.table.set_x_offset(offset)
}
/// Horizontal page-size at the current offset.
#[inline]
pub fn page_width(&self) -> usize {
self.table.page_width()
}
/// Suggested scroll per scroll-event.
#[inline]
pub fn x_scroll_by(&self) -> usize {
self.table.x_scroll_by()
}
/// Ensures that the selected item is visible.
/// Caveat: This doesn't work nicely if you have varying row-heights.
/// Caveat: Number of rows needs to be correct.
#[inline]
pub fn scroll_to_selected(&mut self) -> bool {
self.table.scroll_to_selected()
}
/// Ensures that the given row is visible.
/// Caveat: This doesn't work nicely if you have varying row-heights.
/// Caveat: Number of rows needs to be correct.
#[inline]
pub fn scroll_to_row(&mut self, pos: usize) -> bool {
self.table.scroll_to_row(pos)
}
/// Ensures that the given column is completely visible.
#[inline]
pub fn scroll_to_col(&mut self, pos: usize) -> bool {
self.table.scroll_to_col(pos)
}
/// Ensures that the given position is visible.
#[inline]
pub fn scroll_to_x(&mut self, pos: usize) -> bool {
self.table.scroll_to_x(pos)
}
/// Reduce the row-offset by n.
#[inline]
pub fn scroll_up(&mut self, n: usize) -> bool {
self.table.scroll_up(n)
}
/// Increase the row-offset by n.
#[inline]
pub fn scroll_down(&mut self, n: usize) -> bool {
self.table.scroll_down(n)
}
/// Reduce the col-offset by n.
#[inline]
pub fn scroll_left(&mut self, n: usize) -> bool {
self.table.scroll_left(n)
}
/// Increase the col-offset by n.
#[inline]
pub fn scroll_right(&mut self, n: usize) -> bool {
self.table.scroll_right(n)
}
};
(SELECTION) => {
/// Clear the selection.
#[inline]
pub fn clear_selection(&mut self) {
self.table.clear_selection()
}
};
(ROWSELECTION) => {
/// When scrolling the table, change the selection instead of the offset.
#[inline]
pub fn set_scroll_selection(&mut self, scroll: bool) {
self.table.set_scroll_selection(scroll)
}
/// Anything selected?
#[inline]
pub fn has_selection(&mut self) -> bool {
self.table.has_selection()
}
/// Selected row.
/// The selected row is not constrained by the row-count.
#[inline]
pub fn selected(&self) -> Option<usize> {
self.table.selected()
}
/// Return the selected row and ensure it is in the
/// range `0..rows`.
#[inline]
pub fn selected_checked(&self) -> Option<usize> {
self.table.selected_checked()
}
/// Select the row.
/// The selection is not constrained by the row-count.
#[inline]
pub fn select(&mut self, row: Option<usize>) -> bool {
self.table.select(row)
}
/// Set the selection to None and set the offset to 0
#[inline]
pub fn move_deselect(&mut self) -> bool {
self.table.move_deselect()
}
/// Move the selection to the given row.
/// Ensures the row is visible afterward.
#[inline]
pub fn move_to(&mut self, row: usize) -> bool {
self.table.move_to(row)
}
/// Move the selection up n rows.
/// Ensures the row is visible afterward.
#[inline]
pub fn move_up(&mut self, n: usize) -> bool {
self.table.move_up(n)
}
/// Move the selection down n rows.
/// Ensures the row is visible afterward.
#[inline]
pub fn move_down(&mut self, n: usize) -> bool {
self.table.move_down(n)
}
};
(ROWSETSELECTION) => {
/// Anything selected?
#[inline]
pub fn has_selection(&mut self) -> bool {
self.table.has_selection()
}
/// Selected rows.
#[inline]
pub fn selected(&self) -> HashSet<usize> {
self.table.selected()
}
/// Change the lead-selection. Limits the value to the number of rows.
/// If extend is false the current selection is cleared and both lead and
/// anchor are set to the given value.
/// If extend is true, the anchor is kept where it is and lead is changed.
/// Everything in the range `anchor..lead` is selected. It doesn't matter
/// if anchor < lead.
#[inline]
pub fn set_lead(&mut self, row: Option<usize>, extend: bool) -> bool {
self.table.set_lead(row, extend)
}
/// Current lead.
#[inline]
pub fn lead(&self) -> Option<usize> {
self.table.lead()
}
/// Current anchor.
#[inline]
pub fn anchor(&self) -> Option<usize> {
self.table.anchor()
}
/// Retire the current anchor/lead selection to the set of selected rows.
/// Resets lead and anchor and starts a new selection round.
#[inline]
pub fn retire_selection(&mut self) {
self.table.retire_selection()
}
/// Add to selection. Only works for retired selections, not for the
/// active anchor-lead range.
///
/// To be sure call [retire_selection][Self::retire_selection] first.
#[inline]
pub fn add_selected(&mut self, idx: usize) {
self.table.add_selected(idx)
}
/// Remove from selection. Only works for retired selections, not for the
/// active anchor-lead range.
///
/// To be sure call [retire_selection][Self::retire_selection] first.
#[inline]
pub fn remove_selected(&mut self, idx: usize) {
self.table.remove_selected(idx)
}
/// Move the selection to the given row.
/// Ensures the row is visible afterward.
#[inline]
pub fn move_to(&mut self, row: usize, extend: bool) -> bool {
self.table.move_to(row, extend)
}
/// Move the selection up n rows.
/// Ensures the row is visible afterward.
#[inline]
pub fn move_up(&mut self, n: usize, extend: bool) -> bool {
self.table.move_up(n, extend)
}
/// Move the selection down n rows.
/// Ensures the row is visible afterwards.
#[inline]
pub fn move_down(&mut self, n: usize, extend: bool) -> bool {
self.table.move_down(n, extend)
}
};
(CELLSELECTION) => {
#[inline]
pub fn has_selection(&mut self) -> bool {
self.table.has_selection()
}
/// Selected cell.
#[inline]
pub fn selected(&self) -> Option<(usize, usize)> {
self.table.selected()
}
/// Select a cell.
#[inline]
pub fn select_cell(&mut self, select: Option<(usize, usize)>) -> bool {
self.table.select_cell(select)
}
/// Select a row. Column stays the same.
#[inline]
pub fn select_row(&mut self, row: Option<usize>) -> bool {
self.table.select_row(row)
}
/// Select a column, row stays the same.
#[inline]
pub fn select_column(&mut self, column: Option<usize>) -> bool {
self.table.select_column(column)
}
/// Select a cell, limit to maximum.
#[inline]
pub fn move_to(&mut self, select: (usize, usize)) -> bool {
self.table.move_to(select)
}
/// Select a row, limit to maximum.
#[inline]
pub fn move_to_row(&mut self, row: usize) -> bool {
self.table.move_to_row(row)
}
/// Select a cell, clamp between 0 and maximum.
#[inline]
pub fn move_to_col(&mut self, col: usize) -> bool {
self.table.move_to_col(col)
}
/// Move the selection up n rows.
/// Ensures the row is visible afterwards.
#[inline]
pub fn move_up(&mut self, n: usize) -> bool {
self.table.move_up(n)
}
/// Move the selection down n rows.
/// Ensures the row is visible afterwards.
#[inline]
pub fn move_down(&mut self, n: usize) -> bool {
self.table.move_down(n)
}
/// Move the selection left n columns.
/// Ensures the row is visible afterwards.
#[inline]
pub fn move_left(&mut self, n: usize) -> bool {
self.table.move_left(n)
}
/// Move the selection right n columns.
/// Ensures the row is visible afterwards.
#[inline]
pub fn move_right(&mut self, n: usize) -> bool {
self.table.move_right(n)
}
};
(ITEMS) => {
/// Update the state to match adding items *during* event-handling.
/// A lot of functions use the number of rows, so when you change the
/// underlying data you might want to immediately reflect this in the
/// table-state.
///
/// This corrects the number of rows, offset and selection.
#[inline]
pub fn items_added(&mut self, pos: usize, n: usize) {
self.table.items_added(pos, n)
}
/// Update the state to match removing items *during* event-handling.
/// A lot of functions use the number of rows, so when you change the
/// underlying data you might want to immediately reflect this in the
/// table-state.
///
/// This corrects the number of rows, offset and selection.
#[inline]
pub fn items_removed(&mut self, pos: usize, n: usize) {
self.table.items_removed(pos, n)
}
/// Update the number of rows *during* event-handling.
/// A lot of functions use the number of rows, so when you change the
/// underlying data you might want to immediately reflect this in the
/// table-state.
///
/// This corrects the number of rows, offset and selection.
#[inline]
pub fn rows_changed(&mut self, rows: usize) {
self.table.rows_changed(rows)
}
};
}
pub(crate) use derive_table_widget;
pub(crate) use derive_table_widget_state;