| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
use makeover_layout::{CellPart, Column, Priority, Sort, Width}; |
| 46 |
use ratatui::layout::Constraint; |
| 47 |
use ratatui::style::{Modifier, Style}; |
| 48 |
use ratatui::text::Line; |
| 49 |
use ratatui::widgets::{Cell as TrackCell, Row, Table}; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
const CUTOFFS: [Priority; 3] = [Priority::Optional, Priority::Secondary, Priority::Essential]; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
#[derive(Debug, Clone, Copy, Default)] |
| 69 |
pub struct Sizing<'a> { |
| 70 |
|
| 71 |
|
| 72 |
pub lengths: &'a [(&'a str, u16)], |
| 73 |
|
| 74 |
pub fallback: u16, |
| 75 |
} |
| 76 |
|
| 77 |
impl Sizing<'_> { |
| 78 |
|
| 79 |
fn length_for(&self, name: &str) -> u16 { |
| 80 |
self.lengths |
| 81 |
.iter() |
| 82 |
.find(|(column, _)| *column == name) |
| 83 |
.map_or(self.fallback, |(_, length)| *length) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#[derive(Debug, Clone)] |
| 93 |
pub struct Cell<'a> { |
| 94 |
|
| 95 |
pub column: &'a str, |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
pub part: Option<CellPart>, |
| 103 |
|
| 104 |
pub content: Line<'a>, |
| 105 |
} |
| 106 |
|
| 107 |
impl<'a> Cell<'a> { |
| 108 |
|
| 109 |
#[must_use] |
| 110 |
pub fn new(column: &'a str, content: impl Into<Line<'a>>) -> Self { |
| 111 |
Self { |
| 112 |
column, |
| 113 |
part: None, |
| 114 |
content: content.into(), |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
#[must_use] |
| 120 |
pub fn part(mut self, part: CellPart) -> Self { |
| 121 |
self.part = Some(part); |
| 122 |
self |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 138 |
pub struct TableStyle { |
| 139 |
|
| 140 |
pub header: Style, |
| 141 |
|
| 142 |
pub sorted: Style, |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
pub sortable: Style, |
| 151 |
|
| 152 |
pub value: Style, |
| 153 |
|
| 154 |
|
| 155 |
pub tokens: Style, |
| 156 |
|
| 157 |
pub actions: Style, |
| 158 |
|
| 159 |
pub link: Style, |
| 160 |
|
| 161 |
|
| 162 |
pub selected: Style, |
| 163 |
|
| 164 |
|
| 165 |
pub column_spacing: u16, |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
pub ascending: &'static str, |
| 178 |
|
| 179 |
pub descending: &'static str, |
| 180 |
} |
| 181 |
|
| 182 |
impl Default for TableStyle { |
| 183 |
fn default() -> Self { |
| 184 |
Self { |
| 185 |
header: Style::new().add_modifier(Modifier::BOLD), |
| 186 |
sorted: Style::new().add_modifier(Modifier::BOLD), |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
sortable: Style::new(), |
| 193 |
value: Style::new(), |
| 194 |
tokens: Style::new(), |
| 195 |
actions: Style::new(), |
| 196 |
link: Style::new().add_modifier(Modifier::UNDERLINED), |
| 197 |
selected: Style::new().add_modifier(Modifier::REVERSED), |
| 198 |
column_spacing: 1, |
| 199 |
ascending: Sort::Ascending.glyph(), |
| 200 |
descending: Sort::Descending.glyph(), |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl TableStyle { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
#[cfg(feature = "theme")] |
| 219 |
#[must_use] |
| 220 |
pub fn from_theme(theme: &crate::Theme) -> Self { |
| 221 |
Self { |
| 222 |
header: Style::new() |
| 223 |
.fg(theme.content_muted) |
| 224 |
.add_modifier(Modifier::BOLD), |
| 225 |
sorted: Style::new() |
| 226 |
.fg(theme.content_primary) |
| 227 |
.add_modifier(Modifier::BOLD), |
| 228 |
sortable: Style::new().fg(theme.content_secondary), |
| 229 |
value: Style::new().fg(theme.content_primary), |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
tokens: Style::new().fg(theme.content_secondary), |
| 234 |
actions: Style::new().fg(theme.action_primary), |
| 235 |
link: Style::new() |
| 236 |
.fg(theme.action_primary) |
| 237 |
.add_modifier(Modifier::UNDERLINED), |
| 238 |
selected: Style::new() |
| 239 |
.bg(theme.surface_raised) |
| 240 |
.add_modifier(Modifier::BOLD), |
| 241 |
column_spacing: 1, |
| 242 |
ascending: Sort::Ascending.glyph(), |
| 243 |
descending: Sort::Descending.glyph(), |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#[must_use] |
| 254 |
pub fn for_part(&self, part: Option<CellPart>) -> Style { |
| 255 |
match part { |
| 256 |
Some(CellPart::Tokens) => self.tokens, |
| 257 |
Some(CellPart::Actions) => self.actions, |
| 258 |
Some(CellPart::Link) => self.link, |
| 259 |
_ => self.value, |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
fn heading<'a>(column: &Column<'a>, style: &TableStyle) -> Line<'a> { |
| 279 |
let caret = match column.sorted { |
| 280 |
Some(Sort::Ascending) => style.ascending, |
| 281 |
Some(Sort::Descending) => style.descending, |
| 282 |
None if column.sortable => style.ascending, |
| 283 |
None => return Line::from(column.name), |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
Line::from(format!("{} {caret}", column.name)) |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
fn min_width<'a, R>(column: &Column<'a>, rows: &[R], sizing: &Sizing<'_>, style: &TableStyle) -> u16 |
| 296 |
where |
| 297 |
R: AsRef<[Cell<'a>]>, |
| 298 |
{ |
| 299 |
match column.width { |
| 300 |
Width::Content => measure(column, rows, style), |
| 301 |
Width::Fixed => sizing.length_for(column.name), |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
_ => sizing.length_for(column.name), |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
fn measure<'a, R>(column: &Column<'a>, rows: &[R], style: &TableStyle) -> u16 |
| 317 |
where |
| 318 |
R: AsRef<[Cell<'a>]>, |
| 319 |
{ |
| 320 |
let widest = rows |
| 321 |
.iter() |
| 322 |
.filter_map(|row| { |
| 323 |
row.as_ref() |
| 324 |
.iter() |
| 325 |
.find(|cell| cell.column == column.name) |
| 326 |
.map(|cell| cell.content.width()) |
| 327 |
}) |
| 328 |
.max() |
| 329 |
.unwrap_or(0); |
| 330 |
u16::try_from(widest.max(heading(column, style).width())).unwrap_or(u16::MAX) |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
fn fits<'a, R>( |
| 335 |
columns: &[Column<'a>], |
| 336 |
rows: &[R], |
| 337 |
sizing: &Sizing<'_>, |
| 338 |
style: &TableStyle, |
| 339 |
cutoff: Priority, |
| 340 |
width: u16, |
| 341 |
) -> bool |
| 342 |
where |
| 343 |
R: AsRef<[Cell<'a>]>, |
| 344 |
{ |
| 345 |
let kept: Vec<&Column<'a>> = columns.iter().filter(|c| c.kept_at(cutoff)).collect(); |
| 346 |
let gaps = u32::from(style.column_spacing) * (kept.len().saturating_sub(1)) as u32; |
| 347 |
let tracks: u32 = kept |
| 348 |
.iter() |
| 349 |
.map(|c| u32::from(min_width(c, rows, sizing, style))) |
| 350 |
.sum(); |
| 351 |
tracks + gaps <= u32::from(width) |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
#[must_use] |
| 361 |
pub fn cutoff_for<'a, R>( |
| 362 |
columns: &[Column<'a>], |
| 363 |
rows: &[R], |
| 364 |
sizing: &Sizing<'_>, |
| 365 |
style: &TableStyle, |
| 366 |
width: u16, |
| 367 |
) -> Priority |
| 368 |
where |
| 369 |
R: AsRef<[Cell<'a>]>, |
| 370 |
{ |
| 371 |
for cutoff in CUTOFFS { |
| 372 |
if fits(columns, rows, sizing, style, cutoff, width) { |
| 373 |
return cutoff; |
| 374 |
} |
| 375 |
} |
| 376 |
Priority::Essential |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#[must_use] |
| 386 |
pub fn constraints<'a, R>( |
| 387 |
columns: &[Column<'a>], |
| 388 |
rows: &[R], |
| 389 |
sizing: &Sizing<'_>, |
| 390 |
style: &TableStyle, |
| 391 |
cutoff: Priority, |
| 392 |
) -> Vec<Constraint> |
| 393 |
where |
| 394 |
R: AsRef<[Cell<'a>]>, |
| 395 |
{ |
| 396 |
columns |
| 397 |
.iter() |
| 398 |
.filter(|column| column.kept_at(cutoff)) |
| 399 |
.map(|column| match column.width { |
| 400 |
|
| 401 |
|
| 402 |
Width::Content => Constraint::Length(measure(column, rows, style)), |
| 403 |
Width::Fixed => Constraint::Length(sizing.length_for(column.name)), |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
_ => Constraint::Min(sizing.length_for(column.name)), |
| 409 |
}) |
| 410 |
.collect() |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
#[must_use] |
| 422 |
pub fn row<'a>( |
| 423 |
columns: &[Column<'a>], |
| 424 |
cells: &[Cell<'a>], |
| 425 |
style: &TableStyle, |
| 426 |
cutoff: Priority, |
| 427 |
) -> Row<'a> { |
| 428 |
Row::new( |
| 429 |
columns |
| 430 |
.iter() |
| 431 |
.filter(|column| column.kept_at(cutoff)) |
| 432 |
.map(|column| { |
| 433 |
let found = cells.iter().find(|cell| cell.column == column.name); |
| 434 |
let part = found.and_then(|cell| cell.part); |
| 435 |
let content = found.map_or_else(Line::default, |cell| cell.content.clone()); |
| 436 |
TrackCell::from(content).style(style.for_part(part)) |
| 437 |
}) |
| 438 |
.collect::<Vec<_>>(), |
| 439 |
) |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
#[must_use] |
| 449 |
pub fn header<'a>(columns: &[Column<'a>], style: &TableStyle, cutoff: Priority) -> Row<'a> { |
| 450 |
Row::new( |
| 451 |
columns |
| 452 |
.iter() |
| 453 |
.filter(|column| column.kept_at(cutoff)) |
| 454 |
.map(|column| { |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
let tone = match (column.sorted, column.sortable) { |
| 460 |
(Some(_), _) => style.sorted, |
| 461 |
(None, true) => style.sortable, |
| 462 |
(None, false) => style.header, |
| 463 |
}; |
| 464 |
TrackCell::from(heading(column, style)).style(tone) |
| 465 |
}) |
| 466 |
.collect::<Vec<_>>(), |
| 467 |
) |
| 468 |
.style(style.header) |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
#[must_use] |
| 484 |
pub fn table<'a, R>( |
| 485 |
columns: &[Column<'a>], |
| 486 |
rows: &[R], |
| 487 |
sizing: &Sizing<'_>, |
| 488 |
style: &TableStyle, |
| 489 |
width: u16, |
| 490 |
) -> Table<'a> |
| 491 |
where |
| 492 |
R: AsRef<[Cell<'a>]>, |
| 493 |
{ |
| 494 |
let cutoff = cutoff_for(columns, rows, sizing, style, width); |
| 495 |
let widths = constraints(columns, rows, sizing, style, cutoff); |
| 496 |
let body: Vec<Row<'a>> = rows |
| 497 |
.iter() |
| 498 |
.map(|cells| row(columns, cells.as_ref(), style, cutoff)) |
| 499 |
.collect(); |
| 500 |
|
| 501 |
Table::new(body, widths) |
| 502 |
.header(header(columns, style, cutoff)) |
| 503 |
.column_spacing(style.column_spacing) |
| 504 |
.row_highlight_style(style.selected) |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
#[must_use] |
| 513 |
pub fn overflows<'a, R>( |
| 514 |
columns: &[Column<'a>], |
| 515 |
rows: &[R], |
| 516 |
sizing: &Sizing<'_>, |
| 517 |
style: &TableStyle, |
| 518 |
width: u16, |
| 519 |
) -> bool |
| 520 |
where |
| 521 |
R: AsRef<[Cell<'a>]>, |
| 522 |
{ |
| 523 |
!fits(columns, rows, sizing, style, Priority::Essential, width) |
| 524 |
} |
| 525 |
|
| 526 |
#[cfg(test)] |
| 527 |
mod tests { |
| 528 |
use super::*; |
| 529 |
|
| 530 |
fn columns() -> Vec<Column<'static>> { |
| 531 |
vec![ |
| 532 |
Column { |
| 533 |
name: "name", |
| 534 |
width: Width::Fill, |
| 535 |
priority: Priority::Essential, |
| 536 |
sortable: true, |
| 537 |
sorted: Some(Sort::Ascending), |
| 538 |
}, |
| 539 |
Column { |
| 540 |
name: "size", |
| 541 |
width: Width::Fixed, |
| 542 |
priority: Priority::Secondary, |
| 543 |
sortable: true, |
| 544 |
sorted: None, |
| 545 |
}, |
| 546 |
Column { |
| 547 |
name: "note", |
| 548 |
width: Width::Content, |
| 549 |
priority: Priority::Optional, |
| 550 |
sortable: false, |
| 551 |
sorted: None, |
| 552 |
}, |
| 553 |
] |
| 554 |
} |
| 555 |
|
| 556 |
fn sizing() -> Sizing<'static> { |
| 557 |
Sizing { |
| 558 |
lengths: &[("name", 10), ("size", 6)], |
| 559 |
fallback: 4, |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
fn rows() -> Vec<Vec<Cell<'static>>> { |
| 564 |
vec![ |
| 565 |
vec![ |
| 566 |
Cell::new("name", "alpha"), |
| 567 |
Cell::new("size", "1kb"), |
| 568 |
Cell::new("note", "a longer note"), |
| 569 |
], |
| 570 |
vec![Cell::new("name", "beta"), Cell::new("size", "2kb")], |
| 571 |
] |
| 572 |
} |
| 573 |
|
| 574 |
fn cell_text(row: &Row<'_>) -> Vec<String> { |
| 575 |
|
| 576 |
|
| 577 |
use ratatui::layout::Rect; |
| 578 |
use ratatui::widgets::Widget; |
| 579 |
let mut buf = ratatui::buffer::Buffer::empty(Rect::new(0, 0, 60, 1)); |
| 580 |
Table::new(vec![row.clone()], [Constraint::Length(18); 3]) |
| 581 |
.column_spacing(1) |
| 582 |
.render(Rect::new(0, 0, 60, 1), &mut buf); |
| 583 |
(0..3) |
| 584 |
.map(|i| { |
| 585 |
let start = i * 19; |
| 586 |
(start..start + 18) |
| 587 |
.map(|x| buf[(x as u16, 0)].symbol()) |
| 588 |
.collect::<String>() |
| 589 |
.trim_end() |
| 590 |
.to_owned() |
| 591 |
}) |
| 592 |
.collect() |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
fn cell_colors(row: &Row<'_>) -> Vec<Option<ratatui::style::Color>> { |
| 600 |
use ratatui::layout::Rect; |
| 601 |
use ratatui::widgets::Widget; |
| 602 |
let mut buf = ratatui::buffer::Buffer::empty(Rect::new(0, 0, 60, 1)); |
| 603 |
Table::new(vec![row.clone()], [Constraint::Length(18); 3]) |
| 604 |
.column_spacing(1) |
| 605 |
.render(Rect::new(0, 0, 60, 1), &mut buf); |
| 606 |
(0..3).map(|i| buf[(i * 19, 0)].fg).map(Some).collect() |
| 607 |
} |
| 608 |
|
| 609 |
#[test] |
| 610 |
fn cells_are_ordered_by_the_columns_and_not_by_the_row() { |
| 611 |
|
| 612 |
|
| 613 |
let cols = columns(); |
| 614 |
let out_of_order = vec![ |
| 615 |
Cell::new("note", "third"), |
| 616 |
Cell::new("name", "first"), |
| 617 |
Cell::new("size", "second"), |
| 618 |
]; |
| 619 |
let drawn = row( |
| 620 |
&cols, |
| 621 |
&out_of_order, |
| 622 |
&TableStyle::default(), |
| 623 |
Priority::Optional, |
| 624 |
); |
| 625 |
assert_eq!(cell_text(&drawn), vec!["first", "second", "third"]); |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn a_cell_naming_no_column_is_dropped_and_a_column_with_no_cell_keeps_its_place() { |
| 630 |
let cols = columns(); |
| 631 |
let cells = vec![Cell::new("note", "kept"), Cell::new("nonesuch", "lost")]; |
| 632 |
let drawn = row(&cols, &cells, &TableStyle::default(), Priority::Optional); |
| 633 |
|
| 634 |
|
| 635 |
assert_eq!(cell_text(&drawn), vec!["", "", "kept"]); |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn a_content_column_is_measured_from_its_widest_cell() { |
| 640 |
let style = TableStyle::default(); |
| 641 |
let widths = constraints(&columns(), &rows(), &sizing(), &style, Priority::Optional); |
| 642 |
assert_eq!(widths[2], Constraint::Length("a longer note".len() as u16)); |
| 643 |
} |
| 644 |
|
| 645 |
#[test] |
| 646 |
fn a_content_column_never_truncates_its_own_heading() { |
| 647 |
|
| 648 |
|
| 649 |
let cols = vec![Column { |
| 650 |
name: "duration", |
| 651 |
width: Width::Content, |
| 652 |
priority: Priority::Essential, |
| 653 |
sortable: false, |
| 654 |
sorted: None, |
| 655 |
}]; |
| 656 |
let rows = vec![vec![Cell::new("duration", "3s")]]; |
| 657 |
let widths = constraints( |
| 658 |
&cols, |
| 659 |
&rows, |
| 660 |
&sizing(), |
| 661 |
&TableStyle::default(), |
| 662 |
Priority::Optional, |
| 663 |
); |
| 664 |
assert_eq!(widths[0], Constraint::Length(8)); |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
fn a_caret_is_part_of_what_a_heading_costs() { |
| 669 |
|
| 670 |
|
| 671 |
let cols = vec![Column { |
| 672 |
name: "size", |
| 673 |
width: Width::Content, |
| 674 |
priority: Priority::Essential, |
| 675 |
sortable: true, |
| 676 |
sorted: Some(Sort::Descending), |
| 677 |
}]; |
| 678 |
let rows: Vec<Vec<Cell<'_>>> = vec![]; |
| 679 |
let style = TableStyle::default(); |
| 680 |
let widths = constraints(&cols, &rows, &sizing(), &style, Priority::Optional); |
| 681 |
assert_eq!( |
| 682 |
widths[0], |
| 683 |
Constraint::Length(6), |
| 684 |
"size plus a space and a caret" |
| 685 |
); |
| 686 |
} |
| 687 |
|
| 688 |
#[test] |
| 689 |
fn narrowing_drops_the_optional_column_first_and_the_essential_one_never() { |
| 690 |
let style = TableStyle::default(); |
| 691 |
let (cols, rows, sz) = (columns(), rows(), sizing()); |
| 692 |
|
| 693 |
assert_eq!( |
| 694 |
cutoff_for(&cols, &rows, &sz, &style, 40), |
| 695 |
Priority::Optional |
| 696 |
); |
| 697 |
|
| 698 |
assert_eq!( |
| 699 |
cutoff_for(&cols, &rows, &sz, &style, 20), |
| 700 |
Priority::Secondary |
| 701 |
); |
| 702 |
|
| 703 |
assert_eq!( |
| 704 |
cutoff_for(&cols, &rows, &sz, &style, 12), |
| 705 |
Priority::Essential |
| 706 |
); |
| 707 |
|
| 708 |
assert_eq!( |
| 709 |
cutoff_for(&cols, &rows, &sz, &style, 2), |
| 710 |
Priority::Essential |
| 711 |
); |
| 712 |
assert!(overflows(&cols, &rows, &sz, &style, 2)); |
| 713 |
assert!(!overflows(&cols, &rows, &sz, &style, 12)); |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn a_column_inserted_left_of_the_cut_does_not_change_what_drops() { |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
let dropped = |cols: &[Column<'_>], cutoff| -> Vec<String> { |
| 728 |
cols.iter() |
| 729 |
.filter(|c| !c.kept_at(cutoff)) |
| 730 |
.map(|c| c.name.to_owned()) |
| 731 |
.collect() |
| 732 |
}; |
| 733 |
let before = columns(); |
| 734 |
let mut after = vec![Column { |
| 735 |
name: "mark", |
| 736 |
width: Width::Fixed, |
| 737 |
priority: Priority::Essential, |
| 738 |
sortable: false, |
| 739 |
sorted: None, |
| 740 |
}]; |
| 741 |
after.extend(columns()); |
| 742 |
|
| 743 |
for cutoff in CUTOFFS { |
| 744 |
assert_eq!( |
| 745 |
dropped(&before, cutoff), |
| 746 |
dropped(&after, cutoff), |
| 747 |
"inserting a column changed what {cutoff:?} drops" |
| 748 |
); |
| 749 |
} |
| 750 |
assert_eq!(dropped(&before, Priority::Secondary), vec!["note"]); |
| 751 |
} |
| 752 |
|
| 753 |
#[test] |
| 754 |
fn a_column_never_outlives_a_more_essential_one() { |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
let style = TableStyle::default(); |
| 759 |
let (cols, rows, sz) = (columns(), rows(), sizing()); |
| 760 |
for width in 0..48u16 { |
| 761 |
let cutoff = cutoff_for(&cols, &rows, &sz, &style, width); |
| 762 |
let kept: Vec<&str> = cols |
| 763 |
.iter() |
| 764 |
.filter(|c| c.kept_at(cutoff)) |
| 765 |
.map(|c| c.name) |
| 766 |
.collect(); |
| 767 |
assert!( |
| 768 |
kept.contains(&"name"), |
| 769 |
"the essential column left at {width}" |
| 770 |
); |
| 771 |
if kept.contains(&"note") { |
| 772 |
assert!( |
| 773 |
kept.contains(&"size"), |
| 774 |
"optional outlived secondary at {width}" |
| 775 |
); |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
#[test] |
| 781 |
fn a_dropped_column_takes_its_track_with_it() { |
| 782 |
|
| 783 |
|
| 784 |
let style = TableStyle::default(); |
| 785 |
let widths = constraints(&columns(), &rows(), &sizing(), &style, Priority::Secondary); |
| 786 |
assert_eq!(widths.len(), 2); |
| 787 |
let drawn = row(&columns(), &rows()[0], &style, Priority::Secondary); |
| 788 |
assert_eq!(cell_text(&drawn), vec!["alpha", "1kb", ""]); |
| 789 |
} |
| 790 |
|
| 791 |
#[test] |
| 792 |
fn a_fill_column_keeps_its_floor_while_taking_the_slack() { |
| 793 |
|
| 794 |
|
| 795 |
let style = TableStyle::default(); |
| 796 |
let widths = constraints(&columns(), &rows(), &sizing(), &style, Priority::Optional); |
| 797 |
assert_eq!(widths[0], Constraint::Min(10)); |
| 798 |
assert_eq!(widths[1], Constraint::Length(6)); |
| 799 |
} |
| 800 |
|
| 801 |
#[test] |
| 802 |
fn a_column_with_no_length_of_its_own_takes_the_fallback() { |
| 803 |
let cols = vec![Column { |
| 804 |
name: "unlisted", |
| 805 |
width: Width::Fixed, |
| 806 |
priority: Priority::Essential, |
| 807 |
sortable: false, |
| 808 |
sorted: None, |
| 809 |
}]; |
| 810 |
let rows: Vec<Vec<Cell<'_>>> = vec![]; |
| 811 |
let widths = constraints( |
| 812 |
&cols, |
| 813 |
&rows, |
| 814 |
&sizing(), |
| 815 |
&TableStyle::default(), |
| 816 |
Priority::Optional, |
| 817 |
); |
| 818 |
assert_eq!(widths[0], Constraint::Length(4)); |
| 819 |
} |
| 820 |
|
| 821 |
#[test] |
| 822 |
fn a_heading_carries_a_caret_when_it_is_ordered_by_or_offers_to_be() { |
| 823 |
let style = TableStyle::default(); |
| 824 |
let head = header(&columns(), &style, Priority::Optional); |
| 825 |
assert_eq!( |
| 826 |
cell_text(&head), |
| 827 |
vec!["name \u{25B2}", "size \u{25B2}", "note"], |
| 828 |
"in force and offering both carry one; not a control carries none" |
| 829 |
); |
| 830 |
} |
| 831 |
|
| 832 |
#[test] |
| 833 |
fn the_three_states_of_a_heading_are_three_tones() { |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
use ratatui::style::Color; |
| 839 |
let style = TableStyle { |
| 840 |
sorted: Style::new().fg(Color::Red), |
| 841 |
sortable: Style::new().fg(Color::Green), |
| 842 |
header: Style::new().fg(Color::Blue), |
| 843 |
..TableStyle::default() |
| 844 |
}; |
| 845 |
let drawn = cell_colors(&header(&columns(), &style, Priority::Optional)); |
| 846 |
assert_eq!( |
| 847 |
drawn, |
| 848 |
vec![Some(Color::Red), Some(Color::Green), Some(Color::Blue)] |
| 849 |
); |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
let house = TableStyle::default(); |
| 857 |
let plain = cell_colors(&header(&columns(), &house, Priority::Optional)); |
| 858 |
assert_eq!(plain[0], plain[1], "no colour to spend, so none is claimed"); |
| 859 |
} |
| 860 |
|
| 861 |
#[test] |
| 862 |
fn pressing_a_heading_does_not_move_the_columns_after_it() { |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
let style = TableStyle::default(); |
| 867 |
let offering = Column { |
| 868 |
name: "size", |
| 869 |
width: Width::Content, |
| 870 |
priority: Priority::Secondary, |
| 871 |
sortable: true, |
| 872 |
sorted: None, |
| 873 |
}; |
| 874 |
let in_force = Column { |
| 875 |
sorted: Some(Sort::Descending), |
| 876 |
..offering |
| 877 |
}; |
| 878 |
let rows: Vec<Vec<Cell<'_>>> = vec![]; |
| 879 |
assert_eq!( |
| 880 |
measure(&offering, &rows, &style), |
| 881 |
measure(&in_force, &rows, &style) |
| 882 |
); |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
let inert = Column { |
| 887 |
sortable: false, |
| 888 |
..offering |
| 889 |
}; |
| 890 |
assert!(measure(&inert, &rows, &style) < measure(&offering, &rows, &style)); |
| 891 |
} |
| 892 |
|
| 893 |
#[test] |
| 894 |
fn a_column_sorted_without_being_sortable_still_draws_its_caret() { |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
let cols = vec![Column { |
| 899 |
name: "rank", |
| 900 |
width: Width::Content, |
| 901 |
priority: Priority::Essential, |
| 902 |
sortable: false, |
| 903 |
sorted: Some(Sort::Descending), |
| 904 |
}]; |
| 905 |
let head = header(&cols, &TableStyle::default(), Priority::Optional); |
| 906 |
assert_eq!(cell_text(&head), vec!["rank \u{25BC}", "", ""]); |
| 907 |
} |
| 908 |
|
| 909 |
#[test] |
| 910 |
fn the_parts_a_cell_can_be_are_styled_apart() { |
| 911 |
|
| 912 |
|
| 913 |
let style = TableStyle::default(); |
| 914 |
assert_eq!(style.for_part(Some(CellPart::Value)), style.value); |
| 915 |
assert_eq!(style.for_part(Some(CellPart::Tokens)), style.tokens); |
| 916 |
assert_eq!(style.for_part(Some(CellPart::Actions)), style.actions); |
| 917 |
assert_eq!(style.for_part(Some(CellPart::Link)), style.link); |
| 918 |
assert_ne!(style.for_part(Some(CellPart::Link)), style.value); |
| 919 |
|
| 920 |
assert_eq!(style.for_part(None), style.value); |
| 921 |
} |
| 922 |
|
| 923 |
#[test] |
| 924 |
fn a_table_narrows_itself_from_the_width_it_is_given() { |
| 925 |
|
| 926 |
let style = TableStyle::default(); |
| 927 |
let wide = table(&columns(), &rows(), &sizing(), &style, 40); |
| 928 |
let narrow = table(&columns(), &rows(), &sizing(), &style, 20); |
| 929 |
use ratatui::layout::Rect; |
| 930 |
use ratatui::widgets::Widget; |
| 931 |
|
| 932 |
let mut buf = ratatui::buffer::Buffer::empty(Rect::new(0, 0, 40, 3)); |
| 933 |
wide.render(Rect::new(0, 0, 40, 3), &mut buf); |
| 934 |
let head: String = (0..40).map(|x| buf[(x, 0)].symbol()).collect(); |
| 935 |
assert!(head.contains("note")); |
| 936 |
|
| 937 |
let mut buf = ratatui::buffer::Buffer::empty(Rect::new(0, 0, 20, 3)); |
| 938 |
narrow.render(Rect::new(0, 0, 20, 3), &mut buf); |
| 939 |
let head: String = (0..20).map(|x| buf[(x, 0)].symbol()).collect(); |
| 940 |
assert!(!head.contains("note"), "the optional column is gone"); |
| 941 |
assert!(head.contains("name"), "the essential one is not"); |
| 942 |
} |
| 943 |
|
| 944 |
#[test] |
| 945 |
fn selection_is_carried_by_the_background_alone() { |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
let style = TableStyle::default(); |
| 951 |
assert!(style.selected.fg.is_none()); |
| 952 |
} |
| 953 |
} |
| 954 |
|