Skip to content

Commit

Permalink
Merge pull request #98 from edisonhello/improvement/reduce-print-comp…
Browse files Browse the repository at this point in the history
…lixity

Improvement: Precalculate the cell content
  • Loading branch information
p-ranav committed Feb 8, 2023
2 parents ef7bee3 + 0086488 commit b5518f4
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 95 deletions.
78 changes: 48 additions & 30 deletions include/tabulate/printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ class Printer {

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

static void print_row_in_cell(std::ostream &stream, TableInternal &table,
const std::pair<size_t, size_t> &index,
const std::pair<size_t, size_t> &dimension, size_t num_columns,
size_t row_index);
static void
print_row_in_cell(std::ostream &stream, TableInternal &table,
const std::pair<size_t, size_t> &index,
const std::pair<size_t, size_t> &dimension,
size_t num_columns, size_t row_index,
const std::vector<std::string> &splitted_cell_text);

static bool print_cell_border_top(std::ostream &stream, TableInternal &table,
const std::pair<size_t, size_t> &index,
const std::pair<size_t, size_t> &dimension, size_t num_columns);
static bool print_cell_border_bottom(std::ostream &stream, TableInternal &table,
const std::pair<size_t, size_t> &index,
const std::pair<size_t, size_t> &dimension,
size_t num_columns);
const std::pair<size_t, size_t> &dimension,
size_t num_columns);
static bool
print_cell_border_bottom(std::ostream &stream, TableInternal &table,
const std::pair<size_t, size_t> &index,
const std::pair<size_t, size_t> &dimension,
size_t num_columns);

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

static void reset_element_style(std::ostream &stream) { stream << termcolor::reset; }
static void reset_element_style(std::ostream &stream) {
stream << termcolor::reset;
}

private:
static void print_content_left_aligned(std::ostream &stream, const std::string &cell_content,
const Format &format, size_t text_with_padding_size,
static void print_content_left_aligned(std::ostream &stream,
const std::string &cell_content,
const Format &format,
size_t text_with_padding_size,
size_t column_width) {

// Apply font style
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
*format.font_style_);
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, *format.font_style_);
stream << cell_content;
// Only apply font_style to the font
// Not the padding. So calling apply_element_style with font_style = {}
reset_element_style(stream);
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, {});

if (text_with_padding_size < column_width) {
for (size_t j = 0; j < (column_width - text_with_padding_size); ++j) {
Expand All @@ -91,8 +100,10 @@ class Printer {
}
}

static void print_content_center_aligned(std::ostream &stream, const std::string &cell_content,
const Format &format, size_t text_with_padding_size,
static void print_content_center_aligned(std::ostream &stream,
const std::string &cell_content,
const Format &format,
size_t text_with_padding_size,
size_t column_width) {
auto num_spaces = column_width - text_with_padding_size;
if (num_spaces % 2 == 0) {
Expand All @@ -101,13 +112,14 @@ class Printer {
stream << " ";

// Apply font style
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
*format.font_style_);
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, *format.font_style_);
stream << cell_content;
// Only apply font_style to the font
// Not the padding. So calling apply_element_style with font_style = {}
reset_element_style(stream);
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, {});

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

// Apply font style
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
*format.font_style_);
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, *format.font_style_);
stream << cell_content;
// Only apply font_style to the font
// Not the padding. So calling apply_element_style with font_style = {}
reset_element_style(stream);
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, {});

for (size_t j = 0; j < num_spaces - num_spaces_before; ++j)
stream << " ";
}
}

static void print_content_right_aligned(std::ostream &stream, const std::string &cell_content,
const Format &format, size_t text_with_padding_size,
static void print_content_right_aligned(std::ostream &stream,
const std::string &cell_content,
const Format &format,
size_t text_with_padding_size,
size_t column_width) {
if (text_with_padding_size < column_width) {
for (size_t j = 0; j < (column_width - text_with_padding_size); ++j) {
Expand All @@ -140,13 +155,14 @@ class Printer {
}

// Apply font style
apply_element_style(stream, *format.font_color_, *format.font_background_color_,
*format.font_style_);
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, *format.font_style_);
stream << cell_content;
// Only apply font_style to the font
// Not the padding. So calling apply_element_style with font_style = {}
reset_element_style(stream);
apply_element_style(stream, *format.font_color_, *format.font_background_color_, {});
apply_element_style(stream, *format.font_color_,
*format.font_background_color_, {});
}

static void apply_font_style(std::ostream &stream, FontStyle style) {
Expand Down Expand Up @@ -180,7 +196,8 @@ class Printer {
}
}

static void apply_foreground_color(std::ostream &stream, Color foreground_color) {
static void apply_foreground_color(std::ostream &stream,
Color foreground_color) {
switch (foreground_color) {
case Color::grey:
stream << termcolor::grey;
Expand Down Expand Up @@ -212,7 +229,8 @@ class Printer {
}
}

static void apply_background_color(std::ostream &stream, Color background_color) {
static void apply_background_color(std::ostream &stream,
Color background_color) {
switch (background_color) {
case Color::grey:
stream << termcolor::on_grey;
Expand Down

0 comments on commit b5518f4

Please sign in to comment.