| 1 | use comfy_table::{ColumnConstraint::*, ContentArrangement, Row, Table, Width::*}; |
| 2 | use pretty_assertions::assert_eq; |
| 3 | |
| 4 | use crate::all::assert_table_line_width; |
| 5 | |
| 6 | /// Individual rows can be configured to have a max height. |
| 7 | /// Everything beyond that line height should be truncated. |
| 8 | #[test] |
| 9 | fn table_with_truncate() { |
| 10 | let mut table = Table::new(); |
| 11 | let mut first_row: Row = Row::from(vec![ |
| 12 | "This is a very long line with a lot of text", |
| 13 | "This is anotherverylongtextwithlongwords text", |
| 14 | "smol", |
| 15 | ]); |
| 16 | first_row.max_height(4); |
| 17 | |
| 18 | let mut second_row = Row::from(vec![ |
| 19 | "Now let's\nadd a really long line in the middle of the cell \n and add more multi line stuff", |
| 20 | "This is another text", |
| 21 | "smol", |
| 22 | ]); |
| 23 | second_row.max_height(4); |
| 24 | |
| 25 | table |
| 26 | .set_header(vec!["Header1", "Header2", "Head"]) |
| 27 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 28 | .set_width(35) |
| 29 | .add_row(first_row) |
| 30 | .add_row(second_row); |
| 31 | |
| 32 | // The first column will be wider than 6 chars. |
| 33 | // The second column's content is wider than 6 chars. There should be a '...'. |
| 34 | let second_column = table.column_mut(1).unwrap(); |
| 35 | second_column.set_constraint(Absolute(Fixed(8))); |
| 36 | |
| 37 | // The third column's content is less than 6 chars width. There shouldn't be a '...'. |
| 38 | let third_column = table.column_mut(2).unwrap(); |
| 39 | third_column.set_constraint(Absolute(Fixed(7))); |
| 40 | |
| 41 | println!("{table}"); |
| 42 | let expected = " |
| 43 | +----------------+--------+-------+ |
| 44 | | Header1 | Header | Head | |
| 45 | | | 2 | | |
| 46 | +=================================+ |
| 47 | | This is a very | This | smol | |
| 48 | | long line with | is ano | | |
| 49 | | a lot of text | therve | | |
| 50 | | | ryl... | | |
| 51 | |----------------+--------+-------| |
| 52 | | Now let's | This | smol | |
| 53 | | add a really | is ano | | |
| 54 | | long line in | ther | | |
| 55 | | the middle ... | text | | |
| 56 | +----------------+--------+-------+"; |
| 57 | println!("{expected}"); |
| 58 | assert_table_line_width(&table, 35); |
| 59 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn table_with_truncate_indicator() { |
| 64 | let mut table = Table::new(); |
| 65 | let mut first_row: Row = Row::from(vec![ |
| 66 | "This is a very long line with a lot of text", |
| 67 | "This is anotherverylongtextwithlongwords text", |
| 68 | "smol", |
| 69 | ]); |
| 70 | first_row.max_height(4); |
| 71 | |
| 72 | let mut second_row = Row::from(vec![ |
| 73 | "Now let's\nadd a really long line in the middle of the cell \n and add more multi line stuff", |
| 74 | "This is another text", |
| 75 | "smol", |
| 76 | ]); |
| 77 | second_row.max_height(4); |
| 78 | |
| 79 | table |
| 80 | .set_header(vec!["Header1", "Header2", "Head"]) |
| 81 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 82 | .set_truncation_indicator("…") |
| 83 | .set_width(35) |
| 84 | .add_row(first_row) |
| 85 | .add_row(second_row); |
| 86 | |
| 87 | // The first column will be wider than 6 chars. |
| 88 | // The second column's content is wider than 6 chars. There should be a '…'. |
| 89 | let second_column = table.column_mut(1).unwrap(); |
| 90 | second_column.set_constraint(Absolute(Fixed(8))); |
| 91 | |
| 92 | // The third column's content is less than 6 chars width. There shouldn't be a '…'. |
| 93 | let third_column = table.column_mut(2).unwrap(); |
| 94 | third_column.set_constraint(Absolute(Fixed(7))); |
| 95 | |
| 96 | println!("{table}"); |
| 97 | let expected = " |
| 98 | +----------------+--------+-------+ |
| 99 | | Header1 | Header | Head | |
| 100 | | | 2 | | |
| 101 | +=================================+ |
| 102 | | This is a very | This | smol | |
| 103 | | long line with | is ano | | |
| 104 | | a lot of text | therve | | |
| 105 | | | rylon… | | |
| 106 | |----------------+--------+-------| |
| 107 | | Now let's | This | smol | |
| 108 | | add a really | is ano | | |
| 109 | | long line in | ther | | |
| 110 | | the middle of… | text | | |
| 111 | +----------------+--------+-------+"; |
| 112 | println!("{expected}"); |
| 113 | assert_table_line_width(&table, 35); |
| 114 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn table_with_composite_utf8_strings() { |
| 119 | let mut table = Table::new(); |
| 120 | |
| 121 | table |
| 122 | .set_header(vec!["Header1"]) |
| 123 | .set_width(20) |
| 124 | .add_row(vec!["あいうえおかきくけこさしすせそたちつてと"]) |
| 125 | .set_content_arrangement(comfy_table::ContentArrangement::Dynamic); |
| 126 | |
| 127 | for row in table.row_iter_mut() { |
| 128 | row.max_height(1); // 2 -> also panics, 3 -> ok |
| 129 | } |
| 130 | |
| 131 | println!("{table}"); |
| 132 | let expected = " |
| 133 | +------------------+ |
| 134 | | Header1 | |
| 135 | +==================+ |
| 136 | | あいうえおか... | |
| 137 | +------------------+"; |
| 138 | println!("{expected}"); |
| 139 | assert_table_line_width(&table, 20); |
| 140 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn table_with_composite_utf8_strings_2_lines() { |
| 145 | let mut table = Table::new(); |
| 146 | |
| 147 | table |
| 148 | .set_header(vec!["Header1"]) |
| 149 | .set_width(20) |
| 150 | .add_row(vec!["あいうえおかきくけこさしすせそたちつてと"]) |
| 151 | .set_content_arrangement(comfy_table::ContentArrangement::Dynamic); |
| 152 | |
| 153 | for row in table.row_iter_mut() { |
| 154 | row.max_height(2); |
| 155 | } |
| 156 | |
| 157 | println!("{table}"); |
| 158 | let expected = " |
| 159 | +------------------+ |
| 160 | | Header1 | |
| 161 | +==================+ |
| 162 | | あいうえおかきく | |
| 163 | | けこさしすせ... | |
| 164 | +------------------+"; |
| 165 | println!("{expected}"); |
| 166 | assert_table_line_width(&table, 20); |
| 167 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn table_with_composite_utf8_emojis() { |
| 172 | let mut table = Table::new(); |
| 173 | |
| 174 | table |
| 175 | .set_header(vec!["Header1"]) |
| 176 | .set_width(15) |
| 177 | .add_row(vec![ |
| 178 | "🙂↕️🙂↕️🙂↕️🙂↕️🙂↕️🙂↕️last_line.into_bytes().truncate(truncate_at)", |
| 179 | ]) |
| 180 | .set_content_arrangement(comfy_table::ContentArrangement::Dynamic); |
| 181 | |
| 182 | for row in table.row_iter_mut() { |
| 183 | row.max_height(1); // 2 -> also panics, 3 -> ok |
| 184 | } |
| 185 | |
| 186 | println!("{table}"); |
| 187 | let expected = " |
| 188 | +-------------+ |
| 189 | | Header1 | |
| 190 | +=============+ |
| 191 | | 🙂↕️🙂↕️🙂↕️🙂↕️... | |
| 192 | +-------------+"; |
| 193 | println!("{expected}"); |
| 194 | assert_table_line_width(&table, 15); |
| 195 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 196 | } |