From 749efef29d47baf83fd9b6c65535533befd9d880 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 21 Apr 2025 20:56:54 +1200 Subject: [PATCH] Title --- src/servers.cpp | 2 +- src/tableprint.cpp | 25 ++++++++++++++++++++++++- src/tableprint.hpp | 4 +++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/servers.cpp b/src/servers.cpp index 696465e..b28634b 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -54,7 +54,7 @@ std::vector get_configured_servers() { void list_servers() { auto servers = get_configured_servers(); - tableprint tp; + tableprint tp("All DropShell Servers"); tp.add_row({"Name", "Address"}); for (const auto& server : servers) { tp.add_row({server.name, server.ssh_host}); diff --git a/src/tableprint.cpp b/src/tableprint.cpp index cb00d54..e2a9069 100644 --- a/src/tableprint.cpp +++ b/src/tableprint.cpp @@ -4,13 +4,17 @@ #include #include -tableprint::tableprint() { +tableprint::tableprint(const std::string title) : title(title) { // Set locale for wide character support std::setlocale(LC_ALL, ""); } tableprint::~tableprint() {} +void tableprint::set_title(const std::string title) { + this->title = title; +} + void tableprint::add_row(const std::vector& row) { rows.push_back(row); } @@ -26,6 +30,25 @@ void tableprint::print() { } } + // Calculate total table width + size_t total_width = 0; + for (size_t width : col_widths) { + total_width += width + 2; // +2 for padding + } + total_width += col_widths.size() - 1; // Add space for vertical borders + + // Print title if it exists + if (!title.empty()) { + std::wcout << L"\033[90m"; // Dark grey color for borders + std::wcout << L"┌" << std::wstring(total_width, L'─') << L"┐" << std::endl; + std::wcout << L"│" << L"\033[1;37m"; // White color for title + size_t padding = (total_width - title.length()) / 2; + std::wcout << std::wstring(padding, L' ') << std::wstring(title.begin(), title.end()) + << std::wstring(total_width - title.length() - padding, L' '); + std::wcout << L"\033[90m│" << std::endl; + std::wcout << L"└" << std::wstring(total_width, L'─') << L"┘" << std::endl; + } + // Print top border std::wcout << L"\033[90m"; // Dark grey color for borders std::wcout << L"┌"; diff --git a/src/tableprint.hpp b/src/tableprint.hpp index 25feefb..6305887 100644 --- a/src/tableprint.hpp +++ b/src/tableprint.hpp @@ -11,12 +11,14 @@ // assumes the first row is the header. class tableprint { public: - tableprint(); + tableprint(const std::string title = ""); ~tableprint(); void add_row(const std::vector& row); void print(); + void set_title(const std::string title); private: std::vector> rows; + std::string title; }; # endif