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
use ratatui_core::layout::Rect;
bitflags::bitflags! {
/// Which lines a [`Table`] draws around and between its cells.
///
/// Each flag draws one family of lines:
///
/// - [`TOP`], [`RIGHT`], [`BOTTOM`] and [`LEFT`] draw a line on that edge of the table area,
/// where [`Block::bordered`] would draw its border.
/// - [`INNER_VERTICAL`] draws a line in the gap between each pair of adjacent columns.
/// - [`INNER_HORIZONTAL`] draws a line between each pair of adjacent rows.
/// - [`HEADER_SEPARATOR`] draws a line between the header and the rows.
/// - [`FOOTER_SEPARATOR`] draws a line between the rows and the footer.
///
/// [`OUTER`], [`INNER`] and [`ALL`] combine them. The default, [`TableBorders::empty`], draws
/// no lines. Set the flags with [`Table::borders`], the glyphs with [`Table::border_set`] and
/// the style with [`Table::border_style`]. See [Borders] for where each line goes.
///
/// With a header `A B` and two rows:
///
/// ```text
/// ALL INNER_VERTICAL | HEADER_SEPARATOR HEADER_SEPARATOR
/// ┌─┬─┐ A│B A B
/// │A│B│ ─┼─ ───
/// ├─┼─┤ 1│2 1 2
/// │1│2│ 3│4 3 4
/// ├─┼─┤
/// │3│4│
/// └─┴─┘
/// ```
///
/// # Examples
///
/// Flags combine with `|` or [`TableBorders::union`]:
///
/// ```rust
/// use ratatui::layout::Constraint;
/// use ratatui_table::{Row, Table, TableBorders};
///
/// let columns = TableBorders::INNER_VERTICAL | TableBorders::HEADER_SEPARATOR;
/// let framed = TableBorders::OUTER.union(columns);
/// assert!(framed.contains(TableBorders::LEFT | TableBorders::INNER_VERTICAL));
/// let rows = TableBorders::INNER_HORIZONTAL | TableBorders::FOOTER_SEPARATOR;
/// assert_eq!(framed | rows, TableBorders::ALL);
///
/// let table = Table::new([Row::new(["1", "2"])], [Constraint::Length(1); 2])
/// .header(Row::new(["A", "B"]))
/// .borders(framed);
/// ```
///
/// [`Table`]: crate::Table
/// [`Table::borders`]: crate::Table::borders
/// [`Table::border_set`]: crate::Table::border_set
/// [`Table::border_style`]: crate::Table::border_style
/// [Borders]: crate::Table#borders
/// [`Block::bordered`]: ratatui_widgets::block::Block::bordered
/// [`TOP`]: TableBorders::TOP
/// [`RIGHT`]: TableBorders::RIGHT
/// [`BOTTOM`]: TableBorders::BOTTOM
/// [`LEFT`]: TableBorders::LEFT
/// [`INNER_VERTICAL`]: TableBorders::INNER_VERTICAL
/// [`INNER_HORIZONTAL`]: TableBorders::INNER_HORIZONTAL
/// [`HEADER_SEPARATOR`]: TableBorders::HEADER_SEPARATOR
/// [`FOOTER_SEPARATOR`]: TableBorders::FOOTER_SEPARATOR
/// [`OUTER`]: TableBorders::OUTER
/// [`INNER`]: TableBorders::INNER
/// [`ALL`]: TableBorders::ALL
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TableBorders: u8 {
/// A line along the top edge of the table area.
const TOP = 1 << 0;
/// A line along the right edge of the table area.
const RIGHT = 1 << 1;
/// A line along the bottom edge of the table area.
const BOTTOM = 1 << 2;
/// A line along the left edge of the table area.
const LEFT = 1 << 3;
/// A line in the gap between each pair of adjacent columns.
const INNER_VERTICAL = 1 << 4;
/// A line between each pair of adjacent rows.
const INNER_HORIZONTAL = 1 << 5;
/// A line between the header and the rows, drawn only when the table has a header.
const HEADER_SEPARATOR = 1 << 6;
/// A line between the rows and the footer, drawn only when the table has a footer.
const FOOTER_SEPARATOR = 1 << 7;
/// The four edges of the table area.
const OUTER = Self::TOP.bits()
| Self::RIGHT.bits()
| Self::BOTTOM.bits()
| Self::LEFT.bits();
/// Every line inside the table area, including the header and footer separators.
const INNER = Self::INNER_VERTICAL.bits()
| Self::INNER_HORIZONTAL.bits()
| Self::HEADER_SEPARATOR.bits()
| Self::FOOTER_SEPARATOR.bits();
/// Every line.
const ALL = Self::OUTER.bits() | Self::INNER.bits();
}
}
impl TableBorders {
pub(crate) fn inner(self, area: Rect) -> Rect {
let left = u16::from(self.contains(Self::LEFT));
let top = u16::from(self.contains(Self::TOP));
let right = u16::from(self.contains(Self::RIGHT));
let bottom = u16::from(self.contains(Self::BOTTOM));
Rect {
x: area.x.saturating_add(left),
y: area.y.saturating_add(top),
width: area.width.saturating_sub(left + right),
height: area.height.saturating_sub(top + bottom),
}
}
pub(crate) fn column_spacing(self, spacing: u16) -> u16 {
if self.contains(Self::INNER_VERTICAL) {
spacing.max(1)
} else {
spacing
}
}
pub(crate) fn separator_height_below(self, index: usize, row_count: usize) -> u16 {
u16::from(self.contains(Self::INNER_HORIZONTAL) && index + 1 < row_count)
}
}