MCPcopy Create free account
hub / github.com/p-ranav/tabulate / print_table

Method print_table

include/tabulate/table_internal.hpp:196–289  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

194}
195
196inline void Printer::print_table(std::ostream &stream, TableInternal &table) {
197 size_t num_rows = table.size();
198 size_t num_columns = table.estimate_num_columns();
199 auto dimensions = compute_cell_dimensions(table);
200 auto row_heights = dimensions.first;
201 auto column_widths = dimensions.second;
202 auto splitted_cells_text = std::vector<std::vector<std::vector<std::string>>>(
203 num_rows, std::vector<std::vector<std::string>>(num_columns, std::vector<std::string>{}));
204
205 // Pre-compute the cells' content and split them into lines before actually
206 // iterating the cells.
207 for (size_t i = 0; i < num_rows; ++i) {
208 Row row = table[i];
209 for (size_t j = 0; j < num_columns; ++j) {
210 Cell cell = row.cell(j);
211 const std::string &text = cell.get_text();
212 auto padding_left = *cell.format().padding_left_;
213 auto padding_right = *cell.format().padding_right_;
214
215 // Check if input text has embedded \n that are to be respected
216 bool has_new_line = text.find_first_of('\n') != std::string::npos;
217
218 if (has_new_line) {
219 // Respect to the embedded '\n' characters
220 splitted_cells_text[i][j] = Format::split_lines(
221 text, "\n", cell.locale(), cell.is_multi_byte_character_support_enabled());
222 } else {
223 // If there are no embedded \n characters, then apply word wrap.
224 //
225 // Configured column width cannot be lower than (padding_left +
226 // padding_right) This is a bad configuration E.g., the user is trying
227 // to force the column width to be 5 when padding_left and padding_right
228 // are each configured to 3 (padding_left + padding_right) = 6 >
229 // column_width
230 auto content_width = column_widths[j] > padding_left + padding_right
231 ? column_widths[j] - padding_left - padding_right
232 : column_widths[j];
233 auto word_wrapped_text = Format::word_wrap(text, content_width, cell.locale(),
234 cell.is_multi_byte_character_support_enabled());
235 splitted_cells_text[i][j] = Format::split_lines(
236 word_wrapped_text, "\n", cell.locale(), cell.is_multi_byte_character_support_enabled());
237 }
238 }
239 }
240
241 // For each row,
242 for (size_t i = 0; i < num_rows; ++i) {
243
244 // Print top border
245 bool border_top_printed{true};
246 for (size_t j = 0; j < num_columns; ++j) {
247 border_top_printed &= print_cell_border_top(stream, table, {i, j},
248 {row_heights[i], column_widths[j]}, num_columns);
249 }
250 if (border_top_printed)
251 stream << termcolor::reset << "\n";
252
253 // Print row contents with word wrapping

Callers

nothing calls this directly

Calls 6

sizeMethod · 0.45
estimate_num_columnsMethod · 0.45
formatMethod · 0.45
find_first_ofMethod · 0.45
localeMethod · 0.45

Tested by

no test coverage detected