.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
src/
ansi/
aligned.rs
2.3 KiB
ansi.rs
2.3 KiB
app.rs
4.2 KiB
backend.rs
3.9 KiB
border.rs
6.5 KiB
buffer.rs
20.6 KiB
cell.rs
7.2 KiB
colors.rs
4.2 KiB
event.rs
3.4 KiB
frame.rs
3.7 KiB
hstack.rs
6.5 KiB
label.rs
5.5 KiB
layout.rs
11.2 KiB
lib.rs
2.4 KiB
primitive.rs
19.4 KiB
table.rs
23.5 KiB
terminal.rs
5.2 KiB
text.rs
3.0 KiB
textarea.rs
6.4 KiB
vstack.rs
16.4 KiB
widget.rs
11.0 KiB
Cargo.toml
298 B
README.md
521 B
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-core/src/table.rs
raw
| 1 | //! Column-aligned table widget. |
| 2 | //! |
| 3 | //! `Table` tracks per-column widths as rows are pushed, then renders rows as |
| 4 | //! styled [`Line`](crate::Line) values or directly as a widget. It is intended |
| 5 | //! for compact operational views where stable alignment matters more than |
| 6 | //! complex cell layout. |
| 7 | //! |
| 8 | //! Example: |
| 9 | //! ``` |
| 10 | //! use rustik_tui_core::{Constraint, Table, TableOptions, Widget}; |
| 11 | //! |
| 12 | //! let mut t = Table::new(TableOptions::default()); |
| 13 | //! t.push(["pest", "biological control"]); |
| 14 | //! t.push(["aphid", "lacewing"]); |
| 15 | //! t.push(["spider mite", "ladybug"]); |
| 16 | //! |
| 17 | //! let out = t |
| 18 | //! .display(Constraint::UNBOUNDED) |
| 19 | //! .lines() |
| 20 | //! .map(str::trim_end) |
| 21 | //! .collect::<Vec<_>>() |
| 22 | //! .join("\n") |
| 23 | //! + "\n"; |
| 24 | //! assert_eq!( |
| 25 | //! out, |
| 26 | //! "pest biological control\naphid lacewing\nspider mite ladybug\n", |
| 27 | //! ); |
| 28 | //! # Ok::<(), Box<dyn std::error::Error>>(()) |
| 29 | //! ``` |
| 30 | //! Output: |
| 31 | //! ``` plain |
| 32 | //! pest biological control |
| 33 | //! aphid lacewing |
| 34 | //! spider mite ladybug |
| 35 | //! ``` |
| 36 | use std::{fmt, num::NonZeroUsize}; |
| 37 | |
| 38 | use super::cell::Text; |
| 39 | use super::{Border, Buffer, Constraint, Line, Size, Style, Widget, colors}; |
| 40 | |
| 41 | /// Rendering options for a table. |
| 42 | #[derive(Debug)] |
| 43 | pub struct TableOptions { |
| 44 | /// Whether the table should be allowed to overflow. |
| 45 | pub overflow: bool, |
| 46 | /// Horizontal spacing between table cells. |
| 47 | pub spacing: usize, |
| 48 | /// Table border. |
| 49 | pub border: Option<Border>, |
| 50 | } |
| 51 | |
| 52 | impl Default for TableOptions { |
| 53 | fn default() -> Self { |
| 54 | Self { |
| 55 | overflow: false, |
| 56 | spacing: 1, |
| 57 | border: None, |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl TableOptions { |
| 63 | /// Returns default options with a faint border enabled. |
| 64 | pub fn bordered() -> Self { |
| 65 | Self { |
| 66 | border: Some(Border::new(colors::FAINT)), |
| 67 | spacing: 3, |
| 68 | ..Self::default() |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Responsive behavior for one table column. |
| 74 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 75 | pub struct Column { |
| 76 | /// Relative share of horizontal space assigned to this column. |
| 77 | flex: usize, |
| 78 | /// Order in which this column disappears as space contracts. |
| 79 | collapse: Option<usize>, |
| 80 | } |
| 81 | |
| 82 | impl Column { |
| 83 | /// Returns a column that uses its intrinsic content width. |
| 84 | pub const fn auto() -> Self { |
| 85 | Self { |
| 86 | flex: 0, |
| 87 | collapse: None, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Returns a column that receives this relative share of available width. |
| 92 | pub const fn flex(flex: usize) -> Self { |
| 93 | Self { |
| 94 | flex, |
| 95 | collapse: None, |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Returns an intrinsic-width column that disappears at `order`. |
| 100 | pub const fn collapse(order: usize) -> Self { |
| 101 | Self { |
| 102 | flex: 0, |
| 103 | collapse: Some(order), |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// Internal row representation for table rendering. |
| 109 | #[derive(Debug)] |
| 110 | enum Row<const W: usize, T> { |
| 111 | /// Header row. |
| 112 | Header([T; W]), |
| 113 | /// Data row. |
| 114 | Data { |
| 115 | /// Row cells. |
| 116 | cells: [T; W], |
| 117 | /// Optional row-wide style applied to spacing and trailing pad cells. |
| 118 | style: Option<Style>, |
| 119 | }, |
| 120 | /// Horizontal divider row. |
| 121 | Divider { |
| 122 | /// Horizontal line sigil used for this divider. |
| 123 | sigil: char, |
| 124 | }, |
| 125 | } |
| 126 | |
| 127 | /// Fixed-width table with `W` columns. |
| 128 | #[derive(Debug)] |
| 129 | pub struct Table<const W: usize, T> { |
| 130 | /// Rows in insertion order. |
| 131 | rows: Vec<Row<W, T>>, |
| 132 | /// Maximum content width for each column. |
| 133 | widths: [usize; W], |
| 134 | /// Rendering options. |
| 135 | opts: TableOptions, |
| 136 | /// Responsive behavior for each column. |
| 137 | layout: [Column; W], |
| 138 | } |
| 139 | |
| 140 | impl<const W: usize, T> Default for Table<W, T> { |
| 141 | fn default() -> Self { |
| 142 | Self { |
| 143 | rows: Vec::new(), |
| 144 | widths: [0; W], |
| 145 | opts: TableOptions::default(), |
| 146 | layout: [Column::auto(); W], |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | impl<const W: usize, T: Text> FromIterator<[T; W]> for Table<W, T> { |
| 152 | fn from_iter<I: IntoIterator<Item = [T; W]>>(iter: I) -> Self { |
| 153 | let mut table = Self::default(); |
| 154 | table.extend(iter); |
| 155 | table |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | impl<const W: usize, T: Text + fmt::Debug + Send> Widget for Table<W, T> |
| 160 | where |
| 161 | T::Padded: Into<Line>, |
| 162 | { |
| 163 | fn size(&self, parent: Constraint) -> Size { |
| 164 | Table::size(self, parent) |
| 165 | } |
| 166 | |
| 167 | fn render(&mut self, area: crate::Rect, buf: &mut Buffer) { |
| 168 | let size = area.size(); |
| 169 | Line::renders(self.lines(Constraint::new(size, size)), area, buf); |
| 170 | } |
| 171 | |
| 172 | fn lines(&mut self, parent: Constraint) -> Vec<Line> { |
| 173 | Table::lines(self, parent) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | impl<const W: usize, T: Text> Table<W, T> |
| 178 | where |
| 179 | T::Padded: Into<Line>, |
| 180 | { |
| 181 | /// Returns the table as styled render lines under a parent constraint. |
| 182 | pub fn lines(&self, parent: Constraint) -> Vec<Line> { |
| 183 | let mut lines = Vec::new(); |
| 184 | |
| 185 | // Don't print empty tables. |
| 186 | if self.is_empty() { |
| 187 | return lines; |
| 188 | } |
| 189 | |
| 190 | let border = self.opts.border; |
| 191 | let inner = self.inner(parent); |
| 192 | let cols = inner.cols; |
| 193 | let max_rows = parent.max.rows; |
| 194 | |
| 195 | if let Some(border) = border { |
| 196 | lines.push(border.top(cols)); |
| 197 | } |
| 198 | |
| 199 | for row in &self.rows { |
| 200 | if lines.len() == max_rows { |
| 201 | break; |
| 202 | } |
| 203 | match row { |
| 204 | Row::Header(cells) => lines.push(self.cells_line(cells, None, border, cols)), |
| 205 | Row::Data { cells, style } => { |
| 206 | lines.push(self.cells_line(cells, *style, border, cols)); |
| 207 | } |
| 208 | Row::Divider { sigil } => { |
| 209 | if let Some(border) = border { |
| 210 | lines.push(border.divider(cols, *sigil)); |
| 211 | } else { |
| 212 | lines.push(Line::default()); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | if let Some(border) = border |
| 218 | && lines.len() < max_rows |
| 219 | { |
| 220 | lines.push(border.bottom(cols)); |
| 221 | } |
| 222 | lines |
| 223 | } |
| 224 | |
| 225 | /// Renders one header or data row as a styled line. |
| 226 | /// |
| 227 | /// When `style` is `Some`, the row-wide style is applied to the whole line |
| 228 | /// so trailing pad cells inherit the background. When `None`, each cell |
| 229 | /// keeps its own background (the header / unstyled data path). |
| 230 | fn cells_line( |
| 231 | &self, |
| 232 | cells: &[T; W], |
| 233 | style: Option<Style>, |
| 234 | border: Option<Border>, |
| 235 | cols: usize, |
| 236 | ) -> Line { |
| 237 | let mut line = Line::default(); |
| 238 | if let Some(border) = border { |
| 239 | line.push(border.left(1)); |
| 240 | } |
| 241 | if self.layout == [Column::auto(); W] { |
| 242 | for (i, cell) in cells.iter().enumerate() { |
| 243 | let pad = if i + 1 == W { |
| 244 | 0 |
| 245 | } else { |
| 246 | self.widths[i] + self.opts.spacing |
| 247 | }; |
| 248 | let cell_style = style.unwrap_or_else(|| Style::default().bg(cell.background())); |
| 249 | line = line.extend(cell.pad(pad).into().style(cell_style)); |
| 250 | } |
| 251 | } else { |
| 252 | let (widths, visible) = self.column_widths(cols); |
| 253 | let last = visible.iter().rposition(|visible| *visible); |
| 254 | for (i, cell) in cells.iter().enumerate() { |
| 255 | if !visible[i] { |
| 256 | continue; |
| 257 | } |
| 258 | let spacing = usize::from(Some(i) != last) * self.opts.spacing; |
| 259 | let cell_style = style.unwrap_or_else(|| Style::default().bg(cell.background())); |
| 260 | let mut cell: Line = cell.pad(widths[i]).into(); |
| 261 | Line::truncate(&mut cell, widths[i], "…"); |
| 262 | Line::pad(&mut cell, widths[i] + spacing); |
| 263 | line = line.extend(cell.style(cell_style)); |
| 264 | } |
| 265 | } |
| 266 | Line::pad(&mut line, cols); |
| 267 | Line::truncate(&mut line, cols, "…"); |
| 268 | if let Some(row_style) = style { |
| 269 | line = line.style(row_style); |
| 270 | } |
| 271 | if let Some(border) = border { |
| 272 | line.push(border.right(1)); |
| 273 | } |
| 274 | line |
| 275 | } |
| 276 | |
| 277 | /// Returns rendered widths and visibility under `cols`. |
| 278 | fn column_widths(&self, cols: usize) -> ([usize; W], [bool; W]) { |
| 279 | let mut widths = self.widths; |
| 280 | let mut visible = [true; W]; |
| 281 | let mut visible_count = W; |
| 282 | let mut natural = |
| 283 | widths.iter().sum::<usize>() + visible_count.saturating_sub(1) * self.opts.spacing; |
| 284 | |
| 285 | while natural > cols { |
| 286 | let Some((index, _)) = self |
| 287 | .layout |
| 288 | .iter() |
| 289 | .enumerate() |
| 290 | .filter(|(index, column)| visible[*index] && column.collapse.is_some()) |
| 291 | .min_by_key(|(index, column)| (column.collapse, *index)) |
| 292 | else { |
| 293 | break; |
| 294 | }; |
| 295 | visible[index] = false; |
| 296 | natural = natural.saturating_sub(widths[index]); |
| 297 | visible_count -= 1; |
| 298 | if visible_count > 0 { |
| 299 | natural = natural.saturating_sub(self.opts.spacing); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | let spacing = visible_count.saturating_sub(1) * self.opts.spacing; |
| 304 | let available = cols.saturating_sub(spacing); |
| 305 | let fixed = self |
| 306 | .layout |
| 307 | .iter() |
| 308 | .enumerate() |
| 309 | .filter(|(index, column)| visible[*index] && column.flex == 0) |
| 310 | .map(|(index, _)| widths[index]) |
| 311 | .sum::<usize>(); |
| 312 | let flex = self |
| 313 | .layout |
| 314 | .iter() |
| 315 | .enumerate() |
| 316 | .filter(|(index, _)| visible[*index]) |
| 317 | .map(|(_, column)| column.flex) |
| 318 | .sum::<usize>(); |
| 319 | let Some(flex) = NonZeroUsize::new(flex) else { |
| 320 | return (widths, visible); |
| 321 | }; |
| 322 | let available = available.saturating_sub(fixed); |
| 323 | let mut assigned = 0; |
| 324 | for (index, column) in self.layout.iter().enumerate() { |
| 325 | if !visible[index] || column.flex == 0 { |
| 326 | continue; |
| 327 | } |
| 328 | let width = available.saturating_mul(column.flex) / flex.get(); |
| 329 | widths[index] = width; |
| 330 | assigned += width; |
| 331 | } |
| 332 | if let Some(index) = self |
| 333 | .layout |
| 334 | .iter() |
| 335 | .enumerate() |
| 336 | .rfind(|(index, column)| visible[*index] && column.flex > 0) |
| 337 | .map(|(index, _)| index) |
| 338 | { |
| 339 | widths[index] += available - assigned; |
| 340 | } |
| 341 | (widths, visible) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | impl<const W: usize, T: Text> Table<W, T> { |
| 346 | /// Creates an empty table with explicit options. |
| 347 | pub fn new(opts: TableOptions) -> Self { |
| 348 | Self { |
| 349 | rows: Vec::new(), |
| 350 | widths: [0; W], |
| 351 | opts, |
| 352 | layout: [Column::auto(); W], |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /// Returns the table with replacement options. |
| 357 | pub fn with_opts(mut self, opts: TableOptions) -> Self { |
| 358 | self.opts = opts; |
| 359 | self |
| 360 | } |
| 361 | /// Returns the table with responsive column behavior. |
| 362 | pub fn with_layout(mut self, layout: [Column; W]) -> Self { |
| 363 | self.layout = layout; |
| 364 | self |
| 365 | } |
| 366 | |
| 367 | /// Adds a data row and returns the table. |
| 368 | pub fn row(mut self, row: [T; W]) -> Self { |
| 369 | self.push(row); |
| 370 | self |
| 371 | } |
| 372 | |
| 373 | /// Returns the rendered table size under a parent constraint. |
| 374 | pub fn size(&self, parent: Constraint) -> Size { |
| 375 | if self.is_empty() { |
| 376 | return Size::MIN; |
| 377 | } |
| 378 | self.outer(parent) |
| 379 | } |
| 380 | |
| 381 | /// Adds a divider row. |
| 382 | pub fn divider(&mut self) { |
| 383 | self.divider_with_sigil('─'); |
| 384 | } |
| 385 | |
| 386 | /// Adds a divider row with an explicit line sigil. |
| 387 | pub fn divider_with_sigil(&mut self, sigil: char) { |
| 388 | self.rows.push(Row::Divider { sigil }); |
| 389 | } |
| 390 | |
| 391 | /// Adds a data row. |
| 392 | pub fn push(&mut self, row: [T; W]) { |
| 393 | for (i, cell) in row.iter().enumerate() { |
| 394 | self.widths[i] = self.widths[i].max(cell.width()); |
| 395 | } |
| 396 | self.rows.push(Row::Data { |
| 397 | cells: row, |
| 398 | style: None, |
| 399 | }); |
| 400 | } |
| 401 | |
| 402 | /// Adds a data row with a row-wide style applied to spacing and trailing pad cells. |
| 403 | pub fn push_styled(&mut self, row: [T; W], style: Style) { |
| 404 | for (i, cell) in row.iter().enumerate() { |
| 405 | self.widths[i] = self.widths[i].max(cell.width()); |
| 406 | } |
| 407 | self.rows.push(Row::Data { |
| 408 | cells: row, |
| 409 | style: Some(style), |
| 410 | }); |
| 411 | } |
| 412 | |
| 413 | /// Adds a header row. |
| 414 | pub fn header(&mut self, row: [T; W]) { |
| 415 | for (i, cell) in row.iter().enumerate() { |
| 416 | self.widths[i] = self.widths[i].max(cell.width()); |
| 417 | } |
| 418 | self.rows.push(Row::Header(row)); |
| 419 | } |
| 420 | |
| 421 | /// Returns whether the table contains no data rows. |
| 422 | pub fn is_empty(&self) -> bool { |
| 423 | !self.rows.iter().any(|r| matches!(r, Row::Data { .. })) |
| 424 | } |
| 425 | |
| 426 | /// Returns the table size inside any border. |
| 427 | fn inner(&self, c: Constraint) -> Size { |
| 428 | let mut outer = self.outer(c); |
| 429 | |
| 430 | if self.opts.border.is_some() { |
| 431 | outer.cols = outer.cols.saturating_sub(2); |
| 432 | outer.rows = outer.rows.saturating_sub(2); |
| 433 | } |
| 434 | outer |
| 435 | } |
| 436 | |
| 437 | /// Returns the table size including any border. |
| 438 | fn outer(&self, c: Constraint) -> Size { |
| 439 | let mut cols = self.widths.iter().sum::<usize>() + (W - 1) * self.opts.spacing; |
| 440 | let mut rows = self.rows.len(); |
| 441 | let padding = 2; |
| 442 | |
| 443 | // Account for outer borders. |
| 444 | if self.opts.border.is_some() { |
| 445 | cols += 2 + padding; |
| 446 | rows += 2; |
| 447 | } |
| 448 | Size::new(cols, rows).constrain(c) |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | impl<const W: usize, T: Text> Extend<[T; W]> for Table<W, T> { |
| 453 | fn extend<I>(&mut self, iter: I) |
| 454 | where |
| 455 | I: IntoIterator<Item = [T; W]>, |
| 456 | { |
| 457 | for row in iter { |
| 458 | self.push(row); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | #[cfg(test)] |
| 464 | mod tests { |
| 465 | use crate::Color; |
| 466 | |
| 467 | use crate::Widget; |
| 468 | |
| 469 | use super::*; |
| 470 | |
| 471 | #[test] |
| 472 | fn test_table() { |
| 473 | let mut t = Table::new(TableOptions::default()); |
| 474 | |
| 475 | t.push(["pineapple", "rosemary"]); |
| 476 | t.push(["apples", "pears"]); |
| 477 | |
| 478 | #[rustfmt::skip] |
| 479 | assert_eq!( |
| 480 | t.display(Constraint::UNBOUNDED), |
| 481 | [ |
| 482 | "pineapple rosemary\n", |
| 483 | "apples pears \n" |
| 484 | ].join("") |
| 485 | ); |
| 486 | } |
| 487 | |
| 488 | #[test] |
| 489 | fn test_table_border() { |
| 490 | let mut t = Table::new(TableOptions { |
| 491 | border: Some(Border::new(Color::Unset)), |
| 492 | spacing: 3, |
| 493 | ..TableOptions::default() |
| 494 | }); |
| 495 | |
| 496 | t.push(["Country", "Population", "Code"]); |
| 497 | t.divider(); |
| 498 | t.push(["France", "60M", "FR"]); |
| 499 | t.push(["Switzerland", "7M", "CH"]); |
| 500 | t.push(["Germany", "80M", "DE"]); |
| 501 | |
| 502 | let inner = t.inner(Constraint::UNBOUNDED); |
| 503 | assert_eq!(inner.cols, 33); |
| 504 | assert_eq!(inner.rows, 5); |
| 505 | |
| 506 | let outer = t.outer(Constraint::UNBOUNDED); |
| 507 | assert_eq!(outer.cols, 35); |
| 508 | assert_eq!(outer.rows, 7); |
| 509 | |
| 510 | assert_eq!( |
| 511 | t.display(Constraint::UNBOUNDED), |
| 512 | r" |
| 513 | ╭─────────────────────────────────╮ |
| 514 | │ Country Population Code │ |
| 515 | ├─────────────────────────────────┤ |
| 516 | │ France 60M FR │ |
| 517 | │ Switzerland 7M CH │ |
| 518 | │ Germany 80M DE │ |
| 519 | ╰─────────────────────────────────╯ |
| 520 | " |
| 521 | .trim_start() |
| 522 | ); |
| 523 | } |
| 524 | |
| 525 | #[test] |
| 526 | fn test_table_border_truncated() { |
| 527 | let mut t = Table::new(TableOptions { |
| 528 | border: Some(Border::new(Color::Unset)), |
| 529 | spacing: 3, |
| 530 | ..TableOptions::default() |
| 531 | }); |
| 532 | |
| 533 | t.push(["Code", "Name"]); |
| 534 | t.divider(); |
| 535 | t.push(["FR", "France"]); |
| 536 | t.push(["CH", "Switzerland"]); |
| 537 | t.push(["DE", "Germany"]); |
| 538 | |
| 539 | let constrain = Constraint::max(Size { |
| 540 | cols: 19, |
| 541 | rows: usize::MAX, |
| 542 | }); |
| 543 | let outer = t.outer(constrain); |
| 544 | assert_eq!(outer.cols, 19); |
| 545 | assert_eq!(outer.rows, 7); |
| 546 | |
| 547 | let inner = t.inner(constrain); |
| 548 | assert_eq!(inner.cols, 17); |
| 549 | assert_eq!(inner.rows, 5); |
| 550 | |
| 551 | assert_eq!( |
| 552 | t.display(constrain), |
| 553 | r" |
| 554 | ╭─────────────────╮ |
| 555 | │ Code Name │ |
| 556 | ├─────────────────┤ |
| 557 | │ FR France │ |
| 558 | │ CH Switzer… │ |
| 559 | │ DE Germany │ |
| 560 | ╰─────────────────╯ |
| 561 | " |
| 562 | .trim_start() |
| 563 | ); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn test_table_border_maximized() { |
| 568 | let mut t = Table::new(TableOptions { |
| 569 | border: Some(Border::new(Color::Unset)), |
| 570 | spacing: 3, |
| 571 | ..TableOptions::default() |
| 572 | }); |
| 573 | |
| 574 | t.push(["Code", "Name"]); |
| 575 | t.divider(); |
| 576 | t.push(["FR", "France"]); |
| 577 | t.push(["CH", "Switzerland"]); |
| 578 | t.push(["DE", "Germany"]); |
| 579 | |
| 580 | let constrain = Constraint::new( |
| 581 | Size { cols: 26, rows: 0 }, |
| 582 | Size { |
| 583 | cols: 26, |
| 584 | rows: usize::MAX, |
| 585 | }, |
| 586 | ); |
| 587 | let outer = t.outer(constrain); |
| 588 | assert_eq!(outer.cols, 26); |
| 589 | assert_eq!(outer.rows, 7); |
| 590 | |
| 591 | let inner = t.inner(constrain); |
| 592 | assert_eq!(inner.cols, 24); |
| 593 | assert_eq!(inner.rows, 5); |
| 594 | |
| 595 | assert_eq!( |
| 596 | t.display(constrain), |
| 597 | r" |
| 598 | ╭────────────────────────╮ |
| 599 | │ Code Name │ |
| 600 | ├────────────────────────┤ |
| 601 | │ FR France │ |
| 602 | │ CH Switzerland │ |
| 603 | │ DE Germany │ |
| 604 | ╰────────────────────────╯ |
| 605 | " |
| 606 | .trim_start() |
| 607 | ); |
| 608 | } |
| 609 | |
| 610 | #[test] |
| 611 | fn test_table_truncate() { |
| 612 | let mut t = Table::default(); |
| 613 | let constrain = Constraint::new( |
| 614 | Size::MIN, |
| 615 | Size { |
| 616 | cols: 16, |
| 617 | rows: usize::MAX, |
| 618 | }, |
| 619 | ); |
| 620 | |
| 621 | t.push(["pineapple", "rosemary"]); |
| 622 | t.push(["apples", "pears"]); |
| 623 | |
| 624 | #[rustfmt::skip] |
| 625 | assert_eq!( |
| 626 | t.display(constrain), |
| 627 | [ |
| 628 | "pineapple rosem…\n", |
| 629 | "apples pears \n" |
| 630 | ].join("") |
| 631 | ); |
| 632 | } |
| 633 | |
| 634 | #[test] |
| 635 | fn test_table_unicode() { |
| 636 | let mut t = Table::new(TableOptions::default()); |
| 637 | |
| 638 | t.push(["🍍pineapple", "__rosemary", "__sage"]); |
| 639 | t.push(["__pears", "🍎apples", "🍌bananas"]); |
| 640 | |
| 641 | #[rustfmt::skip] |
| 642 | assert_eq!( |
| 643 | t.display(Constraint::UNBOUNDED), |
| 644 | [ |
| 645 | "🍍pineapple __rosemary __sage \n", |
| 646 | "__pears 🍎apples 🍌bananas\n" |
| 647 | ].join("") |
| 648 | ); |
| 649 | } |
| 650 | |
| 651 | #[test] |
| 652 | fn test_table_unicode_truncate() { |
| 653 | let mut t = Table::new(TableOptions { |
| 654 | ..TableOptions::default() |
| 655 | }); |
| 656 | let constrain = Constraint::max(Size { |
| 657 | cols: 16, |
| 658 | rows: usize::MAX, |
| 659 | }); |
| 660 | t.push(["🍍pineapple", "__rosemary"]); |
| 661 | t.push(["__pears", "🍎apples"]); |
| 662 | |
| 663 | #[rustfmt::skip] |
| 664 | assert_eq!( |
| 665 | t.display(constrain), |
| 666 | [ |
| 667 | "🍍pineapple __r…\n", |
| 668 | "__pears 🍎a…\n" |
| 669 | ].join("") |
| 670 | ); |
| 671 | } |
| 672 | |
| 673 | #[test] |
| 674 | fn empty_table_renders_no_lines_even_with_divider_or_header() { |
| 675 | let mut t = Table::new(TableOptions::bordered()); |
| 676 | |
| 677 | t.header(["Name", "Value"]); |
| 678 | t.divider(); |
| 679 | |
| 680 | assert!(t.is_empty()); |
| 681 | assert_eq!(t.display(Constraint::UNBOUNDED), ""); |
| 682 | } |
| 683 | |
| 684 | #[test] |
| 685 | fn header_rows_render_once_data_exists() { |
| 686 | let mut t = Table::new(TableOptions::default()); |
| 687 | |
| 688 | t.header(["Name", "Value"]); |
| 689 | t.divider(); |
| 690 | t.push(["status", "ready"]); |
| 691 | |
| 692 | assert!(!t.is_empty()); |
| 693 | assert_eq!( |
| 694 | t.display(Constraint::UNBOUNDED), |
| 695 | "Name Value\n \nstatus ready\n" |
| 696 | ); |
| 697 | } |
| 698 | |
| 699 | #[test] |
| 700 | fn from_iterator_tracks_column_widths() { |
| 701 | let mut t = [["a", "beta"], ["alpha", "b"]] |
| 702 | .into_iter() |
| 703 | .collect::<Table<2, _>>(); |
| 704 | |
| 705 | assert_eq!(t.display(Constraint::UNBOUNDED), "a beta\nalpha b \n"); |
| 706 | } |
| 707 | |
| 708 | #[test] |
| 709 | fn responsive_columns_flex_and_collapse_in_order() { |
| 710 | let mut table = Table::new(TableOptions::default()).with_layout([ |
| 711 | Column::auto(), |
| 712 | Column::flex(1), |
| 713 | Column::collapse(2), |
| 714 | Column::collapse(1), |
| 715 | Column::auto(), |
| 716 | Column::collapse(3), |
| 717 | ]); |
| 718 | table.push(["id", "summarywide", "alice", "branch", "progress", "stats"]); |
| 719 | |
| 720 | let branch_hidden = table |
| 721 | .lines(Constraint::tight(35)) |
| 722 | .pop() |
| 723 | .unwrap() |
| 724 | .to_string(); |
| 725 | let author_hidden = table |
| 726 | .lines(Constraint::tight(34)) |
| 727 | .pop() |
| 728 | .unwrap() |
| 729 | .to_string(); |
| 730 | let stats_hidden = table |
| 731 | .lines(Constraint::tight(20)) |
| 732 | .pop() |
| 733 | .unwrap() |
| 734 | .to_string(); |
| 735 | |
| 736 | assert!(!branch_hidden.contains("branch")); |
| 737 | assert!(branch_hidden.contains("alice")); |
| 738 | assert!(!author_hidden.contains("alice")); |
| 739 | assert!(author_hidden.contains("stats")); |
| 740 | assert_eq!(branch_hidden, "id summarywide alice progress stats"); |
| 741 | assert_eq!(author_hidden, "id summarywide progress stats"); |
| 742 | assert_eq!(stats_hidden, "id summary… progress"); |
| 743 | } |
| 744 | |
| 745 | #[test] |
| 746 | fn extend_appends_data_rows_and_updates_widths() { |
| 747 | let mut t = Table::default(); |
| 748 | |
| 749 | t.push(["a", "b"]); |
| 750 | t.extend([["long", "value"]]); |
| 751 | |
| 752 | assert_eq!(t.display(Constraint::UNBOUNDED), "a b \nlong value\n"); |
| 753 | } |
| 754 | |
| 755 | #[test] |
| 756 | fn with_opts_replaces_rendering_options() { |
| 757 | let mut t = Table::default().with_opts(TableOptions { |
| 758 | border: Some(Border::new(Color::Unset)), |
| 759 | spacing: 3, |
| 760 | ..TableOptions::default() |
| 761 | }); |
| 762 | |
| 763 | t.push(["A", "B"]); |
| 764 | |
| 765 | assert_eq!( |
| 766 | t.display(Constraint::UNBOUNDED), |
| 767 | r" |
| 768 | ╭───────╮ |
| 769 | │ A B │ |
| 770 | ╰───────╯ |
| 771 | " |
| 772 | .trim_start() |
| 773 | ); |
| 774 | } |
| 775 | |
| 776 | #[test] |
| 777 | fn border_options_use_faint_border_and_wide_spacing() { |
| 778 | let opts = TableOptions::bordered(); |
| 779 | |
| 780 | assert_eq!(opts.border, Some(Border::new(colors::FAINT))); |
| 781 | assert_eq!(opts.spacing, 3); |
| 782 | assert!(!opts.overflow); |
| 783 | } |
| 784 | |
| 785 | #[test] |
| 786 | fn push_styled_applies_row_background_to_padding() { |
| 787 | let mut t = Table::<2, _>::new(TableOptions { |
| 788 | spacing: 2, |
| 789 | ..TableOptions::default() |
| 790 | }); |
| 791 | |
| 792 | t.push_styled(["a", "b"], Style::new(Color::Unset).bg(Color::Red)); |
| 793 | let lines = t.lines(Constraint::tight(8)); |
| 794 | |
| 795 | assert_eq!(lines.len(), 1); |
| 796 | let line = &lines[0]; |
| 797 | assert_eq!(line.to_string(), "a b "); |
| 798 | assert!( |
| 799 | line.labels() |
| 800 | .iter() |
| 801 | .all(|label| label.paint.style().bg_color() == Color::Red), |
| 802 | "all labels (including spacing/padding) take the row background", |
| 803 | ); |
| 804 | } |
| 805 | |
| 806 | #[test] |
| 807 | fn divider_sigil_options_replace_horizontal_lines() { |
| 808 | let mut t = Table::new(TableOptions { |
| 809 | border: Some(Border::new(Color::Unset)), |
| 810 | spacing: 3, |
| 811 | ..TableOptions::default() |
| 812 | }); |
| 813 | |
| 814 | t.push(["Code", "Name"]); |
| 815 | t.divider_with_sigil('─'); |
| 816 | t.push(["FR", "France"]); |
| 817 | |
| 818 | assert_eq!( |
| 819 | t.display(Constraint::UNBOUNDED), |
| 820 | r" |
| 821 | ╭───────────────╮ |
| 822 | │ Code Name │ |
| 823 | ├───────────────┤ |
| 824 | │ FR France │ |
| 825 | ╰───────────────╯ |
| 826 | " |
| 827 | .trim_start() |
| 828 | ); |
| 829 | } |
| 830 | } |