This commit is contained in:
Your Name 2025-04-21 22:33:44 +12:00
parent b6626cad30
commit c5cffcaea3

View File

@ -103,6 +103,32 @@ std::string process_cell(std::string str,std::string rowcolor) {
return result; return result;
} }
std::string width_print_centered(std::string str,int width, std::string rowcolor) {
size_t padding = (width - get_visible_length(str)) / 2;
std::ostringstream oss;
oss << rowcolor << std::string(padding, ' ') << process_cell(str, rowcolor) <<
std::string(width - get_codepoints(str) - padding, ' ') << "\033[0m";
// std::cout << "str = "<<str <<std::endl;
// std::cout << "width = "<<width <<std::endl;
// std::cout << "padding = "<<padding <<std::endl;
// std::cout << "get_visible_length(str) = "<<get_visible_length(str) <<std::endl;
// std::cout << "get_codepoints(str) = "<<get_codepoints(str) <<std::endl;
// std::cout << "oss.str() = "<<oss.str() <<std::endl;
return oss.str();
}
std::string width_print_left(std::string str,int width, std::string rowcolor) {
size_t padding = (width - get_visible_length(str))-1;
size_t lpad = (padding>1 ? 1 : 0);
size_t rpad = padding - lpad;
std::ostringstream oss;
oss << rowcolor << std::string(lpad, ' ') << process_cell(str, rowcolor)<< std::string(rpad, ' ') << "\033[0m";
return oss.str();
}
// // Helper function to process a cell with replacements // // Helper function to process a cell with replacements
// std::string process_cell(const std::string& cell) { // std::string process_cell(const std::string& cell) {
// std::string result; // std::string result;
@ -210,14 +236,14 @@ void tableprint::print() {
} }
std::cout << "" << std::endl; std::cout << "" << std::endl;
std::cout << "" << "\033[1;37m"; // White color for title std::cout << ""; // White color for title
size_t title_width = 0; size_t title_width = 0;
for (size_t width : col_widths) { for (size_t width : col_widths) {
title_width += width + 2; // +2 for padding title_width += width + 2; // +2 for padding
} }
title_width += col_widths.size() - 1; // Add space for vertical borders title_width += col_widths.size() - 1; // Add space for vertical borders
size_t padding = (title_width - title.length()) / 2;
std::cout << std::string(padding, ' ') << title << std::string(title_width - title.length() - padding, ' '); std::cout << width_print_centered(title,title_width,"\033[1;37m");
std::cout << "\033[90m│" << std::endl; std::cout << "\033[90m│" << std::endl;
// Use └─┴─┘ for bottom of title box to connect with table // Use └─┴─┘ for bottom of title box to connect with table
@ -240,9 +266,8 @@ void tableprint::print() {
// Print header // Print header
std::cout << ""; std::cout << "";
std::cout << "\033[1;36m"; // Cyan color for header
for (size_t i = 0; i < rows[0].size(); ++i) { for (size_t i = 0; i < rows[0].size(); ++i) {
std::cout << " " << std::setw(col_widths[i]) << std::left << rows[0][i] << " "; std::cout << width_print_centered(rows[0][i],col_widths[i]+2,"\033[1;36m");
if (i < rows[0].size() - 1) { if (i < rows[0].size() - 1) {
std::cout << "\033[90m│\033[1;36m"; // Border color then back to cyan std::cout << "\033[90m│\033[1;36m"; // Border color then back to cyan
} else { } else {
@ -268,13 +293,8 @@ void tableprint::print() {
for (size_t i = 0; i < row.size(); ++i) { for (size_t i = 0; i < row.size(); ++i) {
// Set the appropriate color for the row // Set the appropriate color for the row
std::string rowcolor = (row_idx % 2 == 1) ? "\033[38;5;142m" : "\033[38;5;250m"; std::string rowcolor = (row_idx % 2 == 1) ? "\033[38;5;142m" : "\033[38;5;250m";
std::cout << width_print_left(row[i],col_widths[i]+2,rowcolor);
std::cout << rowcolor; std::cout << "\033[90m" << "";
std::string processed_cell = process_cell(row[i],rowcolor);
int l = get_visible_length(row[i]);
for (int i = l+1 ; i < col_widths[i]; i++)
processed_cell += " ";
std::cout << processed_cell << "\033[90m" << "";
} }
std::cout << std::endl; std::cout << std::endl;