1use alloc::sync::Arc;
5use alloc::vec;
6use alloc::vec::Vec;
7use core::hash::Hasher;
8use core::panic::RefUnwindSafe;
9use core::{fmt, ptr};
10
11use itertools::Itertools;
12use ratatui_core::buffer::Buffer;
13use ratatui_core::layout::{Constraint, Flex, Layout, Rect};
14use ratatui_core::style::{Style, Styled};
15use ratatui_core::symbols::line;
16use ratatui_core::text::Text;
17use ratatui_core::widgets::{StatefulWidget, Widget};
18use ratatui_widgets::block::{Block, BlockExt};
19
20use self::border_grid::BorderGrid;
21pub use self::cell::Cell;
22pub use self::highlight_placement::HighlightPlacement;
23pub use self::highlight_spacing::HighlightSpacing;
24pub use self::row::Row;
25pub use self::state::TableState;
26pub use self::table_borders::TableBorders;
27
28mod border_grid;
29mod cell;
30mod highlight_placement;
31mod highlight_spacing;
32mod row;
33mod state;
34mod table_borders;
35
36enum LazyRowHeights<'a> {
43 Uniform(u16),
45 PerRow(Arc<dyn Fn(usize) -> u16 + Send + Sync + RefUnwindSafe + 'a>),
47}
48
49impl LazyRowHeights<'_> {
50 fn height_of(&self, index: usize) -> u16 {
52 match self {
53 Self::Uniform(height) => *height,
54 Self::PerRow(heights) => heights(index),
55 }
56 }
57}
58
59impl fmt::Debug for LazyRowHeights<'_> {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 Self::Uniform(height) => f.debug_tuple("Uniform").field(height).finish(),
63 Self::PerRow(_) => f.debug_tuple("PerRow").finish(),
64 }
65 }
66}
67
68impl Clone for LazyRowHeights<'_> {
69 fn clone(&self) -> Self {
70 match self {
71 Self::Uniform(height) => Self::Uniform(*height),
72 Self::PerRow(heights) => Self::PerRow(Arc::clone(heights)),
73 }
74 }
75}
76
77impl PartialEq for LazyRowHeights<'_> {
78 fn eq(&self, other: &Self) -> bool {
79 match (self, other) {
80 (Self::Uniform(left), Self::Uniform(right)) => left == right,
81 (Self::PerRow(left), Self::PerRow(right)) => Arc::ptr_eq(left, right),
82 _ => false,
83 }
84 }
85}
86
87impl Eq for LazyRowHeights<'_> {}
88
89impl core::hash::Hash for LazyRowHeights<'_> {
90 fn hash<H: Hasher>(&self, state: &mut H) {
91 match self {
92 Self::Uniform(height) => {
93 "uniform".hash(state);
94 height.hash(state);
95 }
96 Self::PerRow(heights) => {
97 "per_row".hash(state);
98 ptr::hash(Arc::as_ptr(heights), state);
99 }
100 }
101 }
102}
103
104struct LazyRowProvider<'a> {
106 count: usize,
107 heights: LazyRowHeights<'a>,
108 factory: Arc<dyn Fn(usize) -> Row<'a> + Send + Sync + RefUnwindSafe + 'a>,
109}
110
111impl LazyRowProvider<'_> {
112 fn height_of(&self, index: usize) -> u16 {
114 self.heights.height_of(index)
115 }
116
117 fn visible_rows(
118 &self,
119 state: &TableState,
120 area: Rect,
121 borders: TableBorders,
122 ) -> (usize, usize) {
123 visible_row_range(self.count, state, area, borders, |index| {
124 let height = self.height_of(index);
125 (height, height)
126 })
127 }
128}
129
130fn visible_row_range(
142 count: usize,
143 state: &TableState,
144 area: Rect,
145 borders: TableBorders,
146 row_heights: impl Fn(usize) -> (u16, u16),
147) -> (usize, usize) {
148 if count == 0 {
149 return (0, 0);
150 }
151 let measure = |index| {
153 let (height, height_with_margin) = row_heights(index);
154 let extent =
155 height_with_margin.saturating_add(borders.separator_height_below(index, count));
156 (height, extent)
157 };
158 let last_row = count - 1;
159 let selected = state.selected.map(|selected| selected.min(last_row));
160
161 let mut start = state.offset.min(last_row);
162 if let Some(selected) = selected {
163 start = start.min(selected);
164 }
165
166 if area.height == 0 {
169 return (start, start);
170 }
171
172 let mut end = start;
177 let mut height = 0_u16;
178 while end < count {
179 let (row_height, extent) = measure(end);
180 let Some(filled) = height.checked_add(row_height) else {
181 break;
182 };
183 if filled > area.height {
184 break;
185 }
186 height = height.saturating_add(extent);
187 end += 1;
188 if window_is_full(start, end, area) {
189 break;
190 }
191 }
192
193 if let Some(selected) = selected
196 && selected >= end
197 {
198 start = selected;
201 end = selected + 1;
202 height = measure(selected).1;
203 let hanging_separator = borders.separator_height_below(selected, count);
204 while start > 0 && !window_is_full(start, end, area) {
205 let Some(filled) = height.checked_add(measure(start - 1).1) else {
206 break;
207 };
208 if filled > area.height.saturating_add(hanging_separator) {
209 break;
210 }
211 height = filled;
212 start -= 1;
213 }
214 }
215
216 if height < area.height && end < count && !window_is_full(start, end, area) {
218 end += 1;
219 }
220
221 (start, end)
222}
223
224const fn window_is_full(start: usize, end: usize, area: Rect) -> bool {
230 end - start > area.height as usize
231}
232
233impl fmt::Debug for LazyRowProvider<'_> {
234 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235 f.debug_struct("LazyRowProvider")
236 .field("count", &self.count)
237 .field("heights", &self.heights)
238 .finish_non_exhaustive()
239 }
240}
241
242impl Clone for LazyRowProvider<'_> {
243 fn clone(&self) -> Self {
244 Self {
245 count: self.count,
246 heights: self.heights.clone(),
247 factory: Arc::clone(&self.factory),
248 }
249 }
250}
251
252impl PartialEq for LazyRowProvider<'_> {
253 fn eq(&self, other: &Self) -> bool {
254 self.count == other.count
255 && self.heights == other.heights
256 && Arc::ptr_eq(&self.factory, &other.factory)
257 }
258}
259
260impl Eq for LazyRowProvider<'_> {}
261
262impl core::hash::Hash for LazyRowProvider<'_> {
263 fn hash<H: Hasher>(&self, state: &mut H) {
264 self.count.hash(state);
265 self.heights.hash(state);
266 ptr::hash(Arc::as_ptr(&self.factory), state);
267 }
268}
269
270#[derive(Debug, Clone, Eq, PartialEq, Hash)]
523pub struct Table<'a> {
524 rows: Vec<Row<'a>>,
526
527 header: Option<Row<'a>>,
529
530 footer: Option<Row<'a>>,
532
533 widths: Vec<Constraint>,
535
536 column_spacing: u16,
538
539 block: Option<Block<'a>>,
541
542 style: Style,
544
545 row_highlight_style: Style,
547
548 column_highlight_style: Style,
550
551 cell_highlight_style: Style,
553
554 highlight_symbol: Text<'a>,
556
557 highlight_spacing: HighlightSpacing,
559
560 highlight_placement: HighlightPlacement,
562
563 flex: Flex,
565
566 lazy: Option<LazyRowProvider<'a>>,
568
569 borders: TableBorders,
571
572 border_set: line::Set<'a>,
574
575 border_style: Style,
577}
578
579impl Default for Table<'_> {
580 fn default() -> Self {
581 Self {
582 rows: Vec::new(),
583 header: None,
584 footer: None,
585 widths: Vec::new(),
586 column_spacing: 1,
587 block: None,
588 style: Style::new(),
589 row_highlight_style: Style::new(),
590 column_highlight_style: Style::new(),
591 cell_highlight_style: Style::new(),
592 highlight_symbol: Text::default(),
593 highlight_spacing: HighlightSpacing::default(),
594 highlight_placement: HighlightPlacement::default(),
595 flex: Flex::Start,
596 lazy: None,
597 borders: TableBorders::empty(),
598 border_set: line::NORMAL,
599 border_style: Style::new(),
600 }
601 }
602}
603
604impl<'a> Table<'a> {
605 pub fn new<R, C>(rows: R, widths: C) -> Self
629 where
630 R: IntoIterator,
631 R::Item: Into<Row<'a>>,
632 C: IntoIterator,
633 C::Item: Into<Constraint>,
634 {
635 let widths = widths.into_iter().map(Into::into).collect_vec();
636 ensure_percentages_less_than_100(&widths);
637
638 let rows = rows.into_iter().map(Into::into).collect();
639 Self {
640 rows,
641 widths,
642 ..Default::default()
643 }
644 }
645
646 #[must_use = "method moves the value of self and returns the modified value"]
672 pub fn rows<T>(mut self, rows: T) -> Self
673 where
674 T: IntoIterator<Item = Row<'a>>,
675 {
676 self.rows = rows.into_iter().collect();
677 self.lazy = None;
678 self
679 }
680
681 #[must_use = "method moves the value of self and returns the modified value"]
699 pub fn header(mut self, header: Row<'a>) -> Self {
700 self.header = Some(header);
701 self
702 }
703
704 #[must_use = "method moves the value of self and returns the modified value"]
722 pub fn footer(mut self, footer: Row<'a>) -> Self {
723 self.footer = Some(footer);
724 self
725 }
726
727 #[must_use = "method moves the value of self and returns the modified value"]
752 pub fn widths<I>(mut self, widths: I) -> Self
753 where
754 I: IntoIterator,
755 I::Item: Into<Constraint>,
756 {
757 let widths = widths.into_iter().map(Into::into).collect_vec();
758 ensure_percentages_less_than_100(&widths);
759 self.widths = widths;
760 self
761 }
762
763 #[must_use = "method moves the value of self and returns the modified value"]
781 pub const fn column_spacing(mut self, spacing: u16) -> Self {
782 self.column_spacing = spacing;
783 self
784 }
785
786 #[must_use = "method moves the value of self and returns the modified value"]
809 pub fn block(mut self, block: Block<'a>) -> Self {
810 self.block = Some(block);
811 self
812 }
813
814 #[must_use = "method moves the value of self and returns the modified value"]
852 pub fn style<S: Into<Style>>(mut self, style: S) -> Self {
853 self.style = style.into();
854 self
855 }
856
857 #[must_use = "method moves the value of self and returns the modified value"]
881 #[deprecated(note = "use `row_highlight_style()` instead", since = "0.2.1")]
882 pub fn highlight_style<S: Into<Style>>(self, highlight_style: S) -> Self {
883 self.row_highlight_style(highlight_style)
884 }
885
886 #[must_use = "method moves the value of self and returns the modified value"]
906 pub fn row_highlight_style<S: Into<Style>>(mut self, highlight_style: S) -> Self {
907 self.row_highlight_style = highlight_style.into();
908 self
909 }
910
911 #[must_use = "method moves the value of self and returns the modified value"]
931 pub fn column_highlight_style<S: Into<Style>>(mut self, highlight_style: S) -> Self {
932 self.column_highlight_style = highlight_style.into();
933 self
934 }
935
936 #[must_use = "method moves the value of self and returns the modified value"]
956 pub fn cell_highlight_style<S: Into<Style>>(mut self, highlight_style: S) -> Self {
957 self.cell_highlight_style = highlight_style.into();
958 self
959 }
960
961 #[must_use = "method moves the value of self and returns the modified value"]
976 pub fn highlight_symbol<T: Into<Text<'a>>>(mut self, highlight_symbol: T) -> Self {
977 self.highlight_symbol = highlight_symbol.into();
978 self
979 }
980
981 #[must_use = "method moves the value of self and returns the modified value"]
1010 pub const fn highlight_spacing(mut self, value: HighlightSpacing) -> Self {
1011 self.highlight_spacing = value;
1012 self
1013 }
1014
1015 #[must_use = "method moves the value of self and returns the modified value"]
1037 pub fn highlight_placement(mut self, value: HighlightPlacement) -> Self {
1038 self.highlight_placement = value;
1039 self
1040 }
1041
1042 #[must_use = "method moves the value of self and returns the modified value"]
1076 pub const fn borders(mut self, borders: TableBorders) -> Self {
1077 self.borders = borders;
1078 self
1079 }
1080
1081 #[must_use = "method moves the value of self and returns the modified value"]
1103 pub const fn border_set(mut self, border_set: line::Set<'a>) -> Self {
1104 self.border_set = border_set;
1105 self
1106 }
1107
1108 #[must_use = "method moves the value of self and returns the modified value"]
1135 pub fn border_style<S: Into<Style>>(mut self, style: S) -> Self {
1136 self.border_style = style.into();
1137 self
1138 }
1139
1140 #[must_use = "method moves the value of self and returns the modified value"]
1164 pub const fn flex(mut self, flex: Flex) -> Self {
1165 self.flex = flex;
1166 self
1167 }
1168
1169 pub fn lazy_rows<C, F>(row_count: usize, widths: C, row: F) -> Self
1202 where
1203 C: IntoIterator,
1204 C::Item: Into<Constraint>,
1205 F: Fn(usize) -> Row<'a> + Send + Sync + RefUnwindSafe + 'a,
1206 {
1207 let widths: Vec<Constraint> = widths.into_iter().map(Into::into).collect();
1208 ensure_percentages_less_than_100(&widths);
1209 Self {
1210 lazy: Some(LazyRowProvider {
1211 count: row_count,
1212 heights: LazyRowHeights::Uniform(1),
1213 factory: Arc::new(row),
1214 }),
1215 widths,
1216 ..Default::default()
1217 }
1218 }
1219
1220 #[must_use = "method moves the value of self and returns the modified value"]
1243 pub fn lazy_row_height(mut self, height: u16) -> Self {
1244 if let Some(ref mut lazy) = self.lazy {
1245 lazy.heights = LazyRowHeights::Uniform(height);
1246 }
1247 self
1248 }
1249
1250 #[must_use = "method moves the value of self and returns the modified value"]
1281 pub fn lazy_row_height_with<H>(mut self, height: H) -> Self
1282 where
1283 H: Fn(usize) -> u16 + Send + Sync + RefUnwindSafe + 'a,
1284 {
1285 if let Some(ref mut lazy) = self.lazy {
1286 lazy.heights = LazyRowHeights::PerRow(Arc::new(height));
1287 }
1288 self
1289 }
1290}
1291
1292impl Widget for Table<'_> {
1293 fn render(self, area: Rect, buf: &mut Buffer) {
1294 Widget::render(&self, area, buf);
1295 }
1296}
1297
1298impl Widget for &Table<'_> {
1299 fn render(self, area: Rect, buf: &mut Buffer) {
1300 let mut state = TableState::default();
1301 StatefulWidget::render(self, area, buf, &mut state);
1302 }
1303}
1304
1305impl StatefulWidget for Table<'_> {
1306 type State = TableState;
1307
1308 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
1309 StatefulWidget::render(&self, area, buf, state);
1310 }
1311}
1312
1313impl StatefulWidget for &Table<'_> {
1314 type State = TableState;
1315
1316 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
1317 buf.set_style(area, self.style);
1318 self.block.as_ref().render(area, buf);
1319 let frame = self.block.inner_if_some(area);
1320 let table_area = self.borders.inner(frame);
1321 let mut grid = BorderGrid::new(self.borders, frame);
1322 if table_area.is_empty() {
1323 grid.render(buf, &self.border_set, self.border_style);
1324 return;
1325 }
1326
1327 let row_count = self.row_count();
1328 if state.selected.is_some_and(|s| s >= row_count) {
1329 state.select(Some(row_count.saturating_sub(1)));
1330 }
1331
1332 if row_count == 0 {
1333 state.select(None);
1334 }
1335
1336 let column_count = self.column_count();
1337 if state.selected_column.is_some_and(|s| s >= column_count) {
1338 state.select_column(Some(column_count.saturating_sub(1)));
1339 }
1340 if column_count == 0 {
1341 state.select_column(None);
1342 }
1343
1344 let selection_width = self.selection_width(state);
1345 let symbol_columns = self
1346 .highlight_placement
1347 .column_mask(state.selected_column(), column_count);
1348 let column_widths = self.get_column_widths(
1349 table_area.width,
1350 selection_width,
1351 column_count,
1352 &symbol_columns,
1353 );
1354 grid.add_columns(table_area, &column_widths, &symbol_columns, selection_width);
1355 let [
1356 header_top,
1357 header_area,
1358 header_bottom,
1359 header_line,
1360 rows_area,
1361 footer_line,
1362 footer_top,
1363 footer_area,
1364 footer_bottom,
1365 ] = self.layout(table_area);
1366
1367 self.render_fixed_row(self.header.as_ref(), header_area, buf, &column_widths);
1368 if let Some(header) = &self.header {
1369 grid.add_spans(header_top.y..header_bottom.bottom(), header);
1370 }
1371 if !header_line.is_empty() {
1372 grid.add_horizontal(header_line.y);
1373 }
1374
1375 self.render_rows(
1376 rows_area,
1377 buf,
1378 selection_width,
1379 state,
1380 &column_widths,
1381 &symbol_columns,
1382 &mut grid,
1383 );
1384
1385 if !footer_line.is_empty() {
1386 grid.add_horizontal(footer_line.y);
1387 }
1388 self.render_fixed_row(self.footer.as_ref(), footer_area, buf, &column_widths);
1389 if let Some(footer) = &self.footer {
1390 grid.add_spans(footer_top.y..footer_bottom.bottom(), footer);
1391 }
1392
1393 grid.render(buf, &self.border_set, self.border_style);
1394 }
1395}
1396
1397impl Table<'_> {
1399 fn layout(&self, area: Rect) -> [Rect; 9] {
1400 let header_top_margin = self.header.as_ref().map_or(0, |h| h.top_margin);
1401 let header_height = self.header.as_ref().map_or(0, |h| h.height);
1402 let header_bottom_margin = self.header.as_ref().map_or(0, |h| h.bottom_margin);
1403 let footer_top_margin = self.footer.as_ref().map_or(0, |h| h.top_margin);
1404 let footer_height = self.footer.as_ref().map_or(0, |f| f.height);
1405 let footer_bottom_margin = self.footer.as_ref().map_or(0, |h| h.bottom_margin);
1406 let header_line = u16::from(
1407 self.header.is_some() && self.borders.contains(TableBorders::HEADER_SEPARATOR),
1408 );
1409 let footer_line = u16::from(
1410 self.footer.is_some() && self.borders.contains(TableBorders::FOOTER_SEPARATOR),
1411 );
1412 Layout::vertical([
1413 Constraint::Length(header_top_margin),
1414 Constraint::Length(header_height),
1415 Constraint::Length(header_bottom_margin),
1416 Constraint::Length(header_line),
1417 Constraint::Min(0),
1418 Constraint::Length(footer_line),
1419 Constraint::Length(footer_top_margin),
1420 Constraint::Length(footer_height),
1421 Constraint::Length(footer_bottom_margin),
1422 ])
1423 .areas(area)
1424 }
1425
1426 fn render_fixed_row(
1429 &self,
1430 row: Option<&Row>,
1431 area: Rect,
1432 buf: &mut Buffer,
1433 column_widths: &[Rect],
1434 ) {
1435 if let Some(row) = row {
1436 buf.set_style(area, row.style);
1437 self.render_row_cells(buf, column_widths, &row.cells, area);
1438 }
1439 }
1440
1441 #[expect(clippy::too_many_arguments)]
1446 fn render_rows(
1447 &self,
1448 area: Rect,
1449 buf: &mut Buffer,
1450 selection_width: u16,
1451 state: &mut TableState,
1452 columns_widths: &[Rect],
1453 symbol_columns: &[bool],
1454 grid: &mut BorderGrid,
1455 ) {
1456 if let Some(ref provider) = self.lazy {
1457 self.render_lazy_rows(
1458 area,
1459 buf,
1460 selection_width,
1461 state,
1462 columns_widths,
1463 symbol_columns,
1464 provider,
1465 grid,
1466 );
1467 return;
1468 }
1469
1470 if self.rows.is_empty() {
1471 return;
1472 }
1473
1474 let (start_index, end_index) = self.visible_rows(state, area);
1475 state.offset = start_index;
1476
1477 let mut y_offset = 0_u16;
1478
1479 let mut selected_row_area = None;
1480 for (i, row) in self
1481 .rows
1482 .iter()
1483 .enumerate()
1484 .skip(start_index)
1485 .take(end_index - start_index)
1486 {
1487 let top = area.y.saturating_add(y_offset);
1488 let y = top.saturating_add(row.top_margin);
1489 let height = (y + row.height).min(area.bottom()).saturating_sub(y);
1490 let row_area = Rect { y, height, ..area };
1491 buf.set_style(row_area, row.style);
1492
1493 let is_selected = state.selected() == Some(i);
1494 self.render_highlight_symbol(
1495 buf,
1496 row_area,
1497 row,
1498 i,
1499 start_index,
1500 state,
1501 columns_widths,
1502 symbol_columns,
1503 selection_width,
1504 );
1505 self.render_row_cells(buf, columns_widths, &row.cells, row_area);
1506 if is_selected {
1507 selected_row_area = Some(row_area);
1508 }
1509 y_offset = y_offset.saturating_add(row.height_with_margin());
1510 let line_y = area.y.saturating_add(y_offset);
1511 grid.add_spans(top..line_y.min(area.bottom()), row);
1512 let below = self.borders.separator_height_below(i, self.rows.len());
1513 if below > 0 && i + 1 < end_index && line_y < area.bottom() {
1514 grid.add_horizontal(line_y);
1515 }
1516 y_offset = y_offset.saturating_add(below);
1517 }
1518
1519 self.apply_highlight_styles(buf, area, selected_row_area, state, columns_widths);
1520 }
1521
1522 #[expect(clippy::too_many_arguments)]
1524 fn render_lazy_rows(
1525 &self,
1526 area: Rect,
1527 buf: &mut Buffer,
1528 selection_width: u16,
1529 state: &mut TableState,
1530 columns_widths: &[Rect],
1531 symbol_columns: &[bool],
1532 provider: &LazyRowProvider,
1533 grid: &mut BorderGrid,
1534 ) {
1535 if provider.count == 0 {
1536 return;
1537 }
1538
1539 let (start_index, end_index) = provider.visible_rows(state, area, self.borders);
1540 state.offset = start_index;
1541
1542 let mut y = area.y;
1543 let mut selected_row_area = None;
1544
1545 for index in start_index..end_index {
1546 let row = (provider.factory)(index);
1547 let row_height = provider.height_of(index);
1550 let height = y
1551 .saturating_add(row_height)
1552 .min(area.bottom())
1553 .saturating_sub(y);
1554 let row_area = Rect { y, height, ..area };
1555 buf.set_style(row_area, row.style);
1556
1557 let is_selected = state.selected() == Some(index);
1558 self.render_highlight_symbol(
1559 buf,
1560 row_area,
1561 &row,
1562 index,
1563 start_index,
1564 state,
1565 columns_widths,
1566 symbol_columns,
1567 selection_width,
1568 );
1569 self.render_row_cells(buf, columns_widths, &row.cells, row_area);
1570 if is_selected {
1571 selected_row_area = Some(row_area);
1572 }
1573 grid.add_spans(y..y.saturating_add(row_height).min(area.bottom()), &row);
1574 y = y.saturating_add(row_height);
1575 let below = self.borders.separator_height_below(index, provider.count);
1576 if below > 0 && index + 1 < end_index && y < area.bottom() {
1577 grid.add_horizontal(y);
1578 }
1579 y = y.saturating_add(below);
1580 }
1581
1582 self.apply_highlight_styles(buf, area, selected_row_area, state, columns_widths);
1583 }
1584
1585 fn apply_highlight_styles(
1587 &self,
1588 buf: &mut Buffer,
1589 area: Rect,
1590 selected_row_area: Option<Rect>,
1591 state: &TableState,
1592 columns_widths: &[Rect],
1593 ) {
1594 let selected_column_area = state.selected_column().and_then(|s| {
1595 columns_widths.get(s).map(|cell_area| Rect {
1598 x: cell_area.x + area.x,
1599 width: cell_area.width,
1600 ..area
1601 })
1602 });
1603
1604 match (selected_row_area, selected_column_area) {
1605 (Some(row_area), Some(col_area)) => {
1606 buf.set_style(row_area, self.row_highlight_style);
1607 buf.set_style(col_area, self.column_highlight_style);
1608 let cell_area = row_area.intersection(col_area);
1609 buf.set_style(cell_area, self.cell_highlight_style);
1610 }
1611 (Some(row_area), None) => {
1612 buf.set_style(row_area, self.row_highlight_style);
1613 }
1614 (None, Some(col_area)) => {
1615 buf.set_style(col_area, self.column_highlight_style);
1616 }
1617 (None, None) => (),
1618 }
1619 }
1620
1621 fn render_row_cells(
1627 &self,
1628 buf: &mut Buffer,
1629 column_widths: &[Rect],
1630 cells: &[Cell],
1631 row_area: Rect,
1632 ) {
1633 let mut column_widths_iterator = column_widths.iter();
1634 for current_cell in cells {
1635 if let Some(cell_area) = Self::get_cell_area(
1636 &mut column_widths_iterator,
1637 current_cell.column_span,
1638 self.borders.column_spacing(self.column_spacing),
1639 ) {
1640 let new_x = row_area.x + cell_area.x;
1641 let area_to_render = Rect::new(new_x, row_area.y, cell_area.width, row_area.height);
1642 current_cell.render(area_to_render, buf);
1643 }
1644 }
1645 }
1646
1647 #[expect(clippy::too_many_arguments)]
1660 fn render_highlight_symbol(
1661 &self,
1662 buf: &mut Buffer,
1663 row_area: Rect,
1664 row: &Row,
1665 row_index: usize,
1666 first_visible_row: usize,
1667 state: &TableState,
1668 columns_widths: &[Rect],
1669 symbol_columns: &[bool],
1670 selection_width: u16,
1671 ) {
1672 if selection_width == 0 {
1673 return;
1674 }
1675 if state.selected().is_none() && state.selected_column().is_none() {
1676 return;
1677 }
1678 if row_index != state.selected().unwrap_or(first_visible_row) {
1679 return;
1680 }
1681
1682 for (column_index, cell_area) in columns_widths.iter().enumerate() {
1683 if !symbol_columns.get(column_index).copied().unwrap_or(false) {
1684 continue;
1685 }
1686 let symbol_area = Rect {
1687 x: (row_area.x + cell_area.x).saturating_sub(selection_width),
1688 width: selection_width,
1689 ..row_area
1690 };
1691 buf.set_style(symbol_area, row.style);
1692 (&self.highlight_symbol).render(symbol_area, buf);
1693 }
1694 }
1695
1696 fn get_cell_area<'a, T>(
1710 column_widths_iterator: &mut T,
1711 cell_column_span: u16,
1712 column_spacing: u16,
1713 ) -> Option<Rect>
1714 where
1715 T: Iterator<Item = &'a Rect>,
1716 {
1717 if cell_column_span == 0 {
1718 return None;
1719 }
1720 let first = column_widths_iterator.next()?;
1721 let (n_columns_taken, all_columns_width) = column_widths_iterator
1722 .take((cell_column_span - 1).into())
1723 .map(|rect| (1, rect.width))
1724 .fold((1, first.width), |so_far, next_column| {
1725 (next_column.0 + so_far.0, next_column.1 + so_far.1)
1726 });
1727 let width = all_columns_width + (n_columns_taken - 1) * column_spacing;
1728 Some(Rect::new(first.x, first.y, width, 1))
1729 }
1730
1731 fn visible_rows(&self, state: &TableState, area: Rect) -> (usize, usize) {
1732 visible_row_range(self.rows.len(), state, area, self.borders, |index| {
1733 let row = &self.rows[index];
1734 (row.height, row.height_with_margin())
1735 })
1736 }
1737
1738 fn get_column_widths(
1743 &self,
1744 max_width: u16,
1745 selection_width: u16,
1746 col_count: usize,
1747 symbol_columns: &[bool],
1748 ) -> Vec<Rect> {
1749 let widths = if self.widths.is_empty() {
1750 vec![Constraint::Length(max_width / col_count.max(1) as u16); col_count]
1752 } else {
1753 self.widths.clone()
1754 };
1755
1756 let slots = symbol_columns.iter().filter(|slot| **slot).count() as u16;
1761 let content_width = max_width.saturating_sub(selection_width * slots);
1762 let rects = Layout::horizontal(widths)
1763 .flex(self.flex)
1764 .spacing(self.borders.column_spacing(self.column_spacing))
1765 .split(Rect::new(0, 0, content_width, 1));
1766
1767 let mut shift = 0;
1768 rects
1769 .iter()
1770 .enumerate()
1771 .map(|(index, rect)| {
1772 if symbol_columns.get(index).copied().unwrap_or(false) {
1773 shift += selection_width;
1774 }
1775 Rect::new(rect.x + shift, 0, rect.width, 1)
1776 })
1777 .collect()
1778 }
1779
1780 fn row_count(&self) -> usize {
1782 self.lazy
1783 .as_ref()
1784 .map_or(self.rows.len(), |provider| provider.count)
1785 }
1786
1787 fn column_count(&self) -> usize {
1788 let lazy_columns = if self.lazy.is_some() {
1791 self.widths.len()
1792 } else {
1793 0
1794 };
1795 self.rows
1796 .iter()
1797 .chain(self.footer.iter())
1798 .chain(self.header.iter())
1799 .map(|row| {
1800 row.cells
1801 .iter()
1802 .map(|cell| usize::from(cell.column_span))
1803 .sum()
1804 })
1805 .chain(Some(lazy_columns))
1806 .max()
1807 .unwrap_or_default()
1808 }
1809
1810 fn selection_width(&self, state: &TableState) -> u16 {
1816 let has_selection = state.selected().is_some() || state.selected_column().is_some();
1817 if self.highlight_spacing.should_add(has_selection) {
1818 self.highlight_symbol.width() as u16
1819 } else {
1820 0
1821 }
1822 }
1823}
1824
1825fn ensure_percentages_less_than_100(widths: &[Constraint]) {
1826 for w in widths {
1827 if let Constraint::Percentage(p) = w {
1828 assert!(
1829 *p <= 100,
1830 "Percentages should be between 0 and 100 inclusively."
1831 );
1832 }
1833 }
1834}
1835
1836impl Styled for Table<'_> {
1837 type Item = Self;
1838
1839 fn style(&self) -> Style {
1840 self.style
1841 }
1842
1843 fn set_style<S: Into<Style>>(self, style: S) -> Self::Item {
1844 self.style(style)
1845 }
1846}
1847
1848impl<'a, Item> FromIterator<Item> for Table<'a>
1849where
1850 Item: Into<Row<'a>>,
1851{
1852 fn from_iter<Iter: IntoIterator<Item = Item>>(rows: Iter) -> Self {
1857 let widths: [Constraint; 0] = [];
1858 Self::new(rows, widths)
1859 }
1860}
1861
1862#[cfg(test)]
1863mod tests {
1864 use alloc::string::ToString;
1865 use alloc::{format, vec};
1866
1867 use ratatui_core::layout::Constraint::*;
1868 use ratatui_core::style::{Color, Modifier, Style, Stylize};
1869 use ratatui_core::text::Line;
1870 use rstest::{fixture, rstest};
1871
1872 use super::*;
1873 use crate::table::Cell;
1874
1875 #[test]
1878 fn lazy_visible_rows_do_not_overflow_the_line_total() {
1879 let table = Table::lazy_rows(4, [Constraint::Length(1)], |_| Row::new(["x"]))
1880 .lazy_row_height_with(|index| if index == 0 { u16::MAX } else { 1 });
1881 let lazy = table.lazy.as_ref().expect("lazy rows");
1882 let area = Rect::new(0, 0, 1, u16::MAX);
1883
1884 let state = TableState::default();
1885 assert_eq!(
1886 lazy.visible_rows(&state, area, TableBorders::empty()),
1887 (0, 1)
1888 );
1889
1890 let state = TableState::default().with_selected(3);
1891 assert_eq!(
1892 lazy.visible_rows(&state, area, TableBorders::empty()),
1893 (1, 4)
1894 );
1895 }
1896
1897 #[test]
1898 fn new() {
1899 let rows = [Row::new(vec![Cell::from("")])];
1900 let widths = [Constraint::Percentage(100)];
1901 let table = Table::new(rows.clone(), widths);
1902 assert_eq!(table.rows, rows);
1903 assert_eq!(table.header, None);
1904 assert_eq!(table.footer, None);
1905 assert_eq!(table.widths, widths);
1906 assert_eq!(table.column_spacing, 1);
1907 assert_eq!(table.block, None);
1908 assert_eq!(table.style, Style::default());
1909 assert_eq!(table.row_highlight_style, Style::default());
1910 assert_eq!(table.highlight_symbol, Text::default());
1911 assert_eq!(table.highlight_spacing, HighlightSpacing::WhenSelected);
1912 assert_eq!(table.flex, Flex::Start);
1913 }
1914
1915 #[test]
1916 fn default() {
1917 let table = Table::default();
1918 assert_eq!(table.rows, []);
1919 assert_eq!(table.header, None);
1920 assert_eq!(table.footer, None);
1921 assert_eq!(table.widths, []);
1922 assert_eq!(table.column_spacing, 1);
1923 assert_eq!(table.block, None);
1924 assert_eq!(table.style, Style::default());
1925 assert_eq!(table.row_highlight_style, Style::default());
1926 assert_eq!(table.highlight_symbol, Text::default());
1927 assert_eq!(table.highlight_spacing, HighlightSpacing::WhenSelected);
1928 assert_eq!(table.flex, Flex::Start);
1929 }
1930
1931 #[test]
1932 fn collect() {
1933 let table = (0..4)
1934 .map(|i| -> Row { (0..4).map(|j| format!("{i}*{j} = {}", i * j)).collect() })
1935 .collect::<Table>()
1936 .widths([Constraint::Percentage(25); 4]);
1937
1938 let expected_rows: Vec<Row> = vec![
1939 Row::new(["0*0 = 0", "0*1 = 0", "0*2 = 0", "0*3 = 0"]),
1940 Row::new(["1*0 = 0", "1*1 = 1", "1*2 = 2", "1*3 = 3"]),
1941 Row::new(["2*0 = 0", "2*1 = 2", "2*2 = 4", "2*3 = 6"]),
1942 Row::new(["3*0 = 0", "3*1 = 3", "3*2 = 6", "3*3 = 9"]),
1943 ];
1944
1945 assert_eq!(table.rows, expected_rows);
1946 assert_eq!(table.widths, [Constraint::Percentage(25); 4]);
1947 }
1948
1949 #[test]
1950 fn widths() {
1951 let table = Table::default().widths([Constraint::Length(100)]);
1952 assert_eq!(table.widths, [Constraint::Length(100)]);
1953
1954 #[expect(clippy::needless_borrows_for_generic_args)]
1957 let table = Table::default().widths(&[Constraint::Length(100)]);
1958 assert_eq!(table.widths, [Constraint::Length(100)]);
1959
1960 let table = Table::default().widths(vec![Constraint::Length(100)]);
1961 assert_eq!(table.widths, [Constraint::Length(100)]);
1962
1963 #[expect(clippy::needless_borrows_for_generic_args)]
1966 let table = Table::default().widths(&vec![Constraint::Length(100)]);
1967 assert_eq!(table.widths, [Constraint::Length(100)]);
1968
1969 let table = Table::default().widths([100].into_iter().map(Constraint::Length));
1970 assert_eq!(table.widths, [Constraint::Length(100)]);
1971 }
1972
1973 #[test]
1974 fn rows() {
1975 let rows = [Row::new(vec![Cell::from("")])];
1976 let table = Table::default().rows(rows.clone());
1977 assert_eq!(table.rows, rows);
1978 }
1979
1980 #[test]
1981 fn column_spacing() {
1982 let table = Table::default().column_spacing(2);
1983 assert_eq!(table.column_spacing, 2);
1984 }
1985
1986 #[test]
1987 fn block() {
1988 let block = Block::bordered().title("Table");
1989 let table = Table::default().block(block.clone());
1990 assert_eq!(table.block, Some(block));
1991 }
1992
1993 #[test]
1994 fn header() {
1995 let header = Row::new(vec![Cell::from("")]);
1996 let table = Table::default().header(header.clone());
1997 assert_eq!(table.header, Some(header));
1998 }
1999
2000 #[test]
2001 fn footer() {
2002 let footer = Row::new(vec![Cell::from("")]);
2003 let table = Table::default().footer(footer.clone());
2004 assert_eq!(table.footer, Some(footer));
2005 }
2006
2007 #[test]
2008 #[expect(deprecated)]
2009 fn highlight_style() {
2010 let style = Style::default().red().italic();
2011 let table = Table::default().highlight_style(style);
2012 assert_eq!(table.row_highlight_style, style);
2013 }
2014
2015 #[test]
2016 fn row_highlight_style() {
2017 let style = Style::default().red().italic();
2018 let table = Table::default().row_highlight_style(style);
2019 assert_eq!(table.row_highlight_style, style);
2020 }
2021
2022 #[test]
2023 fn column_highlight_style() {
2024 let style = Style::default().red().italic();
2025 let table = Table::default().column_highlight_style(style);
2026 assert_eq!(table.column_highlight_style, style);
2027 }
2028
2029 #[test]
2030 fn cell_highlight_style() {
2031 let style = Style::default().red().italic();
2032 let table = Table::default().cell_highlight_style(style);
2033 assert_eq!(table.cell_highlight_style, style);
2034 }
2035
2036 #[test]
2037 fn highlight_symbol() {
2038 let table = Table::default().highlight_symbol(">>");
2039 assert_eq!(table.highlight_symbol, Text::from(">>"));
2040 }
2041
2042 #[test]
2043 fn highlight_spacing() {
2044 let table = Table::default().highlight_spacing(HighlightSpacing::Always);
2045 assert_eq!(table.highlight_spacing, HighlightSpacing::Always);
2046 }
2047
2048 #[test]
2049 #[should_panic = "Percentages should be between 0 and 100 inclusively"]
2050 fn table_invalid_percentages() {
2051 let _ = Table::default().widths([Constraint::Percentage(110)]);
2052 }
2053
2054 #[test]
2055 fn widths_conversions() {
2056 let array = [Constraint::Percentage(100)];
2057 let table = Table::new(Vec::<Row>::new(), array);
2058 assert_eq!(table.widths, [Constraint::Percentage(100)], "array");
2059
2060 let array_ref = &[Constraint::Percentage(100)];
2061 let table = Table::new(Vec::<Row>::new(), array_ref);
2062 assert_eq!(table.widths, [Constraint::Percentage(100)], "array ref");
2063
2064 let vec = vec![Constraint::Percentage(100)];
2065 let slice = vec.as_slice();
2066 let table = Table::new(Vec::<Row>::new(), slice);
2067 assert_eq!(table.widths, [Constraint::Percentage(100)], "slice");
2068
2069 let vec = vec![Constraint::Percentage(100)];
2070 let table = Table::new(Vec::<Row>::new(), vec);
2071 assert_eq!(table.widths, [Constraint::Percentage(100)], "vec");
2072
2073 let vec_ref = &vec![Constraint::Percentage(100)];
2074 let table = Table::new(Vec::<Row>::new(), vec_ref);
2075 assert_eq!(table.widths, [Constraint::Percentage(100)], "vec ref");
2076 }
2077
2078 #[cfg(test)]
2079 mod state {
2080 use ratatui_core::buffer::Buffer;
2081 use ratatui_core::layout::{Constraint, Rect};
2082 use ratatui_core::widgets::StatefulWidget;
2083
2084 use super::*;
2085 use crate::table::{Row, Table, TableState};
2086
2087 #[fixture]
2088 fn table_buf() -> Buffer {
2089 Buffer::empty(Rect::new(0, 0, 10, 10))
2090 }
2091
2092 #[rstest]
2093 fn test_list_state_empty_list(mut table_buf: Buffer) {
2094 let mut state = TableState::default();
2095
2096 let rows: Vec<Row> = Vec::new();
2097 let widths = vec![Constraint::Percentage(100)];
2098 let table = Table::new(rows, widths);
2099 state.select_first();
2100 StatefulWidget::render(table, table_buf.area, &mut table_buf, &mut state);
2101 assert_eq!(state.selected(), None);
2102 assert_eq!(state.selected_column(), None);
2103 }
2104
2105 #[rstest]
2106 fn test_list_state_single_item(mut table_buf: Buffer) {
2107 let mut state = TableState::default();
2108
2109 let widths = vec![Constraint::Percentage(100)];
2110
2111 let items = vec![Row::new(vec!["Item 1"])];
2112 let table = Table::new(items, widths);
2113 state.select_first();
2114 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2115 assert_eq!(state.selected(), Some(0));
2116 assert_eq!(state.selected_column(), None);
2117
2118 state.select_last();
2119 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2120 assert_eq!(state.selected(), Some(0));
2121 assert_eq!(state.selected_column(), None);
2122
2123 state.select_previous();
2124 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2125 assert_eq!(state.selected(), Some(0));
2126 assert_eq!(state.selected_column(), None);
2127
2128 state.select_next();
2129 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2130 assert_eq!(state.selected(), Some(0));
2131 assert_eq!(state.selected_column(), None);
2132
2133 let mut state = TableState::default();
2134
2135 state.select_first_column();
2136 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2137 assert_eq!(state.selected_column(), Some(0));
2138 assert_eq!(state.selected(), None);
2139
2140 state.select_last_column();
2141 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2142 assert_eq!(state.selected_column(), Some(0));
2143 assert_eq!(state.selected(), None);
2144
2145 state.select_previous_column();
2146 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2147 assert_eq!(state.selected_column(), Some(0));
2148 assert_eq!(state.selected(), None);
2149
2150 state.select_next_column();
2151 StatefulWidget::render(&table, table_buf.area, &mut table_buf, &mut state);
2152 assert_eq!(state.selected_column(), Some(0));
2153 assert_eq!(state.selected(), None);
2154 }
2155 }
2156
2157 #[cfg(test)]
2158 mod render {
2159 use ratatui_core::layout::Alignment;
2160
2161 use super::*;
2162
2163 #[test]
2164 fn render_empty_area() {
2165 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2166 let rows = vec![Row::new(vec!["Cell1", "Cell2"])];
2167 let table = Table::new(rows, vec![Constraint::Length(5); 2]);
2168 Widget::render(table, Rect::new(0, 0, 0, 0), &mut buf);
2169 assert_eq!(buf, Buffer::empty(Rect::new(0, 0, 15, 3)));
2170 }
2171
2172 #[test]
2173 fn render_default() {
2174 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2175 let table = Table::default();
2176 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2177 assert_eq!(buf, Buffer::empty(Rect::new(0, 0, 15, 3)));
2178 }
2179
2180 #[test]
2181 fn render_with_block() {
2182 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2183 let rows = vec![
2184 Row::new(vec!["Cell1", "Cell2"]),
2185 Row::new(vec!["Cell3", "Cell4"]),
2186 ];
2187 let block = Block::bordered().title("Block");
2188 let table = Table::new(rows, vec![Constraint::Length(5); 2]).block(block);
2189 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2190 #[rustfmt::skip]
2191 let expected = Buffer::with_lines([
2192 "┌Block────────┐",
2193 "│Cell1 Cell2 │",
2194 "└─────────────┘",
2195 ]);
2196 assert_eq!(buf, expected);
2197 }
2198
2199 #[rstest]
2200 #[case(15, 5, vec![
2201 Row::new(vec![
2202 Cell::new("Cell1").column_span(1),
2203 Cell::new("Cell2").column_span(1),
2204 ]),
2205 Row::new(vec![
2206 Cell::new("Cell3").column_span(1),
2207 Cell::new("Cell4").column_span(1),
2208 ]),
2209 ],
2210 &Buffer::with_lines(["Cell1 Cell2 ", "Cell3 Cell4 "]))]
2211 #[case(15, 5, vec![
2212 Row::new(vec![
2213 Cell::new("Cell1").column_span(0),
2214 Cell::new("Cell2").column_span(1),
2215 ]),
2216 Row::new(vec![
2217 Cell::new("Cell3").column_span(1),
2218 Cell::new("Cell4").column_span(1),
2219 ]),
2220 ], &Buffer::with_lines(["Cell2 ", "Cell3 Cell4 "]))]
2221 #[case(15, 5, vec![
2222 Row::new(vec![
2223 Cell::new("Cell1").column_span(2),
2224 Cell::new("Cell2").column_span(1),
2225 ]),
2226 Row::new(vec![
2227 Cell::new("Cell3").column_span(1),
2228 Cell::new("Cell4").column_span(1),
2229 ]),
2230 ], &Buffer::with_lines(["Cell1 ", "Cell3 Cell4 "]))]
2231 fn test_colspans_2_cols<'rows, Rows>(
2232 #[case] width: u16,
2233 #[case] column_width: u16,
2234 #[case] rows: Rows,
2235 #[case] expected: &Buffer,
2236 ) where
2237 Rows: IntoIterator<Item = Row<'rows>>,
2238 {
2239 let mut buf = Buffer::empty(Rect::new(0, 0, width, 2));
2240 let table = Table::new(rows, [Constraint::Length(column_width); 2]);
2241 Widget::render(table, Rect::new(0, 0, width, 2), &mut buf);
2242 assert_eq!(buf, *expected);
2243 }
2244
2245 #[rstest]
2246 #[case(17, 5, vec![
2247 Row::new(vec![
2248 Cell::new("Cell1").column_span(2),
2249 Cell::new("Cell2").column_span(1),
2250 ]),
2251 Row::new(vec![
2252 Cell::new("Cell3").column_span(1),
2253 Cell::new("Cell4").column_span(1),
2254 Cell::new("Cell5").column_span(1),
2255 ]),
2256 ], &Buffer::with_lines(["Cell1 Cell2", "Cell3 Cell4 Cell5"]))]
2257 #[case(17, 5, vec![
2258 Row::new(vec![
2259 Cell::new("Cell1").column_span(1),
2260 Cell::new("Cell2").column_span(2),
2261 Cell::new("Cell3").column_span(1),
2262 ]),
2263 Row::new(vec![
2264 Cell::new("Cell4").column_span(1),
2265 Cell::new("Cell5").column_span(1),
2266 Cell::new("Cell6").column_span(1),
2267 ]),
2268 ], &Buffer::with_lines(["Cell1 Cell2 ", "Cell4 Cell5 Cell6"]))]
2269 #[case(15, 5, vec![
2270 Row::new(vec![
2271 Cell::new("11111111111111111111").column_span(2),
2272 Cell::new("22222222222222222222").column_span(1),
2273 ]),
2274 Row::new(vec![
2275 Cell::new("33333333333333333333").column_span(1),
2276 Cell::new("44444444444444444444").column_span(2),
2277 Cell::new("55555555555555555555").column_span(1),
2278 ]),
2279 ], &Buffer::with_lines(["1111111111 2222", "3333 4444444444"]))]
2280 fn test_colspans_3_cols<'rows, Rows>(
2281 #[case] width: u16,
2282 #[case] column_width: u16,
2283 #[case] rows: Rows,
2284 #[case] expected: &Buffer,
2285 ) where
2286 Rows: IntoIterator<Item = Row<'rows>>,
2287 {
2288 let mut buf = Buffer::empty(Rect::new(0, 0, width, 2));
2289 let table = Table::new(rows, [Constraint::Length(column_width); 3]);
2290 Widget::render(table, Rect::new(0, 0, width, 2), &mut buf);
2291 assert_eq!(buf, *expected);
2292 }
2293
2294 #[test]
2295 fn render_with_header() {
2296 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2297 let header = Row::new(vec!["Head1", "Head2"]);
2298 let rows = vec![
2299 Row::new(vec!["Cell1", "Cell2"]),
2300 Row::new(vec!["Cell3", "Cell4"]),
2301 ];
2302 let table = Table::new(rows, [Constraint::Length(5); 2]).header(header);
2303 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2304 #[rustfmt::skip]
2305 let expected = Buffer::with_lines([
2306 "Head1 Head2 ",
2307 "Cell1 Cell2 ",
2308 "Cell3 Cell4 ",
2309 ]);
2310 assert_eq!(buf, expected);
2311 }
2312
2313 #[test]
2314 fn render_with_footer() {
2315 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2316 let footer = Row::new(vec!["Foot1", "Foot2"]);
2317 let rows = vec![
2318 Row::new(vec!["Cell1", "Cell2"]),
2319 Row::new(vec!["Cell3", "Cell4"]),
2320 ];
2321 let table = Table::new(rows, [Constraint::Length(5); 2]).footer(footer);
2322 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2323 #[rustfmt::skip]
2324 let expected = Buffer::with_lines([
2325 "Cell1 Cell2 ",
2326 "Cell3 Cell4 ",
2327 "Foot1 Foot2 ",
2328 ]);
2329 assert_eq!(buf, expected);
2330 }
2331
2332 #[test]
2333 fn render_with_header_and_footer() {
2334 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2335 let header = Row::new(vec!["Head1", "Head2"]);
2336 let footer = Row::new(vec!["Foot1", "Foot2"]);
2337 let rows = vec![Row::new(vec!["Cell1", "Cell2"])];
2338 let table = Table::new(rows, [Constraint::Length(5); 2])
2339 .header(header)
2340 .footer(footer);
2341 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2342 #[rustfmt::skip]
2343 let expected = Buffer::with_lines([
2344 "Head1 Head2 ",
2345 "Cell1 Cell2 ",
2346 "Foot1 Foot2 ",
2347 ]);
2348 assert_eq!(buf, expected);
2349 }
2350
2351 #[test]
2352 fn render_header_and_footer_cells_across_their_column_span() {
2353 let mut buf = Buffer::empty(Rect::new(0, 0, 11, 3));
2354 let table = Table::new([Row::new(["a", "b"])], [Constraint::Length(5); 2])
2355 .header(Row::new([Cell::new("Header spans").column_span(2)]))
2356 .footer(Row::new([Cell::new("Footer spans").column_span(2)]));
2357 Widget::render(table, buf.area, &mut buf);
2358 #[rustfmt::skip]
2359 let expected = Buffer::with_lines([
2360 "Header span",
2361 "a b ",
2362 "Footer span",
2363 ]);
2364 assert_eq!(buf, expected);
2365 }
2366
2367 #[test]
2368 fn render_default_widths_split_between_every_spanned_column() {
2369 let mut buf = Buffer::empty(Rect::new(0, 0, 11, 1));
2370 let rows = [Row::new([Cell::new("AB").column_span(2), Cell::new("C")])];
2371 Widget::render(Table::default().rows(rows), buf.area, &mut buf);
2372 assert_eq!(buf, Buffer::with_lines(["AB C "]));
2373 }
2374
2375 #[test]
2376 fn render_keeps_selected_column_covered_by_a_spanned_cell() {
2377 let mut buf = Buffer::empty(Rect::new(0, 0, 5, 1));
2378 let rows = [Row::new([Cell::new("AB").column_span(2), Cell::new("C")])];
2379 let table = Table::new(rows, [Constraint::Length(1); 3]);
2380 let mut state = TableState::new().with_selected_column(Some(2));
2381 StatefulWidget::render(table, buf.area, &mut buf, &mut state);
2382 assert_eq!(state.selected_column(), Some(2));
2383 }
2384
2385 #[test]
2386 fn render_with_header_margin() {
2387 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2388 let header = Row::new(vec!["Head1", "Head2"]).bottom_margin(1);
2389 let rows = vec![
2390 Row::new(vec!["Cell1", "Cell2"]),
2391 Row::new(vec!["Cell3", "Cell4"]),
2392 ];
2393 let table = Table::new(rows, [Constraint::Length(5); 2]).header(header);
2394 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2395 #[rustfmt::skip]
2396 let expected = Buffer::with_lines([
2397 "Head1 Head2 ",
2398 " ",
2399 "Cell1 Cell2 ",
2400 ]);
2401 assert_eq!(buf, expected);
2402 }
2403
2404 #[test]
2405 fn render_with_footer_margin() {
2406 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2407 let footer = Row::new(vec!["Foot1", "Foot2"]).top_margin(1);
2408 let rows = vec![Row::new(vec!["Cell1", "Cell2"])];
2409 let table = Table::new(rows, [Constraint::Length(5); 2]).footer(footer);
2410 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2411 #[rustfmt::skip]
2412 let expected = Buffer::with_lines([
2413 "Cell1 Cell2 ",
2414 " ",
2415 "Foot1 Foot2 ",
2416 ]);
2417 assert_eq!(buf, expected);
2418 }
2419
2420 #[test]
2421 fn render_with_row_margin() {
2422 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2423 let rows = vec![
2424 Row::new(vec!["Cell1", "Cell2"]).bottom_margin(1),
2425 Row::new(vec!["Cell3", "Cell4"]),
2426 ];
2427 let table = Table::new(rows, [Constraint::Length(5); 2]);
2428 Widget::render(table, Rect::new(0, 0, 15, 3), &mut buf);
2429 #[rustfmt::skip]
2430 let expected = Buffer::with_lines([
2431 "Cell1 Cell2 ",
2432 " ",
2433 "Cell3 Cell4 ",
2434 ]);
2435 assert_eq!(buf, expected);
2436 }
2437
2438 #[test]
2439 fn render_with_tall_row() {
2440 let mut buf = Buffer::empty(Rect::new(0, 0, 23, 3));
2441 let rows = vec![
2442 Row::new(vec!["Cell1", "Cell2"]),
2443 Row::new(vec![
2444 Text::raw("Cell3-Line1\nCell3-Line2\nCell3-Line3"),
2445 Text::raw("Cell4-Line1\nCell4-Line2\nCell4-Line3"),
2446 ])
2447 .height(3),
2448 ];
2449 let table = Table::new(rows, [Constraint::Length(11); 2]);
2450 Widget::render(table, Rect::new(0, 0, 23, 3), &mut buf);
2451 #[rustfmt::skip]
2452 let expected = Buffer::with_lines([
2453 "Cell1 Cell2 ",
2454 "Cell3-Line1 Cell4-Line1",
2455 "Cell3-Line2 Cell4-Line2",
2456 ]);
2457 assert_eq!(buf, expected);
2458 }
2459
2460 #[test]
2461 fn render_with_alignment() {
2462 let mut buf = Buffer::empty(Rect::new(0, 0, 10, 3));
2463 let rows = vec![
2464 Row::new(vec![Line::from("Left").alignment(Alignment::Left)]),
2465 Row::new(vec![Line::from("Center").alignment(Alignment::Center)]),
2466 Row::new(vec![Line::from("Right").alignment(Alignment::Right)]),
2467 ];
2468 let table = Table::new(rows, [Percentage(100)]);
2469 Widget::render(table, Rect::new(0, 0, 10, 3), &mut buf);
2470 let expected = Buffer::with_lines(["Left ", " Center ", " Right"]);
2471 assert_eq!(buf, expected);
2472 }
2473
2474 #[test]
2475 fn render_with_overflow_does_not_panic() {
2476 let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));
2477 let table = Table::new(Vec::<Row>::new(), [Constraint::Min(20); 1])
2478 .header(Row::new([Line::from("").alignment(Alignment::Right)]))
2479 .footer(Row::new([Line::from("").alignment(Alignment::Right)]));
2480 Widget::render(table, Rect::new(0, 0, 20, 3), &mut buf);
2481 }
2482
2483 #[test]
2484 fn render_with_selected_column_and_incorrect_width_count_does_not_panic() {
2485 let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));
2486 let table = Table::new(
2487 vec![Row::new(vec!["Row1", "Row2", "Row3"])],
2488 [Constraint::Length(10); 1],
2489 );
2490 let mut state = TableState::new().with_selected_column(2);
2491 StatefulWidget::render(table, Rect::new(0, 0, 20, 3), &mut buf, &mut state);
2492 }
2493
2494 #[test]
2495 fn render_with_selected() {
2496 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2497 let rows = vec![
2498 Row::new(vec!["Cell1", "Cell2"]),
2499 Row::new(vec!["Cell3", "Cell4"]),
2500 ];
2501 let table = Table::new(rows, [Constraint::Length(5); 2])
2502 .row_highlight_style(Style::new().red())
2503 .highlight_symbol(">>");
2504 let mut state = TableState::new().with_selected(Some(0));
2505 StatefulWidget::render(table, Rect::new(0, 0, 15, 3), &mut buf, &mut state);
2506 let expected = Buffer::with_lines([
2507 ">>Cell1 Cell2 ".red(),
2508 " Cell3 Cell4 ".into(),
2509 " ".into(),
2510 ]);
2511 assert_eq!(buf, expected);
2512 }
2513
2514 #[test]
2515 fn render_with_selected_column() {
2516 let mut buf = Buffer::empty(Rect::new(0, 0, 15, 3));
2517 let rows = vec![
2518 Row::new(vec!["Cell1", "Cell2"]),
2519 Row::new(vec!["Cell3", "Cell4"]),
2520 ];
2521 let table = Table::new(rows, [Constraint::Length(5); 2])
2522 .column_highlight_style(Style::new().blue())
2523 .highlight_symbol(">>");
2524 let mut state = TableState::new().with_selected_column(Some(1));
2525 StatefulWidget::render(table, Rect::new(0, 0, 15, 3), &mut buf, &mut state);
2526 let expected = Buffer::with_lines::<[Line; 3]>([
2527 Line::from(vec![
2528 ">>Cell1".into(),
2529 " ".into(),
2530 "Cell2".blue(),
2531 " ".into(),
2532 ]),
2533 Line::from(vec![
2534 " Cell3".into(),
2535 " ".into(),
2536 "Cell4".blue(),
2537 " ".into(),
2538 ]),
2539 Line::from(vec![" ".into(), " ".blue(), " ".into()]),
2540 ]);
2541 assert_eq!(buf, expected);
2542 }
2543
2544 #[test]
2545 fn render_with_selected_cell() {
2546 let mut buf = Buffer::empty(Rect::new(0, 0, 20, 4));
2547 let rows = vec![
2548 Row::new(vec!["Cell1", "Cell2", "Cell3"]),
2549 Row::new(vec!["Cell4", "Cell5", "Cell6"]),
2550 Row::new(vec!["Cell7", "Cell8", "Cell9"]),
2551 ];
2552 let table = Table::new(rows, [Constraint::Length(5); 3])
2553 .highlight_symbol(">>")
2554 .cell_highlight_style(Style::new().green());
2555 let mut state = TableState::new().with_selected_cell((1, 2));
2556 StatefulWidget::render(table, Rect::new(0, 0, 20, 4), &mut buf, &mut state);
2557 let expected = Buffer::with_lines::<[Line; 4]>([
2558 Line::from(vec![" Cell1 ".into(), "Cell2 ".into(), "Cell3".into()]),
2559 Line::from(vec![">>Cell4 Cell5 ".into(), "Cell6".green(), " ".into()]),
2560 Line::from(vec![" Cell7 ".into(), "Cell8 ".into(), "Cell9".into()]),
2561 Line::from(vec![" ".into()]),
2562 ]);
2563 assert_eq!(buf, expected);
2564 }
2565
2566 #[test]
2567 fn render_with_selected_row_and_column() {
2568 let mut buf = Buffer::empty(Rect::new(0, 0, 20, 4));
2569 let rows = vec![
2570 Row::new(vec!["Cell1", "Cell2", "Cell3"]),
2571 Row::new(vec!["Cell4", "Cell5", "Cell6"]),
2572 Row::new(vec!["Cell7", "Cell8", "Cell9"]),
2573 ];
2574 let table = Table::new(rows, [Constraint::Length(5); 3])
2575 .highlight_symbol(">>")
2576 .row_highlight_style(Style::new().red())
2577 .column_highlight_style(Style::new().blue());
2578 let mut state = TableState::new().with_selected(1).with_selected_column(2);
2579 StatefulWidget::render(table, Rect::new(0, 0, 20, 4), &mut buf, &mut state);
2580 let expected = Buffer::with_lines::<[Line; 4]>([
2581 Line::from(vec![" Cell1 ".into(), "Cell2 ".into(), "Cell3".blue()]),
2582 Line::from(vec![">>Cell4 Cell5 ".red(), "Cell6".blue(), " ".red()]),
2583 Line::from(vec![" Cell7 ".into(), "Cell8 ".into(), "Cell9".blue()]),
2584 Line::from(vec![" ".into(), " ".blue(), " ".into()]),
2585 ]);
2586 assert_eq!(buf, expected);
2587 }
2588
2589 #[test]
2590 fn render_with_selected_row_and_column_and_cell() {
2591 let mut buf = Buffer::empty(Rect::new(0, 0, 20, 4));
2592 let rows = vec![
2593 Row::new(vec!["Cell1", "Cell2", "Cell3"]),
2594 Row::new(vec!["Cell4", "Cell5", "Cell6"]),
2595 Row::new(vec!["Cell7", "Cell8", "Cell9"]),
2596 ];
2597 let table = Table::new(rows, [Constraint::Length(5); 3])
2598 .highlight_symbol(">>")
2599 .row_highlight_style(Style::new().red())
2600 .column_highlight_style(Style::new().blue())
2601 .cell_highlight_style(Style::new().green());
2602 let mut state = TableState::new().with_selected(1).with_selected_column(2);
2603 StatefulWidget::render(table, Rect::new(0, 0, 20, 4), &mut buf, &mut state);
2604 let expected = Buffer::with_lines::<[Line; 4]>([
2605 Line::from(vec![" Cell1 ".into(), "Cell2 ".into(), "Cell3".blue()]),
2606 Line::from(vec![">>Cell4 Cell5 ".red(), "Cell6".green(), " ".red()]),
2607 Line::from(vec![" Cell7 ".into(), "Cell8 ".into(), "Cell9".blue()]),
2608 Line::from(vec![" ".into(), " ".blue(), " ".into()]),
2609 ]);
2610 assert_eq!(buf, expected);
2611 }
2612
2613 #[rstest]
2617 #[case::no_selection(None, 50, ["50", "51", "52", "53", "54"])]
2618 #[case::selection_before_offset(20, 20, ["20", "21", "22", "23", "24"])]
2619 #[case::selection_immediately_before_offset(49, 49, ["49", "50", "51", "52", "53"])]
2620 #[case::selection_at_start_of_offset(50, 50, ["50", "51", "52", "53", "54"])]
2621 #[case::selection_at_end_of_offset(54, 50, ["50", "51", "52", "53", "54"])]
2622 #[case::selection_immediately_after_offset(55, 51, ["51", "52", "53", "54", "55"])]
2623 #[case::selection_after_offset(80, 76, ["76", "77", "78", "79", "80"])]
2624 fn render_with_selection_and_offset<T: Into<Option<usize>>>(
2625 #[case] selected_row: T,
2626 #[case] expected_offset: usize,
2627 #[case] expected_items: [&str; 5],
2628 ) {
2629 let rows = (0..100).map(|i| Row::new([i.to_string()]));
2631 let table = Table::new(rows, [Constraint::Length(2)]);
2632 let mut buf = Buffer::empty(Rect::new(0, 0, 2, 5));
2633 let mut state = TableState::new()
2634 .with_offset(50)
2635 .with_selected(selected_row.into());
2636
2637 StatefulWidget::render(table.clone(), Rect::new(0, 0, 5, 5), &mut buf, &mut state);
2638
2639 assert_eq!(buf, Buffer::with_lines(expected_items));
2640 assert_eq!(state.offset, expected_offset);
2641 }
2642 }
2643
2644 mod highlight_placement {
2645 use super::*;
2646
2647 #[track_caller]
2649 fn assert_render(
2650 placement: HighlightPlacement,
2651 selected_row: Option<usize>,
2652 selected_column: Option<usize>,
2653 expected: [&str; 3],
2654 ) {
2655 let rows = [
2656 Row::new(["A1", "A2", "A3"]),
2657 Row::new(["B1", "B2", "B3"]),
2658 Row::new(["C1", "C2", "C3"]),
2659 ];
2660 let table = Table::new(rows, [Constraint::Length(2); 3])
2661 .highlight_symbol(">>")
2662 .highlight_placement(placement);
2663 let mut state = TableState::new()
2664 .with_selected(selected_row)
2665 .with_selected_column(selected_column);
2666 let area = Rect::new(0, 0, 24, 3);
2667 let mut buf = Buffer::empty(area);
2668 StatefulWidget::render(table, area, &mut buf, &mut state);
2669 assert_eq!(buf, Buffer::with_lines(expected));
2670 }
2671
2672 #[test]
2673 fn first_column_with_row_selection() {
2674 assert_render(
2675 HighlightPlacement::FirstColumn,
2676 Some(1),
2677 None,
2678 [
2679 " A1 A2 A3 ",
2680 ">>B1 B2 B3 ",
2681 " C1 C2 C3 ",
2682 ],
2683 );
2684 }
2685
2686 #[test]
2687 fn first_column_keeps_symbol_when_a_later_column_is_selected() {
2688 assert_render(
2690 HighlightPlacement::FirstColumn,
2691 Some(1),
2692 Some(2),
2693 [
2694 " A1 A2 A3 ",
2695 ">>B1 B2 B3 ",
2696 " C1 C2 C3 ",
2697 ],
2698 );
2699 }
2700
2701 #[test]
2702 fn selected_column_moves_the_slot() {
2703 assert_render(
2704 HighlightPlacement::SelectedColumn,
2705 Some(1),
2706 Some(1),
2707 [
2708 "A1 A2 A3 ",
2709 "B1 >>B2 B3 ",
2710 "C1 C2 C3 ",
2711 ],
2712 );
2713 }
2714
2715 #[test]
2716 fn selected_column_without_a_column_falls_back_to_first() {
2717 assert_render(
2718 HighlightPlacement::SelectedColumn,
2719 Some(0),
2720 None,
2721 [
2722 ">>A1 A2 A3 ",
2723 " B1 B2 B3 ",
2724 " C1 C2 C3 ",
2725 ],
2726 );
2727 }
2728
2729 #[test]
2730 fn columns_indents_each_listed_column() {
2731 assert_render(
2732 HighlightPlacement::Columns(vec![1, 2]),
2733 Some(0),
2734 None,
2735 [
2736 "A1 >>A2 >>A3 ",
2737 "B1 B2 B3 ",
2738 "C1 C2 C3 ",
2739 ],
2740 );
2741 }
2742
2743 #[test]
2744 fn columns_ignores_out_of_range_indices() {
2745 assert_render(
2747 HighlightPlacement::Columns(vec![1, 9]),
2748 Some(0),
2749 None,
2750 [
2751 "A1 >>A2 A3 ",
2752 "B1 B2 B3 ",
2753 "C1 C2 C3 ",
2754 ],
2755 );
2756 }
2757
2758 #[test]
2759 fn all_columns() {
2760 assert_render(
2761 HighlightPlacement::AllColumns,
2762 Some(2),
2763 None,
2764 [
2765 " A1 A2 A3 ",
2766 " B1 B2 B3 ",
2767 ">>C1 >>C2 >>C3 ",
2768 ],
2769 );
2770 }
2771
2772 #[test]
2773 fn column_only_selection_anchors_to_the_first_visible_row() {
2774 let rows = ["A", "B", "C", "D"].map(|label| Row::new([label, label]));
2777 let table = Table::new(rows, [Constraint::Length(1); 2])
2778 .highlight_symbol(">>")
2779 .highlight_placement(HighlightPlacement::SelectedColumn);
2780 let mut state = TableState::new().with_offset(2).with_selected_column(1);
2781 let area = Rect::new(0, 0, 8, 2);
2782 let mut buf = Buffer::empty(area);
2783 StatefulWidget::render(table, area, &mut buf, &mut state);
2784 assert_eq!(buf, Buffer::with_lines(["C >>C ", "D D "]));
2785 }
2786
2787 #[test]
2788 fn never_spacing_allocates_no_slot() {
2789 let rows = [Row::new(["A1", "A2"]), Row::new(["B1", "B2"])];
2790 let table = Table::new(rows, [Constraint::Length(2); 2])
2791 .highlight_symbol(">>")
2792 .highlight_spacing(HighlightSpacing::Never)
2793 .highlight_placement(HighlightPlacement::AllColumns);
2794 let mut state = TableState::new().with_selected(0);
2795 let area = Rect::new(0, 0, 12, 2);
2796 let mut buf = Buffer::empty(area);
2797 StatefulWidget::render(table, area, &mut buf, &mut state);
2798 assert_eq!(buf, Buffer::with_lines(["A1 A2 ", "B1 B2 "]));
2799 }
2800
2801 #[test]
2802 fn symbol_follows_columns_not_spanned_cells() {
2803 let rows = [Row::new([
2806 Cell::from("A1").column_span(2),
2807 Cell::from("A3"),
2808 ])];
2809 let table = Table::new(rows, [Constraint::Length(2); 3])
2810 .header(Row::new(["H1", "H2", "H3"]))
2811 .highlight_symbol(">>")
2812 .highlight_placement(HighlightPlacement::AllColumns);
2813 let mut state = TableState::new().with_selected(0);
2814 let area = Rect::new(0, 0, 20, 2);
2815 let mut buf = Buffer::empty(area);
2816 StatefulWidget::render(table, area, &mut buf, &mut state);
2817 assert_eq!(
2818 buf,
2819 Buffer::with_lines([" H1 H2 H3 ", ">>A1 >> >>A3 "])
2820 );
2821 }
2822
2823 #[test]
2824 fn column_highlight_does_not_reach_header_or_footer() {
2825 let rows = [Row::new(["A1", "A2"])];
2826 let table = Table::new(rows, [Constraint::Length(2); 2])
2827 .header(Row::new(["H1", "H2"]))
2828 .footer(Row::new(["F1", "F2"]))
2829 .column_highlight_style(Style::new().blue());
2830 let mut state = TableState::new().with_selected_column(1);
2831 let area = Rect::new(0, 0, 6, 3);
2832 let mut buf = Buffer::empty(area);
2833 StatefulWidget::render(table, area, &mut buf, &mut state);
2834
2835 let mut expected = Buffer::with_lines(["H1 H2 ", "A1 A2 ", "F1 F2 "]);
2836 expected.set_style(Rect::new(3, 1, 2, 1), Style::new().blue());
2837 assert_eq!(buf, expected);
2838 }
2839 }
2840
2841 mod column_widths {
2843 use super::*;
2844
2845 #[test]
2846 fn length_constraint() {
2847 let table = Table::default().widths([Length(4), Length(4)]);
2849 assert_eq!(
2850 table.get_column_widths(20, 0, 0, &[]),
2851 [Rect::new(0, 0, 4, 1), Rect::new(5, 0, 4, 1),]
2852 );
2853
2854 let table = Table::default().widths([Length(4), Length(4)]);
2856 assert_eq!(
2857 table.get_column_widths(20, 3, 0, &[true]),
2858 [Rect::new(3, 0, 4, 1), Rect::new(8, 0, 4, 1)]
2859 );
2860
2861 let table = Table::default().widths([Length(4), Length(4)]);
2863 assert_eq!(
2864 table.get_column_widths(7, 0, 0, &[]),
2865 [Rect::new(0, 0, 3, 1), Rect::new(4, 0, 3, 1)]
2866 );
2867
2868 let table = Table::default().widths([Length(4), Length(4)]);
2875 assert_eq!(
2876 table.get_column_widths(7, 3, 0, &[true]),
2877 [Rect::new(3, 0, 2, 1), Rect::new(6, 0, 1, 1)]
2878 );
2879 }
2880
2881 #[test]
2882 fn max_constraint() {
2883 let table = Table::default().widths([Max(4), Max(4)]);
2885 assert_eq!(
2886 table.get_column_widths(20, 0, 0, &[]),
2887 [Rect::new(0, 0, 4, 1), Rect::new(5, 0, 4, 1)]
2888 );
2889
2890 let table = Table::default().widths([Max(4), Max(4)]);
2892 assert_eq!(
2893 table.get_column_widths(20, 3, 0, &[true]),
2894 [Rect::new(3, 0, 4, 1), Rect::new(8, 0, 4, 1)]
2895 );
2896
2897 let table = Table::default().widths([Max(4), Max(4)]);
2899 assert_eq!(
2900 table.get_column_widths(7, 0, 0, &[]),
2901 [Rect::new(0, 0, 3, 1), Rect::new(4, 0, 3, 1)]
2902 );
2903
2904 let table = Table::default().widths([Max(4), Max(4)]);
2906 assert_eq!(
2907 table.get_column_widths(7, 3, 0, &[true]),
2908 [Rect::new(3, 0, 2, 1), Rect::new(6, 0, 1, 1)]
2909 );
2910 }
2911
2912 #[test]
2913 fn min_constraint() {
2914 let table = Table::default().widths([Min(4), Min(4)]);
2920 assert_eq!(
2921 table.get_column_widths(20, 0, 0, &[]),
2922 [Rect::new(0, 0, 10, 1), Rect::new(11, 0, 9, 1)]
2923 );
2924
2925 let table = Table::default().widths([Min(4), Min(4)]);
2927 assert_eq!(
2928 table.get_column_widths(20, 3, 0, &[true]),
2929 [Rect::new(3, 0, 8, 1), Rect::new(12, 0, 8, 1)]
2930 );
2931
2932 let table = Table::default().widths([Min(4), Min(4)]);
2935 assert_eq!(
2936 table.get_column_widths(7, 0, 0, &[]),
2937 [Rect::new(0, 0, 3, 1), Rect::new(4, 0, 3, 1)]
2938 );
2939
2940 let table = Table::default().widths([Min(4), Min(4)]);
2943 assert_eq!(
2944 table.get_column_widths(7, 3, 0, &[true]),
2945 [Rect::new(3, 0, 2, 1), Rect::new(6, 0, 1, 1)]
2946 );
2947 }
2948
2949 #[test]
2950 fn percentage_constraint() {
2951 let table = Table::default().widths([Percentage(30), Percentage(30)]);
2953 assert_eq!(
2954 table.get_column_widths(20, 0, 0, &[]),
2955 [Rect::new(0, 0, 6, 1), Rect::new(7, 0, 6, 1)]
2956 );
2957
2958 let table = Table::default().widths([Percentage(30), Percentage(30)]);
2960 assert_eq!(
2961 table.get_column_widths(20, 3, 0, &[true]),
2962 [Rect::new(3, 0, 5, 1), Rect::new(9, 0, 5, 1)]
2963 );
2964
2965 let table = Table::default().widths([Percentage(30), Percentage(30)]);
2968 assert_eq!(
2969 table.get_column_widths(7, 0, 0, &[]),
2970 [Rect::new(0, 0, 2, 1), Rect::new(3, 0, 2, 1)]
2971 );
2972
2973 let table = Table::default().widths([Percentage(30), Percentage(30)]);
2976 assert_eq!(
2977 table.get_column_widths(7, 3, 0, &[true]),
2978 [Rect::new(3, 0, 1, 1), Rect::new(5, 0, 1, 1)]
2979 );
2980 }
2981
2982 #[test]
2983 fn ratio_constraint() {
2984 let table = Table::default().widths([Ratio(1, 3), Ratio(1, 3)]);
2987 assert_eq!(
2988 table.get_column_widths(20, 0, 0, &[]),
2989 [Rect::new(0, 0, 7, 1), Rect::new(8, 0, 6, 1)]
2990 );
2991
2992 let table = Table::default().widths([Ratio(1, 3), Ratio(1, 3)]);
2995 assert_eq!(
2996 table.get_column_widths(20, 3, 0, &[true]),
2997 [Rect::new(3, 0, 6, 1), Rect::new(10, 0, 5, 1)]
2998 );
2999
3000 let table = Table::default().widths([Ratio(1, 3), Ratio(1, 3)]);
3003 assert_eq!(
3004 table.get_column_widths(7, 0, 0, &[]),
3005 [Rect::new(0, 0, 2, 1), Rect::new(3, 0, 3, 1)]
3006 );
3007
3008 let table = Table::default().widths([Ratio(1, 3), Ratio(1, 3)]);
3011 assert_eq!(
3012 table.get_column_widths(7, 3, 0, &[true]),
3013 [Rect::new(3, 0, 1, 1), Rect::new(5, 0, 2, 1)]
3014 );
3015 }
3016
3017 #[test]
3019 fn underconstrained_flex() {
3020 let table = Table::default().widths([Min(10), Min(10), Min(1)]);
3021 assert_eq!(
3022 table.get_column_widths(62, 0, 0, &[]),
3023 &[
3024 Rect::new(0, 0, 20, 1),
3025 Rect::new(21, 0, 20, 1),
3026 Rect::new(42, 0, 20, 1)
3027 ]
3028 );
3029
3030 let table = Table::default()
3031 .widths([Min(10), Min(10), Min(1)])
3032 .flex(Flex::Legacy);
3033 assert_eq!(
3034 table.get_column_widths(62, 0, 0, &[]),
3035 &[
3036 Rect::new(0, 0, 10, 1),
3037 Rect::new(11, 0, 10, 1),
3038 Rect::new(22, 0, 40, 1)
3039 ]
3040 );
3041
3042 let table = Table::default()
3043 .widths([Min(10), Min(10), Min(1)])
3044 .flex(Flex::SpaceBetween);
3045 assert_eq!(
3046 table.get_column_widths(62, 0, 0, &[]),
3047 &[
3048 Rect::new(0, 0, 20, 1),
3049 Rect::new(21, 0, 20, 1),
3050 Rect::new(42, 0, 20, 1)
3051 ]
3052 );
3053 }
3054
3055 #[test]
3056 fn underconstrained_segment_size() {
3057 let table = Table::default().widths([Min(10), Min(10), Min(1)]);
3058 assert_eq!(
3059 table.get_column_widths(62, 0, 0, &[]),
3060 &[
3061 Rect::new(0, 0, 20, 1),
3062 Rect::new(21, 0, 20, 1),
3063 Rect::new(42, 0, 20, 1)
3064 ]
3065 );
3066
3067 let table = Table::default()
3068 .widths([Min(10), Min(10), Min(1)])
3069 .flex(Flex::Legacy);
3070 assert_eq!(
3071 table.get_column_widths(62, 0, 0, &[]),
3072 &[
3073 Rect::new(0, 0, 10, 1),
3074 Rect::new(11, 0, 10, 1),
3075 Rect::new(22, 0, 40, 1)
3076 ]
3077 );
3078 }
3079
3080 #[test]
3081 fn no_constraint_with_rows() {
3082 let table = Table::default()
3083 .rows(vec![
3084 Row::new(vec!["a", "b"]),
3085 Row::new(vec!["c", "d", "e"]),
3086 ])
3087 .header(Row::new(vec!["f", "g"]))
3089 .footer(Row::new(vec!["h", "i"]))
3090 .column_spacing(0);
3091 assert_eq!(
3092 table.get_column_widths(30, 0, 3, &[]),
3093 &[
3094 Rect::new(0, 0, 10, 1),
3095 Rect::new(10, 0, 10, 1),
3096 Rect::new(20, 0, 10, 1)
3097 ]
3098 );
3099 }
3100
3101 #[test]
3102 fn no_constraint_with_header() {
3103 let table = Table::default()
3104 .rows(vec![])
3105 .header(Row::new(vec!["f", "g"]))
3106 .column_spacing(0);
3107 assert_eq!(
3108 table.get_column_widths(10, 0, 2, &[]),
3109 [Rect::new(0, 0, 5, 1), Rect::new(5, 0, 5, 1)]
3110 );
3111 }
3112
3113 #[test]
3114 fn no_constraint_with_footer() {
3115 let table = Table::default()
3116 .rows(vec![])
3117 .footer(Row::new(vec!["h", "i"]))
3118 .column_spacing(0);
3119 assert_eq!(
3120 table.get_column_widths(10, 0, 2, &[]),
3121 [Rect::new(0, 0, 5, 1), Rect::new(5, 0, 5, 1)]
3122 );
3123 }
3124
3125 #[track_caller]
3126 fn test_table_with_selection<'line, Lines>(
3127 highlight_spacing: HighlightSpacing,
3128 columns: u16,
3129 spacing: u16,
3130 selection: Option<usize>,
3131 expected: Lines,
3132 ) where
3133 Lines: IntoIterator,
3134 Lines::Item: Into<Line<'line>>,
3135 {
3136 let table = Table::default()
3137 .rows(vec![Row::new(vec!["ABCDE", "12345"])])
3138 .highlight_spacing(highlight_spacing)
3139 .highlight_symbol(">>>")
3140 .column_spacing(spacing);
3141 let area = Rect::new(0, 0, columns, 3);
3142 let mut buf = Buffer::empty(area);
3143 let mut state = TableState::default().with_selected(selection);
3144 StatefulWidget::render(table, area, &mut buf, &mut state);
3145 assert_eq!(buf, Buffer::with_lines(expected));
3146 }
3147
3148 #[test]
3149 fn excess_area_highlight_symbol_and_column_spacing_allocation() {
3150 test_table_with_selection(
3152 HighlightSpacing::Never,
3153 15, 0, None, [
3157 "ABCDE 12345 ", " ", " ", ],
3163 );
3164
3165 let table = Table::default()
3166 .rows(vec![Row::new(vec!["ABCDE", "12345"])])
3167 .widths([5, 5])
3168 .column_spacing(0);
3169 let area = Rect::new(0, 0, 15, 3);
3170 let mut buf = Buffer::empty(area);
3171 Widget::render(table, area, &mut buf);
3172 let expected = Buffer::with_lines([
3173 "ABCDE12345 ", " ", " ", ]);
3178 assert_eq!(buf, expected);
3179
3180 test_table_with_selection(
3182 HighlightSpacing::Never,
3183 15, 0, Some(0), [
3187 "ABCDE 12345 ", " ", " ", ],
3191 );
3192
3193 test_table_with_selection(
3195 HighlightSpacing::WhenSelected,
3196 15, 0, None, [
3200 "ABCDE 12345 ", " ", " ", ],
3204 );
3205 test_table_with_selection(
3207 HighlightSpacing::WhenSelected,
3208 15, 0, Some(0), [
3212 ">>>ABCDE 12345 ", " ", " ", ],
3216 );
3217
3218 test_table_with_selection(
3220 HighlightSpacing::Always,
3221 15, 0, None, [
3225 " ABCDE 12345 ", " ", " ", ],
3229 );
3230
3231 test_table_with_selection(
3233 HighlightSpacing::Always,
3234 15, 0, Some(0), [
3238 ">>>ABCDE 12345 ", " ", " ", ],
3242 );
3243 }
3244
3245 #[expect(clippy::too_many_lines)]
3246 #[test]
3247 fn insufficient_area_highlight_symbol_and_column_spacing_allocation() {
3248 test_table_with_selection(
3250 HighlightSpacing::Never,
3251 10, 1, None, [
3255 "ABCDE 1234", " ", " ", ],
3259 );
3260 test_table_with_selection(
3261 HighlightSpacing::WhenSelected,
3262 10, 1, None, [
3266 "ABCDE 1234", " ", " ", ],
3270 );
3271
3272 test_table_with_selection(
3281 HighlightSpacing::Always,
3282 10, 1, None, [
3286 " ABC 123", " ", " ", ],
3290 );
3291
3292 test_table_with_selection(
3294 HighlightSpacing::Always,
3295 9, 1, None, [
3299 " ABC 12", " ", " ", ],
3303 );
3304 test_table_with_selection(
3305 HighlightSpacing::Always,
3306 8, 1, None, [
3310 " AB 12", " ", " ", ],
3314 );
3315 test_table_with_selection(
3316 HighlightSpacing::Always,
3317 7, 1, None, [
3321 " AB 1", " ", " ", ],
3325 );
3326
3327 let table = Table::default()
3328 .rows(vec![Row::new(vec!["ABCDE", "12345"])])
3329 .highlight_spacing(HighlightSpacing::Always)
3330 .flex(Flex::Legacy)
3331 .highlight_symbol(">>>")
3332 .column_spacing(1);
3333 let area = Rect::new(0, 0, 10, 3);
3334 let mut buf = Buffer::empty(area);
3335 Widget::render(table, area, &mut buf);
3336 #[rustfmt::skip]
3338 let expected = Buffer::with_lines([
3339 " ABCDE 1",
3340 " ",
3341 " ",
3342 ]);
3343 assert_eq!(buf, expected);
3344
3345 let table = Table::default()
3346 .rows(vec![Row::new(vec!["ABCDE", "12345"])])
3347 .highlight_spacing(HighlightSpacing::Always)
3348 .flex(Flex::Start)
3349 .highlight_symbol(">>>")
3350 .column_spacing(1);
3351 let area = Rect::new(0, 0, 10, 3);
3352 let mut buf = Buffer::empty(area);
3353 Widget::render(table, area, &mut buf);
3354 #[rustfmt::skip]
3356 let expected = Buffer::with_lines([
3357 " ABC 123",
3358 " ",
3359 " ",
3360 ]);
3361 assert_eq!(buf, expected);
3362
3363 test_table_with_selection(
3364 HighlightSpacing::Never,
3365 10, 1, Some(0), [
3369 "ABCDE 1234", " ",
3371 " ",
3372 ],
3373 );
3374
3375 test_table_with_selection(
3376 HighlightSpacing::WhenSelected,
3377 10, 1, Some(0), [
3381 ">>>ABC 123", " ", " ", ],
3385 );
3386
3387 test_table_with_selection(
3388 HighlightSpacing::Always,
3389 10, 1, Some(0), [
3393 ">>>ABC 123", " ", " ", ],
3397 );
3398 }
3399
3400 #[test]
3401 fn insufficient_area_highlight_symbol_allocation_with_no_column_spacing() {
3402 test_table_with_selection(
3403 HighlightSpacing::Never,
3404 10, 0, None, [
3408 "ABCDE12345", " ", " ", ],
3412 );
3413 test_table_with_selection(
3414 HighlightSpacing::WhenSelected,
3415 10, 0, None, [
3419 "ABCDE12345", " ", " ", ],
3423 );
3424 test_table_with_selection(
3429 HighlightSpacing::Always,
3430 10, 0, None, [
3434 " ABCD123", " ", " ", ],
3438 );
3439 test_table_with_selection(
3440 HighlightSpacing::Never,
3441 10, 0, Some(0), [
3445 "ABCDE12345", " ", " ", ],
3449 );
3450 test_table_with_selection(
3451 HighlightSpacing::WhenSelected,
3452 10, 0, Some(0), [
3456 ">>>ABCD123", " ", " ", ],
3460 );
3461 test_table_with_selection(
3462 HighlightSpacing::Always,
3463 10, 0, Some(0), [
3467 ">>>ABCD123", " ", " ", ],
3471 );
3472 }
3473 }
3474
3475 #[test]
3476 fn stylize() {
3477 assert_eq!(
3478 Table::new(vec![Row::new(vec![Cell::from("")])], [Percentage(100)])
3479 .black()
3480 .on_white()
3481 .bold()
3482 .not_crossed_out()
3483 .style,
3484 Style::default()
3485 .fg(Color::Black)
3486 .bg(Color::White)
3487 .add_modifier(Modifier::BOLD)
3488 .remove_modifier(Modifier::CROSSED_OUT)
3489 );
3490 }
3491
3492 #[rstest]
3493 #[case::no_columns(vec![], vec![], vec![], 0)]
3494 #[case::only_header(vec!["H1", "H2"], vec![], vec![], 2)]
3495 #[case::only_rows(
3496 vec![],
3497 vec![vec!["C1", "C2"], vec!["C1", "C2", "C3"]],
3498 vec![],
3499 3
3500 )]
3501 #[case::only_footer(vec![], vec![], vec!["F1", "F2", "F3", "F4"], 4)]
3502 #[case::rows_longer(
3503 vec!["H1", "H2", "H3", "H4"],
3504 vec![vec!["C1", "C2"],vec!["C1", "C2", "C3"]],
3505 vec!["F1", "F2"],
3506 4
3507 )]
3508 #[case::rows_longer(
3509 vec!["H1", "H2"],
3510 vec![vec!["C1", "C2"], vec!["C1", "C2", "C3", "C4"]],
3511 vec!["F1", "F2"],
3512 4
3513 )]
3514 #[case::footer_longer(
3515 vec!["H1", "H2"],
3516 vec![vec!["C1", "C2"], vec!["C1", "C2", "C3"]],
3517 vec!["F1", "F2", "F3", "F4"],
3518 4
3519 )]
3520
3521 fn column_count(
3522 #[case] header: Vec<&str>,
3523 #[case] rows: Vec<Vec<&str>>,
3524 #[case] footer: Vec<&str>,
3525 #[case] expected: usize,
3526 ) {
3527 let header = Row::new(header);
3528 let footer = Row::new(footer);
3529 let rows: Vec<Row> = rows.into_iter().map(Row::new).collect();
3530 let table = Table::new(rows, Vec::<Constraint>::new())
3531 .header(header)
3532 .footer(footer);
3533 let column_count = table.column_count();
3534 assert_eq!(column_count, expected);
3535 }
3536
3537 #[rstest]
3538 #[case::spanned_cell_counts_every_column(
3539 Row::new([Cell::new("AB").column_span(2), Cell::new("C")]),
3540 3
3541 )]
3542 #[case::zero_span_cell_counts_no_column(
3543 Row::new([Cell::new("hidden").column_span(0), Cell::new("C")]),
3544 1
3545 )]
3546 fn column_count_counts_spanned_columns(#[case] row: Row, #[case] expected: usize) {
3547 let table = Table::new([row], Vec::<Constraint>::new());
3548 assert_eq!(table.column_count(), expected);
3549 }
3550
3551 #[test]
3552 fn column_count_counts_header_and_footer_spans() {
3553 let table = Table::new([Row::new(["a"])], Vec::<Constraint>::new())
3554 .header(Row::new([Cell::new("H").column_span(3)]))
3555 .footer(Row::new([Cell::new("F").column_span(2)]));
3556 assert_eq!(table.column_count(), 3);
3557 }
3558
3559 #[test]
3560 fn render_in_minimal_buffer() {
3561 let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
3562 let rows = vec![
3563 Row::new(vec!["Cell1", "Cell2", "Cell3"]),
3564 Row::new(vec!["Cell4", "Cell5", "Cell6"]),
3565 ];
3566 let table = Table::new(rows, [Constraint::Length(10); 3])
3567 .header(Row::new(vec!["Header1", "Header2", "Header3"]))
3568 .footer(Row::new(vec!["Footer1", "Footer2", "Footer3"]));
3569 Widget::render(table, buffer.area, &mut buffer);
3571 assert_eq!(buffer, Buffer::with_lines([" "]));
3572 }
3573
3574 #[test]
3575 fn render_in_zero_size_buffer() {
3576 let mut buffer = Buffer::empty(Rect::ZERO);
3577 let rows = vec![
3578 Row::new(vec!["Cell1", "Cell2", "Cell3"]),
3579 Row::new(vec!["Cell4", "Cell5", "Cell6"]),
3580 ];
3581 let table = Table::new(rows, [Constraint::Length(10); 3])
3582 .header(Row::new(vec!["Header1", "Header2", "Header3"]))
3583 .footer(Row::new(vec!["Footer1", "Footer2", "Footer3"]));
3584 Widget::render(table, buffer.area, &mut buffer);
3586 }
3587
3588 #[test]
3589 fn get_area_for_column_span_one_no_more_columns() {
3590 let columns = [];
3591 let column_span = Table::get_cell_area(&mut columns.iter(), 1, 1);
3592 assert!(column_span.is_none());
3593 }
3594
3595 #[test]
3596 fn get_area_for_column_span_two_no_more_columns() {
3597 let columns = [];
3598 let column_span = Table::get_cell_area(&mut columns.iter(), 2, 1);
3599 assert!(column_span.is_none());
3600 }
3601
3602 #[rstest]
3603 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}], 2, 5)]
3604 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}], 2, 5,)]
3605 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}], 1, 2)]
3606 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}], 3, 5)]
3607 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}], 1, 2)]
3608 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}], 2, 2)]
3609 #[case(&[
3610 Rect{x: 3, width: 2, y: 0, height: 1},
3611 Rect{x: 3, width: 2, y: 0, height: 1},
3612 Rect{x: 3, width: 2, y: 0, height: 1},
3613 Rect{x: 3, width: 2, y: 0, height: 1},
3614 ], 3, 8)]
3615 #[case(&[
3616 Rect{x: 3, width: 2, y: 0, height: 1},
3617 Rect{x: 3, width: 2, y: 0, height: 1},
3618 Rect{x: 3, width: 2, y: 0, height: 1},
3619 ], 3, 8)]
3620 fn test_colspan_width_single_column_spacing(
3621 #[case] columns: &[Rect],
3622 #[case] column_span: u16,
3623 #[case] expected_column_width: u16,
3624 ) {
3625 let column_span = Table::get_cell_area(&mut columns.iter(), column_span, 1);
3626 assert!(column_span.is_some());
3627 assert_eq!(column_span.unwrap().width, expected_column_width);
3628 }
3629
3630 #[rstest]
3631 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}, Rect{x: 3, width: 2, y: 0, height: 1}], 3, 10)]
3632 #[case(&[Rect{x: 3, width: 2, y: 0, height: 1}], 3, 2)]
3633 fn test_colspan_width_two_column_spacing(
3634 #[case] columns: &[Rect],
3635 #[case] column_span: u16,
3636 #[case] expected_column_width: u16,
3637 ) {
3638 let column_span = Table::get_cell_area(&mut columns.iter(), column_span, 2);
3639 assert!(column_span.is_some());
3640 assert_eq!(column_span.unwrap().width, expected_column_width);
3641 }
3642
3643 #[rstest]
3644 #[case(
3645 HighlightSpacing::Always,
3646 15, 1, None, [
3650 Cell::new("ABCDEFGHIJK").column_span(2),
3651 Cell::new("12345678901"),
3652 Cell::new("XYZXYZXYZXY"),
3653 ],
3654 [
3655 " ABCDEF 12 XY",
3656 " ", " ", ])]
3659 #[case(
3660 HighlightSpacing::Always,
3661 15, 1, Some(0), [
3665 Cell::new("ABCDEFGHIJK").column_span(2),
3666 Cell::new("12345678901"),
3667 Cell::new("XYZXYZXYZXY"),
3668 ],
3669 [
3670 ">>>ABCDEF 12 XY",
3671 " ", " ", ])]
3674 #[case(
3675 HighlightSpacing::WhenSelected,
3676 15, 1, None, [
3680 Cell::new("ABCDEFGHIJK").column_span(2),
3681 Cell::new("12345678901"),
3682 Cell::new("XYZXYZXYZXY"),
3683 ],
3684 [
3685 "ABCDEFG 123 XYZ",
3686 " ", " ", ])]
3689 #[case(
3690 HighlightSpacing::WhenSelected,
3691 15, 1, Some(0), [
3695 Cell::new("ABCDEFGHIJK").column_span(2),
3696 Cell::new("12345678901"),
3697 Cell::new("XYZXYZXYZXY"),
3698 ],
3699 [
3700 ">>>ABCDEF 12 XY",
3701 " ", " ", ])]
3704 fn test_table_with_selection_and_column_spans<'line, 'cell, Lines, Cells>(
3705 #[case] highlight_spacing: HighlightSpacing,
3706 #[case] columns: u16,
3707 #[case] spacing: u16,
3708 #[case] selection: Option<usize>,
3709 #[case] cells: Cells,
3710 #[case] expected: Lines,
3711 ) where
3712 Cells: IntoIterator,
3713 Cells::Item: Into<Cell<'cell>>,
3714 Lines: IntoIterator,
3715 Lines::Item: Into<Line<'line>>,
3716 {
3717 let table = Table::default()
3718 .rows(vec![Row::new(cells)])
3719 .highlight_spacing(highlight_spacing)
3720 .highlight_symbol(">>>")
3721 .column_spacing(spacing);
3722 let area = Rect::new(0, 0, columns, 3);
3723 let mut buf = Buffer::empty(area);
3724 let mut state = TableState::default().with_selected(selection);
3725 StatefulWidget::render(table, area, &mut buf, &mut state);
3726 assert_eq!(buf, Buffer::with_lines(expected));
3727 }
3728}