This commit is contained in:
Your Name
2025-04-21 21:14:09 +12:00
parent 49948e3b80
commit 5e2e2bc70f
2 changed files with 70 additions and 8 deletions

View File

@ -3,6 +3,38 @@
#include <algorithm>
#include <locale>
#include <cwchar>
#include <string>
// Helper function to replace all occurrences of a substring in a regular string
std::wstring replace_all(const std::string& str, const std::string& from, const std::wstring& to) {
std::wstring result;
size_t start_pos = 0;
size_t find_pos;
while ((find_pos = str.find(from, start_pos)) != std::string::npos) {
result += std::wstring(str.begin() + start_pos, str.begin() + find_pos);
result += to;
start_pos = find_pos + from.length();
}
result += std::wstring(str.begin() + start_pos, str.end());
return result;
}
// Helper function to replace all occurrences of a substring in a wide string
std::wstring replace_all(const std::wstring& str, const std::string& from, const std::wstring& to) {
std::wstring result;
size_t start_pos = 0;
size_t find_pos;
std::wstring from_w(from.begin(), from.end());
while ((find_pos = str.find(from_w, start_pos)) != std::wstring::npos) {
result += str.substr(start_pos, find_pos - start_pos);
result += to;
start_pos = find_pos + from_w.length();
}
result += str.substr(start_pos);
return result;
}
tableprint::tableprint(const std::string title) : title(title) {
// Set locale for wide character support
@ -26,7 +58,10 @@ void tableprint::print() {
std::vector<size_t> col_widths(rows[0].size(), 0);
for (const auto& row : rows) {
for (size_t i = 0; i < row.size(); ++i) {
col_widths[i] = std::max(col_widths[i], row[i].length());
// Calculate width considering that :tick: and :cross: will be replaced with single characters
std::string cell = row[i];
size_t tick_count = std::count(cell.begin(), cell.end(), ':') / 2; // Rough estimate
col_widths[i] = std::max(col_widths[i], cell.length() - (tick_count * 5)); // 5 is length of ":tick:" minus 1
}
}
@ -92,12 +127,14 @@ void tableprint::print() {
for (size_t i = 0; i < row.size(); ++i) {
std::string cell = row[i];
// Replace :tick: and :cross: with colored symbols
if (cell == ":tick:") {
std::wcout << L" " << L"\033[1;32m" << std::setw(col_widths[i]) << std::left << L"" << L"\033[90m" << L"";
} else if (cell == ":cross:") {
std::wcout << L" " << L"\033[1;31m" << std::setw(col_widths[i]) << std::left << L"" << L"\033[90m" << L"";
std::wstring processed_cell = replace_all(cell, ":tick:", L"\033[1;32m✓\033[0m");
processed_cell = replace_all(processed_cell, ":cross:", L"\033[1;31m✗\033[0m");
// Set the appropriate color for the row
if (row_idx % 2 == 1) {
std::wcout << L" " << L"\033[38;5;142m" << processed_cell << L"\033[90m" << L"";
} else {
std::wcout << L" " << L"\033[37m" << std::setw(col_widths[i]) << std::left << std::wstring(cell.begin(), cell.end()) << L"\033[90m" << L"";
std::wcout << L" " << L"\033[38;5;250m" << processed_cell << L"\033[90m" << L"";
}
}
std::wcout << std::endl;