| 1 | #[cfg(feature = "tty")] |
| 2 | use crosstermrust-analyzer cargo crossterm 0.29.0 crate/::stylerust-analyzer cargo crossterm 0.29.0 style/::{Stylizerust-analyzer cargo crossterm 0.29.0 style/stylize/Stylize#, stylerust-analyzer cargo crossterm 0.29.0 style/style().}; |
| 3 | use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#; |
| 4 | use unicode_widthrust-analyzer cargo unicode-width 0.2.2 crate/::UnicodeWidthStrrust-analyzer cargo unicode-width 0.2.2 UnicodeWidthStr#; |
| 5 | |
| 6 | use superrust-analyzer cargo comfy-table 7.2.2 utils/formatting/::content_splitrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/::{measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width()., split_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/split_line().}; |
| 7 | #[cfg(feature = "tty")] |
| 8 | use craterust-analyzer cargo comfy-table 7.2.2 crate/::stylerust-analyzer cargo comfy-table 7.2.2 style/::{map_attributerust-analyzer cargo comfy-table 7.2.2 style/styling_enums/map_attribute()., map_colorrust-analyzer cargo comfy-table 7.2.2 style/styling_enums/map_color().}; |
| 9 | use craterust-analyzer cargo comfy-table 7.2.2 crate/::{cellrust-analyzer cargo comfy-table 7.2.2 cell/::Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#, rowrust-analyzer cargo comfy-table 7.2.2 row/::Rowrust-analyzer cargo comfy-table 7.2.2 row/Row#, stylerust-analyzer cargo comfy-table 7.2.2 style/::CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#, tablerust-analyzer cargo comfy-table 7.2.2 table/::Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, utilsrust-analyzer cargo comfy-table 7.2.2 utils/::ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#}; |
| 10 | |
| 11 | pub fn delimiterrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/delimiter().(cell: &Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#, info: &ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#, table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#) -> char { |
| 12 | // Determine, which delimiter should be used |
| 13 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(delimiter) = cell.delimiterrust-analyzer cargo comfy-table 7.2.2 cell/Cell#delimiter. { |
| 14 | delimiter |
| 15 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(delimiter) = info.delimiterrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#delimiter. { |
| 16 | delimiter |
| 17 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(delimiter) = table.delimiterrust-analyzer cargo comfy-table 7.2.2 table/Table#delimiter. { |
| 18 | delimiter |
| 19 | } else { |
| 20 | ' ' |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /// Returns the formatted content of the table. |
| 25 | /// The content is organized in the following structure |
| 26 | /// |
| 27 | /// tc stands for table content and represents the returned value |
| 28 | /// ``` text |
| 29 | /// column1 column2 |
| 30 | /// row1 tc[0][0][0] tc[0][0][1] <-line1 |
| 31 | /// tc[0][1][0] tc[0][1][1] <-line2 |
| 32 | /// tc[0][2][0] tc[0][2][1] <-line3 |
| 33 | /// |
| 34 | /// row2 tc[1][0][0] tc[1][0][1] <-line1 |
| 35 | /// tc[1][1][0] tc[1][1][1] <-line2 |
| 36 | /// tc[1][2][0] tc[1][2][1] <-line3 |
| 37 | /// ``` |
| 38 | /// |
| 39 | /// The strings for each row will be padded and aligned according to their respective column. |
| 40 | pub fn format_contentrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/format_content().(table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, display_info: &[ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>> { |
| 41 | // The content of the whole table |
| 42 | let mut table_content = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(table.rowsrust-analyzer cargo comfy-table 7.2.2 table/Table#rows..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 43 | |
| 44 | // Format table header if it exists |
| 45 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(header) = table.headerrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]header().() { |
| 46 | table_content.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(format_rowrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/format_row().(header, display_info, table)); |
| 47 | } |
| 48 | |
| 49 | for row in table.rowsrust-analyzer cargo comfy-table 7.2.2 table/Table#rows..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 50 | table_content.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(format_rowrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/format_row().(row, display_info, table)); |
| 51 | } |
| 52 | table_content |
| 53 | } |
| 54 | |
| 55 | pub fn format_rowrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/format_row().( |
| 56 | row: &Rowrust-analyzer cargo comfy-table 7.2.2 row/Row#, |
| 57 | display_infos: &[ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#], |
| 58 | table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, |
| 59 | ) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> { |
| 60 | // The content of this specific row |
| 61 | let mut temp_row_content = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(display_infos.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 62 | |
| 63 | let mut cell_iter = row.cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().(); |
| 64 | // Now iterate over all cells and handle them according to their alignment |
| 65 | for info in display_infos.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 66 | if info.is_hiddenrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#is_hidden. { |
| 67 | cell_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().(); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | // Each cell is divided into several lines divided by newline |
| 72 | // Every line that's too long will be split into multiple lines |
| 73 | let mut cell_lines = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 74 | |
| 75 | // Check if the row has as many cells as the table has columns. |
| 76 | // If that's not the case, create a new cell with empty spaces. |
| 77 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cell) = cell_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().() else { |
| 78 | cell_lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(info.widthrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]width().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 79 | temp_row_content.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(cell_lines); |
| 80 | continue; |
| 81 | }; |
| 82 | |
| 83 | // The delimiter is configurable, determine which one should be used for this cell. |
| 84 | let delimiter = delimiterrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/delimiter().(cell, info, table); |
| 85 | |
| 86 | // Iterate over each line and split it into multiple lines if necessary. |
| 87 | // Newlines added by the user will be preserved. |
| 88 | for line in cell.contentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#content..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 89 | if measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(line) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().info.content_widthrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#content_width..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() { |
| 90 | let mut parts = split_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/split_line().(line, info, delimiter); |
| 91 | cell_lines.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut parts); |
| 92 | } else { |
| 93 | cell_lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(line.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Remove all unneeded lines of this cell, if the row's height is capped to a certain |
| 98 | // amount of lines and there're too many lines in this cell. |
| 99 | // This then truncates and inserts a '...' string at the end of the last line to indicate |
| 100 | // that the cell has been truncated. |
| 101 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(lines) = row.max_heightrust-analyzer cargo comfy-table 7.2.2 row/Row#max_height. |
| 102 | && cell_lines.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().lines |
| 103 | { |
| 104 | // We already have to many lines. Cut off the surplus lines. |
| 105 | let _ = cell_lines.split_offrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]split_off().(lines); |
| 106 | |
| 107 | // Directly access the last line. |
| 108 | let last_line = cell_lines |
| 109 | .get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(lines rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) |
| 110 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("We know it's this long."); |
| 111 | |
| 112 | // Truncate any ansi codes, as the following cutoff might break ansi code |
| 113 | // otherwise anyway. This could be handled smarter, but it's simple and just works. |
| 114 | #[cfg(feature = "custom_styling")] |
| 115 | { |
| 116 | let stripped = consolerust-analyzer cargo console 0.16.1 crate/::strip_ansi_codesrust-analyzer cargo console 0.16.1 ansi/strip_ansi_codes().(last_line).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 117 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().last_line = stripped; |
| 118 | } |
| 119 | |
| 120 | let max_width: usize = info.content_widthrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#content_width..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 121 | let indicator_width = table.truncation_indicatorrust-analyzer cargo comfy-table 7.2.2 table/Table#truncation_indicator..widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(); |
| 122 | |
| 123 | let mut truncate_at = 0; |
| 124 | // Start the accumulated_width with the indicator_width, which is the minimum width |
| 125 | // we may show anyway. |
| 126 | let mut accumulated_width = indicator_width; |
| 127 | let mut full_string_fits = false; |
| 128 | |
| 129 | // Leave these print statements in here in case we ever have to debug this annoying |
| 130 | // stuff again. |
| 131 | //println!("\nSTART:"); |
| 132 | //println!("\nMax width: {max_width}, Indicator width: {indicator_width}"); |
| 133 | //println!("Full line hex: {last_line}"); |
| 134 | //println!( |
| 135 | // "Full line hex: {}", |
| 136 | // last_line |
| 137 | // .as_bytes() |
| 138 | // .iter() |
| 139 | // .map(|byte| format!("{byte:02x}")) |
| 140 | // .collect::<Vec<String>>() |
| 141 | // .join(", ") |
| 142 | //); |
| 143 | |
| 144 | // Iterate through the UTF-8 graphemes. |
| 145 | // Check the `split_long_word` inline function docs to see why we're using |
| 146 | // graphemes. |
| 147 | // **Note:** The `index` here is the **byte** index. So we cannot just |
| 148 | // String::truncate afterwards. We have to convert to a byte vector to perform |
| 149 | // the truncation first. |
| 150 | let mut grapheme_iter = last_line.grapheme_indicesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]grapheme_indices().(true).peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(); |
| 151 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((index, grapheme)) = grapheme_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 152 | // Leave these print statements in here in case we ever have to debug this |
| 153 | // annoying stuff again |
| 154 | //println!( |
| 155 | // "Current index: {index}, Next grapheme: {grapheme} (width: {})", |
| 156 | // grapheme.width() |
| 157 | //); |
| 158 | //println!( |
| 159 | // "Next grapheme hex: {}", |
| 160 | // grapheme |
| 161 | // .as_bytes() |
| 162 | // .iter() |
| 163 | // .map(|byte| format!("{byte:02x}")) |
| 164 | // .collect::<Vec<String>>() |
| 165 | // .join(", ") |
| 166 | //); |
| 167 | |
| 168 | // Immediately save where to truncate in case this grapheme doesn't fit. |
| 169 | // The index is just before the current grapheme actually starts. |
| 170 | truncate_at = index; |
| 171 | // Check if the next grapheme would break the boundary of the allowed line |
| 172 | // length. |
| 173 | let new_width = accumulated_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().grapheme.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(); |
| 174 | //println!( |
| 175 | // "Next width: {new_width}/{max_width} ({accumulated_width} + {})", |
| 176 | // grapheme.width() |
| 177 | //); |
| 178 | if new_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().max_width { |
| 179 | //println!( |
| 180 | // "Breaking: {:?}", |
| 181 | // accumulated_width + grapheme.width() > max_width |
| 182 | //); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | // The grapheme seems to fit. Save the index and check the next one. |
| 187 | accumulated_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().grapheme.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(); |
| 188 | |
| 189 | // This is a special case. |
| 190 | // We reached the last char, meaning that full last line + the indicator fit. |
| 191 | if grapheme_iter.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 192 | full_string_fits = true |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Only do any truncation logic if the line doesn't fit. |
| 197 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().full_string_fits { |
| 198 | // Truncate the string at the byte index just behind the last valid grapheme |
| 199 | // and overwrite the last line with the new truncated string. |
| 200 | let mut last_line_bytes = last_line.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(); |
| 201 | last_line_bytes.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(truncate_at); |
| 202 | let new_last_line = |
| 203 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(last_line_bytes).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("We cut at an exact char boundary"); |
| 204 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().last_line = new_last_line; |
| 205 | } |
| 206 | |
| 207 | // Push the truncation indicator. |
| 208 | last_line.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&table.truncation_indicatorrust-analyzer cargo comfy-table 7.2.2 table/Table#truncation_indicator.); |
| 209 | } |
| 210 | |
| 211 | // Iterate over all generated lines of this cell and align them |
| 212 | let cell_lines = cell_lines |
| 213 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 214 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|line| align_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/align_line().(table, info, cell, line.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 215 | |
| 216 | temp_row_content.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(cell_lines.collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()); |
| 217 | } |
| 218 | |
| 219 | // Right now, we have a different structure than desired. |
| 220 | // The content is organized by `row->cell->line`. |
| 221 | // We want to remove the cell from our datastructure, since this makes the next step a lot |
| 222 | // easier. In the end it should look like this: `row->lines->column`. |
| 223 | // To achieve this, we calculate the max amount of lines for the current row. |
| 224 | // Afterwards, we iterate over each cell and convert the current structure to the desired one. |
| 225 | // This step basically transforms this: |
| 226 | // tc[0][0][0] tc[0][1][0] |
| 227 | // tc[0][0][1] tc[0][1][1] |
| 228 | // tc[0][0][2] This part of the line is missing |
| 229 | // |
| 230 | // to this: |
| 231 | // tc[0][0][0] tc[0][0][1] |
| 232 | // tc[0][1][0] tc[0][1][1] |
| 233 | // tc[0][2][0] tc[0][2][1] <- Now filled with placeholder (spaces) |
| 234 | let max_lines = temp_row_content.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().).maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#max().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0); |
| 235 | let mut row_content = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(max_lines rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().display_infos.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 236 | |
| 237 | // Each column should have `max_lines` for this row. |
| 238 | // Cells content with fewer lines will simply be topped up with empty strings. |
| 239 | for index in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#max_lines { |
| 240 | let mut line = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(display_infos.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 241 | let mut cell_iter = temp_row_content.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().(); |
| 242 | |
| 243 | for info in display_infos.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 244 | if info.is_hiddenrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#is_hidden. { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | let cell = cell_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 249 | match cell.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index) { |
| 250 | // The current cell has content for this line. Append it |
| 251 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(content) => line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(content.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().()), |
| 252 | // The current cell doesn't have content for this line. |
| 253 | // Fill with a placeholder (empty spaces) |
| 254 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(info.widthrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]width().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())), |
| 255 | } |
| 256 | } |
| 257 | row_content.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(line); |
| 258 | } |
| 259 | |
| 260 | row_content |
| 261 | } |
| 262 | |
| 263 | /// Apply the alignment for a column. Alignment can be either Left/Right/Center. |
| 264 | /// In every case all lines will be exactly the same character length `info.width - padding long` |
| 265 | /// This is needed, so we can simply insert it into the border frame later on. |
| 266 | /// Padding is applied in this function as well. |
| 267 | #[allow(unused_variables)] |
| 268 | fn align_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/align_line().(table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, info: &ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#, cell: &Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#, mut line: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 269 | let content_width = info.content_widthrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#content_width.; |
| 270 | let remaining: usize = usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u16>`]from().(content_width).saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(&line)); |
| 271 | |
| 272 | // Apply the styling before aligning the line, if the user requests it. |
| 273 | // That way non-delimiter whitespaces won't have stuff like underlines. |
| 274 | #[cfg(feature = "tty")] |
| 275 | if table.should_stylerust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]should_style().() && table.style_text_onlyrust-analyzer cargo comfy-table 7.2.2 table/Table#style_text_only. { |
| 276 | line = style_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/style_line().(line, cell); |
| 277 | } |
| 278 | |
| 279 | // Determine the alignment of the column cells. |
| 280 | // Cell settings overwrite the columns Alignment settings. |
| 281 | // Default is Left |
| 282 | let alignment = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(alignment) = cell.alignmentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#alignment. { |
| 283 | alignment |
| 284 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(alignment) = info.cell_alignmentrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#cell_alignment. { |
| 285 | alignment |
| 286 | } else { |
| 287 | CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#::Leftrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#Left# |
| 288 | }; |
| 289 | |
| 290 | // Apply left/right/both side padding depending on the alignment of the column |
| 291 | match alignment { |
| 292 | CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#::Leftrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#Left# => { |
| 293 | line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().&" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(remaining); |
| 294 | } |
| 295 | CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#::Rightrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#Right# => { |
| 296 | line = " ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(remaining) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().&line; |
| 297 | } |
| 298 | CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#::Centerrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#Center# => { |
| 299 | let left_padding = (remaining as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().2f32).ceilrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]ceil().() as usize; |
| 300 | let right_padding = (remaining as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().2f32).floorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]floor().() as usize; |
| 301 | line = " ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(left_padding) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().&line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().&" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(right_padding); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | line = pad_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/pad_line().(&line, info); |
| 306 | |
| 307 | #[cfg(feature = "tty")] |
| 308 | if table.should_stylerust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]should_style().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().table.style_text_onlyrust-analyzer cargo comfy-table 7.2.2 table/Table#style_text_only. { |
| 309 | return style_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/style_line().(line, cell); |
| 310 | } |
| 311 | |
| 312 | line |
| 313 | } |
| 314 | |
| 315 | /// Apply the column's padding to this line |
| 316 | fn pad_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/pad_line().(line: &str, info: &ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 317 | let mut padded_line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 318 | |
| 319 | padded_line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().&" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(info.paddingrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#padding..0.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 320 | padded_line rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign().+=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign().line; |
| 321 | padded_line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().&" ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(info.paddingrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#padding..1.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 322 | |
| 323 | padded_line |
| 324 | } |
| 325 | |
| 326 | #[cfg(feature = "tty")] |
| 327 | fn style_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_format/style_line().(line: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, cell: &Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 328 | // Just return the line, if there's no need to style. |
| 329 | if cell.fgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#fg..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() && cell.bgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#bg..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() && cell.attributesrust-analyzer cargo comfy-table 7.2.2 cell/Cell#attributes..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 330 | return line; |
| 331 | } |
| 332 | |
| 333 | let mut content = stylerust-analyzer cargo crossterm 0.29.0 style/style().(line); |
| 334 | |
| 335 | // Apply text color |
| 336 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color) = cell.fgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#fg. { |
| 337 | content = content.withrust-analyzer cargo crossterm 0.29.0 style/stylize/Stylize#with().(map_colorrust-analyzer cargo comfy-table 7.2.2 style/styling_enums/map_color().(color)); |
| 338 | } |
| 339 | |
| 340 | // Apply background color |
| 341 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color) = cell.bgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#bg. { |
| 342 | content = content.onrust-analyzer cargo crossterm 0.29.0 style/stylize/Stylize#on().(map_colorrust-analyzer cargo comfy-table 7.2.2 style/styling_enums/map_color().(color)); |
| 343 | } |
| 344 | |
| 345 | for attribute in cell.attributesrust-analyzer cargo comfy-table 7.2.2 cell/Cell#attributes..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 346 | content = content.attributerust-analyzer cargo crossterm 0.29.0 style/stylize/Stylize#attribute().(map_attributerust-analyzer cargo comfy-table 7.2.2 style/styling_enums/map_attribute().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().attribute)); |
| 347 | } |
| 348 | |
| 349 | content.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 350 | } |