| 1 | use comfy_table::*; |
| 2 | use pretty_assertions::assert_eq; |
| 3 | |
| 4 | /// Cell alignment can be specified on Columns and Cells |
| 5 | /// Alignment settings on Cells overwrite the settings of Columns |
| 6 | #[test] |
| 7 | fn cell_alignment() { |
| 8 | let mut table = Table::new(); |
| 9 | table |
| 10 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 11 | .add_row(vec![ |
| 12 | "Very long line Test", |
| 13 | "Very long line Test", |
| 14 | "Very long line Test", |
| 15 | ]) |
| 16 | .add_row(vec![ |
| 17 | Cell::new("Right").set_alignment(CellAlignment::Right), |
| 18 | Cell::new("Left").set_alignment(CellAlignment::Left), |
| 19 | Cell::new("Center").set_alignment(CellAlignment::Center), |
| 20 | ]) |
| 21 | .add_row(vec!["Left", "Center", "Right"]); |
| 22 | |
| 23 | let alignment = [ |
| 24 | CellAlignment::Left, |
| 25 | CellAlignment::Center, |
| 26 | CellAlignment::Right, |
| 27 | ]; |
| 28 | |
| 29 | // Add the alignment to their respective column |
| 30 | for (column_index, column) in table.column_iter_mut().enumerate() { |
| 31 | let alignment = alignment.get(column_index).unwrap(); |
| 32 | column.set_cell_alignment(*alignment); |
| 33 | } |
| 34 | |
| 35 | println!("{table}"); |
| 36 | let expected = " |
| 37 | +---------------------+---------------------+---------------------+ |
| 38 | | Header1 | Header2 | Header3 | |
| 39 | +=================================================================+ |
| 40 | | Very long line Test | Very long line Test | Very long line Test | |
| 41 | |---------------------+---------------------+---------------------| |
| 42 | | Right | Left | Center | |
| 43 | |---------------------+---------------------+---------------------| |
| 44 | | Left | Center | Right | |
| 45 | +---------------------+---------------------+---------------------+"; |
| 46 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 47 | } |