diff --git a/CPU.cpp b/CPU.cpp index 186a403..3a96055 100644 --- a/CPU.cpp +++ b/CPU.cpp @@ -6,11 +6,14 @@ using namespace std; void display(const CPU &cpu) { + const int minTotalWidth = max((size_t) 38, cpu.cpuId.length() + 2); + // Calculate total width, make sure architecture fits + const vector widths = {18, max(minTotalWidth - 19, (int) cpu.architecture.length() + 2)}; + const int totalWidth = widths[0] + widths[1]; // CPU ID // width is different because bold characters are counted but invisible - string boldCenteredCpuId = centeredStr(boldStr(cpu.cpuId), 46); - printTableHeader({39}, {boldCenteredCpuId}); - static const vector widths = {18, 20}; + string boldCenteredCpuId = centeredStr(boldStr(cpu.cpuId), totalWidth + 8); + printTableHeader({totalWidth + 1}, {boldCenteredCpuId}); // Release Year cout << "| "; diff --git a/DisplayManager.h b/DisplayManager.h index 2843cbf..828d6e8 100644 --- a/DisplayManager.h +++ b/DisplayManager.h @@ -55,11 +55,20 @@ void DisplayManager::displayTree() const { "Architecture", "Base Clock (GHz)" }; - static const std::vector widths = {20, 14, 12, 20, 18}; - printTableHeader(widths, headers); + std::vector widths = {20, 14, 12, 20, 18}; + // Call the BST's inorder traversal method to calculate the column widths + bst->inOrder([this, &widths](const string &cpuId) { + CPU cpu; + cpu.setCpuId(cpuId); + this->hashTable->search(cpu, cpu, key_to_index); + widths[0] = max(cpu.getCpuId().length() + 2, (size_t) widths[0]); + widths[3] = max(cpu.getArchitecture().length() + 2, (size_t) widths[3]); + }); + + printTableHeader(widths, headers); // Call the BST's inorder traversal method to display the tree - bst->inOrder([this](const string &cpuId) { + bst->inOrder([this, &widths](const string &cpuId) { CPU cpu; cpu.setCpuId(cpuId); this->hashTable->search(cpu, cpu, key_to_index);