diff --git a/src/servers.cpp b/src/servers.cpp index b28634b..20a854c 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -55,9 +55,34 @@ void list_servers() { auto servers = get_configured_servers(); tableprint tp("All DropShell Servers"); - tp.add_row({"Name", "Address"}); + tp.add_row({"Name", "Address", "Service Health", "Ports"}); for (const auto& server : servers) { - tp.add_row({server.name, server.ssh_host}); + std::vector ports_used; + std::string serviceticks = ""; + std::vector services = get_server_services(server.name); + for (const auto& service : services) { + server_service ss; + if (ss.init(server.name, service)) + { + if (ss.is_healthy()) + serviceticks += ":tick: "; + else + serviceticks += ":cross: "; + } + std::vector ports = ss.get_ports(); + ports_used.insert(ports_used.end(), ports.begin(), ports.end()); + } + // convert ports_used to string + std::string ports_used_str = ""; + bool first = true; + for (const auto& port : ports_used) { + if (!first) { + ports_used_str += ", "; + } + ports_used_str += std::to_string(port); + first = false; + } + tp.add_row({server.name, server.ssh_host, serviceticks, ports_used_str}); } tp.print(); } diff --git a/src/tableprint.cpp b/src/tableprint.cpp index 7cb9c22..cf9b48a 100644 --- a/src/tableprint.cpp +++ b/src/tableprint.cpp @@ -3,6 +3,38 @@ #include #include #include +#include + +// 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 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;