This commit is contained in:
Your Name 2025-04-21 22:39:09 +12:00
parent c5cffcaea3
commit 013176dbe5

View File

@ -104,23 +104,25 @@ std::string process_cell(std::string str,std::string rowcolor) {
} }
std::string width_print_centered(std::string str,int width, std::string rowcolor) { std::string width_print_centered(std::string str,int width, std::string rowcolor) {
size_t padding = (width - get_visible_length(str)) / 2; size_t padding = (width - get_visible_length(str));
size_t lpad = padding/2;
size_t rpad = padding - lpad;
std::ostringstream oss; std::ostringstream oss;
oss << rowcolor << std::string(padding, ' ') << process_cell(str, rowcolor) << oss << rowcolor << std::string(lpad, ' ') << process_cell(str, rowcolor) <<
std::string(width - get_codepoints(str) - padding, ' ') << "\033[0m"; std::string(rpad, ' ') << "\033[0m";
// std::cout << "str = "<<str <<std::endl; // std::cout << "str = "<<str <<std::endl;
// std::cout << "width = "<<width <<std::endl; // std::cout << "width = "<<width <<std::endl;
// std::cout << "padding = "<<padding <<std::endl; // std::cout << "padding = "<<padding <<std::endl;
// std::cout << "get_visible_length(str) = "<<get_visible_length(str) <<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 << "get_codepoints(str) = "<<get_codepoints(str) <<std::endl;
// std::cout << "oss.str() = "<<oss.str() <<std::endl; // std::cout << "oss.str() = ["<<oss.str() <<"]"<<std::endl;
return oss.str(); return oss.str();
} }
std::string width_print_left(std::string str,int width, std::string rowcolor) { std::string width_print_left(std::string str,int width, std::string rowcolor) {
size_t padding = (width - get_visible_length(str))-1; size_t padding = (width - get_visible_length(str));
size_t lpad = (padding>1 ? 1 : 0); size_t lpad = (padding>1 ? 1 : 0);
size_t rpad = padding - lpad; size_t rpad = padding - lpad;
std::ostringstream oss; std::ostringstream oss;
@ -128,45 +130,6 @@ std::string width_print_left(std::string str,int width, std::string rowcolor) {
return oss.str(); return oss.str();
} }
// // Helper function to process a cell with replacements
// std::string process_cell(const std::string& cell) {
// std::string result;
// size_t pos = 0;
// while (pos < cell.length()) {
// bool found_replacement = false;
// for (const auto& [key, value] : kReplacements) {
// if (cell.compare(pos, key.length(), key) == 0) {
// result += get_color_code(value.color) + value.text + "\033[0m";
// pos += key.length();
// found_replacement = true;
// break;
// }
// }
// if (!found_replacement) {
// result += cell[pos];
// pos++;
// }
// }
// return result;
// }
// // Helper function to get visible length (ignoring color codes)
// size_t get_visible_length(const std::string& str) {
// size_t length = 0;
// bool in_color_code = false;
// for (size_t i = 0; i < str.length(); ++i) {
// if (str[i] == '\033') {
// while (i < str.length() && str[i] != 'm') {
// i++;
// }
// } else {
// length++;
// }
// }
// return length;
// }
tableprint::tableprint(const std::string title) : title(title) { tableprint::tableprint(const std::string title) : title(title) {
// Set locale for wide character support // Set locale for wide character support
std::setlocale(LC_ALL, ""); std::setlocale(LC_ALL, "");
@ -294,7 +257,7 @@ void tableprint::print() {
// 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 << width_print_left(row[i],col_widths[i]+2,rowcolor);
std::cout << "\033[90m" << " "; std::cout << "\033[90m" << "";
} }
std::cout << std::endl; std::cout << std::endl;