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

@ -55,9 +55,34 @@ void list_servers() {
auto servers = get_configured_servers(); auto servers = get_configured_servers();
tableprint tp("All DropShell Servers"); tableprint tp("All DropShell Servers");
tp.add_row({"Name", "Address"}); tp.add_row({"Name", "Address", "Service Health", "Ports"});
for (const auto& server : servers) { for (const auto& server : servers) {
tp.add_row({server.name, server.ssh_host}); std::vector<int> ports_used;
std::string serviceticks = "";
std::vector<std::string> 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<int> 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(); tp.print();
} }

View File

@ -3,6 +3,38 @@
#include <algorithm> #include <algorithm>
#include <locale> #include <locale>
#include <cwchar> #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) { tableprint::tableprint(const std::string title) : title(title) {
// Set locale for wide character support // Set locale for wide character support
@ -26,7 +58,10 @@ void tableprint::print() {
std::vector<size_t> col_widths(rows[0].size(), 0); std::vector<size_t> col_widths(rows[0].size(), 0);
for (const auto& row : rows) { for (const auto& row : rows) {
for (size_t i = 0; i < row.size(); ++i) { 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) { for (size_t i = 0; i < row.size(); ++i) {
std::string cell = row[i]; std::string cell = row[i];
// Replace :tick: and :cross: with colored symbols // Replace :tick: and :cross: with colored symbols
if (cell == ":tick:") { std::wstring processed_cell = replace_all(cell, ":tick:", L"\033[1;32m✓\033[0m");
std::wcout << L" " << L"\033[1;32m" << std::setw(col_widths[i]) << std::left << L"" << L"\033[90m" << L""; processed_cell = replace_all(processed_cell, ":cross:", L"\033[1;31m✗\033[0m");
} else if (cell == ":cross:") {
std::wcout << L" " << L"\033[1;31m" << std::setw(col_widths[i]) << std::left << L"" << L"\033[90m" << L""; // 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 { } 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; std::wcout << std::endl;