Skip to main content

ratatui_table/table/
table_borders.rs

1use ratatui_core::layout::Rect;
2
3bitflags::bitflags! {
4    /// Which lines a [`Table`] draws around and between its cells.
5    ///
6    /// Each flag draws one family of lines:
7    ///
8    /// - [`TOP`], [`RIGHT`], [`BOTTOM`] and [`LEFT`] draw a line on that edge of the table area,
9    ///   where [`Block::bordered`] would draw its border.
10    /// - [`INNER_VERTICAL`] draws a line in the gap between each pair of adjacent columns.
11    /// - [`INNER_HORIZONTAL`] draws a line between each pair of adjacent rows.
12    /// - [`HEADER_SEPARATOR`] draws a line between the header and the rows.
13    /// - [`FOOTER_SEPARATOR`] draws a line between the rows and the footer.
14    ///
15    /// [`OUTER`], [`INNER`] and [`ALL`] combine them. The default, [`TableBorders::empty`], draws
16    /// no lines. Set the flags with [`Table::borders`], the glyphs with [`Table::border_set`] and
17    /// the style with [`Table::border_style`]. See [Borders] for where each line goes.
18    ///
19    /// With a header `A B` and two rows:
20    ///
21    /// ```text
22    /// ALL      INNER_VERTICAL | HEADER_SEPARATOR   HEADER_SEPARATOR
23    /// ┌─┬─┐    A│B                                 A B
24    /// │A│B│    ─┼─                                 ───
25    /// ├─┼─┤    1│2                                 1 2
26    /// │1│2│    3│4                                 3 4
27    /// ├─┼─┤
28    /// │3│4│
29    /// └─┴─┘
30    /// ```
31    ///
32    /// # Examples
33    ///
34    /// Flags combine with `|` or [`TableBorders::union`]:
35    ///
36    /// ```rust
37    /// use ratatui::layout::Constraint;
38    /// use ratatui_table::{Row, Table, TableBorders};
39    ///
40    /// let columns = TableBorders::INNER_VERTICAL | TableBorders::HEADER_SEPARATOR;
41    /// let framed = TableBorders::OUTER.union(columns);
42    /// assert!(framed.contains(TableBorders::LEFT | TableBorders::INNER_VERTICAL));
43    /// let rows = TableBorders::INNER_HORIZONTAL | TableBorders::FOOTER_SEPARATOR;
44    /// assert_eq!(framed | rows, TableBorders::ALL);
45    ///
46    /// let table = Table::new([Row::new(["1", "2"])], [Constraint::Length(1); 2])
47    ///     .header(Row::new(["A", "B"]))
48    ///     .borders(framed);
49    /// ```
50    ///
51    /// [`Table`]: crate::Table
52    /// [`Table::borders`]: crate::Table::borders
53    /// [`Table::border_set`]: crate::Table::border_set
54    /// [`Table::border_style`]: crate::Table::border_style
55    /// [Borders]: crate::Table#borders
56    /// [`Block::bordered`]: ratatui_widgets::block::Block::bordered
57    /// [`TOP`]: TableBorders::TOP
58    /// [`RIGHT`]: TableBorders::RIGHT
59    /// [`BOTTOM`]: TableBorders::BOTTOM
60    /// [`LEFT`]: TableBorders::LEFT
61    /// [`INNER_VERTICAL`]: TableBorders::INNER_VERTICAL
62    /// [`INNER_HORIZONTAL`]: TableBorders::INNER_HORIZONTAL
63    /// [`HEADER_SEPARATOR`]: TableBorders::HEADER_SEPARATOR
64    /// [`FOOTER_SEPARATOR`]: TableBorders::FOOTER_SEPARATOR
65    /// [`OUTER`]: TableBorders::OUTER
66    /// [`INNER`]: TableBorders::INNER
67    /// [`ALL`]: TableBorders::ALL
68    #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
69    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70    pub struct TableBorders: u8 {
71        /// A line along the top edge of the table area.
72        const TOP = 1 << 0;
73        /// A line along the right edge of the table area.
74        const RIGHT = 1 << 1;
75        /// A line along the bottom edge of the table area.
76        const BOTTOM = 1 << 2;
77        /// A line along the left edge of the table area.
78        const LEFT = 1 << 3;
79        /// A line in the gap between each pair of adjacent columns.
80        const INNER_VERTICAL = 1 << 4;
81        /// A line between each pair of adjacent rows.
82        const INNER_HORIZONTAL = 1 << 5;
83        /// A line between the header and the rows, drawn only when the table has a header.
84        const HEADER_SEPARATOR = 1 << 6;
85        /// A line between the rows and the footer, drawn only when the table has a footer.
86        const FOOTER_SEPARATOR = 1 << 7;
87        /// The four edges of the table area.
88        const OUTER = Self::TOP.bits()
89            | Self::RIGHT.bits()
90            | Self::BOTTOM.bits()
91            | Self::LEFT.bits();
92        /// Every line inside the table area, including the header and footer separators.
93        const INNER = Self::INNER_VERTICAL.bits()
94            | Self::INNER_HORIZONTAL.bits()
95            | Self::HEADER_SEPARATOR.bits()
96            | Self::FOOTER_SEPARATOR.bits();
97        /// Every line.
98        const ALL = Self::OUTER.bits() | Self::INNER.bits();
99    }
100}
101
102impl TableBorders {
103    pub(crate) fn inner(self, area: Rect) -> Rect {
104        let left = u16::from(self.contains(Self::LEFT));
105        let top = u16::from(self.contains(Self::TOP));
106        let right = u16::from(self.contains(Self::RIGHT));
107        let bottom = u16::from(self.contains(Self::BOTTOM));
108        Rect {
109            x: area.x.saturating_add(left),
110            y: area.y.saturating_add(top),
111            width: area.width.saturating_sub(left + right),
112            height: area.height.saturating_sub(top + bottom),
113        }
114    }
115
116    pub(crate) fn column_spacing(self, spacing: u16) -> u16 {
117        if self.contains(Self::INNER_VERTICAL) {
118            spacing.max(1)
119        } else {
120            spacing
121        }
122    }
123
124    pub(crate) fn separator_height_below(self, index: usize, row_count: usize) -> u16 {
125        u16::from(self.contains(Self::INNER_HORIZONTAL) && index + 1 < row_count)
126    }
127}