Skip to content

Commit b5518f4

Browse files
authored
Merge pull request #98 from edisonhello/improvement/reduce-print-complixity
Improvement: Precalculate the cell content
2 parents ef7bee3 + 0086488 commit b5518f4

File tree

2 files changed

+136
-95
lines changed

2 files changed

+136
-95
lines changed

include/tabulate/printer.hpp

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,22 @@ class Printer {
4646

4747
static void print_table(std::ostream &stream, TableInternal &table);
4848

49-
static void print_row_in_cell(std::ostream &stream, TableInternal &table,
50-
const std::pair<size_t, size_t> &index,
51-
const std::pair<size_t, size_t> &dimension, size_t num_columns,
52-
size_t row_index);
49+
static void
50+
print_row_in_cell(std::ostream &stream, TableInternal &table,
51+
const std::pair<size_t, size_t> &index,
52+
const std::pair<size_t, size_t> &dimension,
53+
size_t num_columns, size_t row_index,
54+
const std::vector<std::string> &splitted_cell_text);
5355

5456
static bool print_cell_border_top(std::ostream &stream, TableInternal &table,
5557
const std::pair<size_t, size_t> &index,
56-
const std::pair<size_t, size_t> &dimension, size_t num_columns);
57-
static bool print_cell_border_bottom(std::ostream &stream, TableInternal &table,
58-
const std::pair<size_t, size_t> &index,
59-
const std::pair<size_t, size_t> &dimension,
60-
size_t num_columns);
58+
const std::pair<size_t, size_t> &dimension,
59+
size_t num_columns);
60+
static bool
61+
print_cell_border_bottom(std::ostream &stream, TableInternal &table,
62+
const std::pair<size_t, size_t> &index,
63+
const std::pair<size_t, size_t> &dimension,
64+
size_t num_columns);
6165

6266
static void apply_element_style(std::ostream &stream, Color foreground_color,
6367
Color background_color,
@@ -68,21 +72,26 @@ class Printer {
6872
apply_font_style(stream, style);
6973
}
7074

71-
static void reset_element_style(std::ostream &stream) { stream << termcolor::reset; }
75+
static void reset_element_style(std::ostream &stream) {
76+
stream << termcolor::reset;
77+
}
7278

7379
private:
74-
static void print_content_left_aligned(std::ostream &stream, const std::string &cell_content,
75-
const Format &format, size_t text_with_padding_size,
80+
static void print_content_left_aligned(std::ostream &stream,
81+
const std::string &cell_content,
82+
const Format &format,
83+
size_t text_with_padding_size,
7684
size_t column_width) {
7785

7886
// Apply font style
79-
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
80-
*format.font_style_);
87+
apply_element_style(stream, *format.font_color_,
88+
*format.font_background_color_, *format.font_style_);
8189
stream << cell_content;
8290
// Only apply font_style to the font
8391
// Not the padding. So calling apply_element_style with font_style = {}
8492
reset_element_style(stream);
85-
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
93+
apply_element_style(stream, *format.font_color_,
94+
*format.font_background_color_, {});
8695

8796
if (text_with_padding_size < column_width) {
8897
for (size_t j = 0; j < (column_width - text_with_padding_size); ++j) {
@@ -91,8 +100,10 @@ class Printer {
91100
}
92101
}
93102

94-
static void print_content_center_aligned(std::ostream &stream, const std::string &cell_content,
95-
const Format &format, size_t text_with_padding_size,
103+
static void print_content_center_aligned(std::ostream &stream,
104+
const std::string &cell_content,
105+
const Format &format,
106+
size_t text_with_padding_size,
96107
size_t column_width) {
97108
auto num_spaces = column_width - text_with_padding_size;
98109
if (num_spaces % 2 == 0) {
@@ -101,13 +112,14 @@ class Printer {
101112
stream << " ";
102113

103114
// Apply font style
104-
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
105-
*format.font_style_);
115+
apply_element_style(stream, *format.font_color_,
116+
*format.font_background_color_, *format.font_style_);
106117
stream << cell_content;
107118
// Only apply font_style to the font
108119
// Not the padding. So calling apply_element_style with font_style = {}
109120
reset_element_style(stream);
110-
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
121+
apply_element_style(stream, *format.font_color_,
122+
*format.font_background_color_, {});
111123

112124
for (size_t j = 0; j < num_spaces / 2; ++j)
113125
stream << " ";
@@ -117,21 +129,24 @@ class Printer {
117129
stream << " ";
118130

119131
// Apply font style
120-
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
121-
*format.font_style_);
132+
apply_element_style(stream, *format.font_color_,
133+
*format.font_background_color_, *format.font_style_);
122134
stream << cell_content;
123135
// Only apply font_style to the font
124136
// Not the padding. So calling apply_element_style with font_style = {}
125137
reset_element_style(stream);
126-
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
138+
apply_element_style(stream, *format.font_color_,
139+
*format.font_background_color_, {});
127140

128141
for (size_t j = 0; j < num_spaces - num_spaces_before; ++j)
129142
stream << " ";
130143
}
131144
}
132145

133-
static void print_content_right_aligned(std::ostream &stream, const std::string &cell_content,
134-
const Format &format, size_t text_with_padding_size,
146+
static void print_content_right_aligned(std::ostream &stream,
147+
const std::string &cell_content,
148+
const Format &format,
149+
size_t text_with_padding_size,
135150
size_t column_width) {
136151
if (text_with_padding_size < column_width) {
137152
for (size_t j = 0; j < (column_width - text_with_padding_size); ++j) {
@@ -140,13 +155,14 @@ class Printer {
140155
}
141156

142157
// Apply font style
143-
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
144-
*format.font_style_);
158+
apply_element_style(stream, *format.font_color_,
159+
*format.font_background_color_, *format.font_style_);
145160
stream << cell_content;
146161
// Only apply font_style to the font
147162
// Not the padding. So calling apply_element_style with font_style = {}
148163
reset_element_style(stream);
149-
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
164+
apply_element_style(stream, *format.font_color_,
165+
*format.font_background_color_, {});
150166
}
151167

152168
static void apply_font_style(std::ostream &stream, FontStyle style) {
@@ -180,7 +196,8 @@ class Printer {
180196
}
181197
}
182198

183-
static void apply_foreground_color(std::ostream &stream, Color foreground_color) {
199+
static void apply_foreground_color(std::ostream &stream,
200+
Color foreground_color) {
184201
switch (foreground_color) {
185202
case Color::grey:
186203
stream << termcolor::grey;
@@ -212,7 +229,8 @@ class Printer {
212229
}
213230
}
214231

215-
static void apply_background_color(std::ostream &stream, Color background_color) {
232+
static void apply_background_color(std::ostream &stream,
233+
Color background_color) {
216234
switch (background_color) {
217235
case Color::grey:
218236
stream << termcolor::on_grey;

0 commit comments

Comments
 (0)