MVault 0.0.1
Loading...
Searching...
No Matches
PrintAsTable.h
1#pragma once
2
3#include "../VaultRecordRef.h"
4
5namespace mvlt
6{
26 template <template <class E, class Alloc = std::allocator<E>> class T>
27 void PrintAsTable(const T<VaultRecordRef>& container, bool isPrintId = false, const std::size_t& amountOfRecords = -1, const std::vector<std::string> keys = {}) noexcept
28 {
29 if (container.empty())
30 {
31 std::cout << "The keys could not be retrieved because the container is empty" << std::endl;
32 std::cout << " (0 records)" << std::endl;
33 return;
34 }
35
36 std::vector<std::string>* realKeys;
37 if (keys.empty())
38 realKeys = new std::vector<std::string>(container.front().GetKeys());
39 else
40 realKeys = const_cast<std::vector<std::string>*>(&keys);
41
42 std::vector<std::size_t> maxLengths(realKeys->size());
43
44 // Fill each max length using key length
45 for (std::size_t i = 0; i < realKeys->size(); ++i)
46 maxLengths[i] = (*realKeys)[i].length();
47
48 // Find each real max length
49 std::string dataString;
50 for (std::size_t i = 0; i < realKeys->size(); ++i)
51 {
52 for (const auto& record : container)
53 {
54 record.GetDataAsString((*realKeys)[i], dataString);
55 if (dataString.length() > maxLengths[i]) maxLengths[i] = dataString.length();
56 }
57 }
58
59 // Print splitter
60 std::cout << "+";
61 for (std::size_t i = 0; i < realKeys->size(); ++i)
62 {
63 std::cout << "-";
64 for (std::size_t j = 0; j < maxLengths[i]; ++j) std::cout << "-";
65 std::cout << "-+";
66 }
67 std::cout << std::endl;
68
69 // Print header
70 std::cout << "|";
71 for (std::size_t i = 0; i < realKeys->size(); ++i)
72 {
73 std::cout << " " << (*realKeys)[i];
74 for (std::size_t j = (*realKeys)[i].length(); j < maxLengths[i]; ++j) std::cout << " ";
75 std::cout << " |";
76 }
77 std::cout << std::endl;
78
79 // Print splitter
80 std::cout << "+";
81 for (std::size_t i = 0; i < realKeys->size(); ++i)
82 {
83 std::cout << "-";
84 for (std::size_t j = 0; j < maxLengths[i]; ++j) std::cout << "-";
85 std::cout << "-+";
86 }
87 std::cout << std::endl;
88
89 // Print records
90 std::size_t counter = 0;
91 for (const auto& record : container)
92 {
93 std::cout << "|";
94 for (std::size_t i = 0; i < realKeys->size(); ++i)
95 {
96 record.GetDataAsString((*realKeys)[i], dataString);
97 std::cout << " " << dataString;
98 for (std::size_t j = dataString.length(); j < maxLengths[i]; ++j) std::cout << " ";
99 std::cout << " |";
100 }
101 if (isPrintId)
102 std::cout << " " << record.GetRecordUniqueId() << std::endl;
103 else
104 std::cout << std::endl;
105
106 ++counter;
107 if (counter == amountOfRecords) break;
108 }
109
110 // Print splitter
111 if (amountOfRecords >= container.size())
112 {
113 std::cout << "+";
114 for (std::size_t i = 0; i < realKeys->size(); ++i)
115 {
116 std::cout << "-";
117 for (std::size_t j = 0; j < maxLengths[i]; ++j) std::cout << "-";
118 std::cout << "-+";
119 }
120 std::cout << std::endl;
121 }
122 else
123 {
124 std::cout << "$";
125 for (std::size_t i = 0; i < realKeys->size(); ++i)
126 {
127 std::cout << "~";
128 for (std::size_t j = 0; j < maxLengths[i]; ++j) std::cout << "~";
129 std::cout << "~$";
130 }
131 std::cout << std::endl;
132 }
133
134 std::cout << " (" << container.size() << " records)" << std::endl;
135
136 if (keys.empty())
137 delete realKeys;
138 }
139
141}
void PrintAsTable(const T< VaultRecordRef > &container, bool isPrintId=false, const std::size_t &amountOfRecords=-1, const std::vector< std::string > keys={}) noexcept
A method for printing data as tables.
Definition PrintAsTable.h:27