tabulate icon indicating copy to clipboard operation
tabulate copied to clipboard

Can force color output even not in console

Open HR1025 opened this issue 5 months ago • 0 comments

Hi, tabulate is a very useful tool, and I really like the feature of tabulate that supports colored output.

I noticed a phenomenon with the following test code:

int main() {
  Table movies;
  movies.add_row({"S/N", "Movie Name", "Director", "Estimated Budget", "Release Date"});
  movies.add_row({"tt1979376", "Toy Story 4", "Josh Cooley", "$200,000,000", "21 June 2019"});
  movies.add_row({"tt3263904", "Sully", "Clint Eastwood", "$60,000,000", "9 September 2016"});
  movies.add_row({"tt1535109", "Captain Phillips", "Paul Greengrass", "$55,000,000", " 11 October 2013"});

  // center align 'Director' column
  movies.column(2).format()
    .font_align(FontAlign::center);

  // right align 'Estimated Budget' column
  movies.column(3).format()
    .font_align(FontAlign::right);

  // right align 'Release Date' column
  movies.column(4).format()
    .font_align(FontAlign::right);

  // center-align and color header cells
  for (size_t i = 0; i < 5; ++i) {
    movies[0][i].format()
      .font_color(Color::yellow)
      .font_align(FontAlign::center)
      .font_style({FontStyle::bold});
  }

  std::cout << movies.str() << std::endl;
  std::cout << movies << std::endl;
}

The first print is without color, and the second print is with color.

After checking the code, it seems to be related to the is_colorized interface, which enables colored printing only when outputting to the console.

I understand the intention behind this design, but could add a user option to control this behavior separately? There are scenarios where we might want to retain colored printing even when not outputting to the console. For example, in my case: I plan to use tabulate to create a remote shell, where the remote print information is transmitted to the local machine via a socket and then printed locally. So, I use .str() to get the text data, but the color information is lost when printing locally.

HR1025 avatar Sep 24 '24 11:09 HR1025