Guest User

Untitled

a guest
Apr 19th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. +---------------+-------------+-----------------+-----------------+---------------------+
  2. | Country | Capital | Population | Area | Currency |
  3. +---------------+-------------+-----------------+-----------------+---------------------+
  4. | Australia | NEW ENTRY | 24,877,800 | 7,692,024 km2 | Australian Dollar |
  5. | China | Beijing | 1,403,500,365 | 9,596,961 km2 | Yuan |
  6. | France | Paris | 67,201,000 | 640,679 km2 | Euro |
  7. | Germany | Berlin | 82,800,000 | 357,168 km2 | Euro |
  8. | Iceland | Reykjavik | 348,580 | 102,775 km2 | Icelandic Krona |
  9. | Netherlands | Amsterdam | 17,200,671 | 41,543 km2 | Euro |
  10. +---------------+-------------+-----------------+-----------------+---------------------+
  11.  
  12. #include "ConsoleTable.h"
  13.  
  14.  
  15. ConsoleTable::ConsoleTable(std::initializer_list<std::string> headers) {
  16. this->headers = {headers};
  17.  
  18. for (std::string column : headers) {
  19. widths.push_back(column.length());
  20. }
  21. }
  22.  
  23.  
  24. void ConsoleTable::setPadding(unsigned int n) {
  25. this->padding = n;
  26. }
  27.  
  28.  
  29. void ConsoleTable::setStyle(unsigned int n) {
  30. switch (n) {
  31. case 0 :
  32. style = BasicStyle;
  33. break;
  34. case 1 :
  35. style = LineStyle;
  36. break;
  37. case 2 :
  38. style = DoubleLineStyle;
  39. break;
  40. case 3 :
  41. style = InvisibleStyle;
  42. break;
  43. default :
  44. style = BasicStyle;
  45. break;
  46. }
  47. }
  48.  
  49.  
  50. bool ConsoleTable::addRow(std::initializer_list<std::string> row) {
  51. if (row.size() > widths.size()) {
  52. throw std::invalid_argument{"Appended row size must be same as header size"};
  53. }
  54.  
  55. std::vector<std::string> r = std::vector<std::string>{row};
  56. rows.push_back(r);
  57. for (int i = 0; i < r.size(); ++i) {
  58. widths[i] = std::max(r[i].size(), widths[i]);
  59. }
  60. return true;
  61. }
  62.  
  63.  
  64. bool ConsoleTable::removeRow(unsigned int index) {
  65. if (index > rows.size())
  66. return false;
  67.  
  68. rows.erase(rows.begin() + index);
  69. return true;
  70. }
  71.  
  72.  
  73. ConsoleTable &ConsoleTable::operator+=(std::initializer_list<std::string> row) {
  74. if (row.size() > widths.size()) {
  75. throw std::invalid_argument{"Appended row size must be same as header size"};
  76. }
  77.  
  78. addRow(row);
  79. return *this;
  80. }
  81.  
  82.  
  83. ConsoleTable &ConsoleTable::operator-=(unsigned int rowIndex) {
  84. if (rows.size() < rowIndex)
  85. throw std::out_of_range{"Row index out of range."};
  86.  
  87. removeRow(rowIndex);
  88.  
  89. }
  90.  
  91.  
  92. std::string ConsoleTable::getLine(RowType rowType) const {
  93. std::stringstream line;
  94. line << rowType.left;
  95. for (int i = 0; i < widths.size(); ++i) {
  96. for (int j = 0; j < (widths[i] + padding + padding); ++j) {
  97. line << style.horizontal;
  98. }
  99. line << (i == widths.size() - 1 ? rowType.right : rowType.intersect);
  100. }
  101. return line.str() + "n";
  102. }
  103.  
  104.  
  105. std::string ConsoleTable::getHeaders(Headers headers) const {
  106. std::stringstream line;
  107. line << style.vertical;
  108. for (int i = 0; i < headers.size(); ++i) {
  109. std::string text = headers[i];
  110. line << space * padding + text + space * (widths[i] - text.length()) + space * padding;
  111. line << style.vertical;
  112. }
  113. line << "n";
  114. return line.str();
  115. }
  116.  
  117.  
  118. std::string ConsoleTable::getRows(Rows rows) const {
  119. std::stringstream line;
  120. for (int i = 0; i < rows.size(); ++i) {
  121. line << style.vertical;
  122. for (int j = 0; j < rows[i].size(); ++j) {
  123. std::string text = rows[i][j];
  124. line << space * padding + text + space * (widths[j] - text.length()) + space * padding;
  125. line << style.vertical;
  126. }
  127. line << "n";
  128. }
  129.  
  130. return line.str();
  131. }
  132.  
  133.  
  134. std::ostream &operator<<(std::ostream &out, const ConsoleTable &consoleTable) {
  135. out << consoleTable.getLine(consoleTable.style.top);
  136. out << consoleTable.getHeaders(consoleTable.headers);
  137. out << consoleTable.getLine(consoleTable.style.middle);
  138. out << consoleTable.getRows(consoleTable.rows);
  139. out << consoleTable.getLine(consoleTable.style.bottom);
  140. return out;
  141. }
  142.  
  143. bool ConsoleTable::sort(bool ascending) {
  144. if (ascending)
  145. std::sort(rows.begin(), rows.end(), std::less<std::vector<std::string>>());
  146. else
  147. std::sort(rows.begin(), rows.end(), std::greater<std::vector<std::string>>());
  148. return true;
  149. }
  150.  
  151. void ConsoleTable::updateRow(unsigned int row, unsigned int header, std::string data) {
  152. if (row > rows.size() - 1)
  153. throw std::out_of_range{"Row index out of range."};
  154. if (header > headers.size() - 1)
  155. throw std::out_of_range{"Header index out of range."};
  156.  
  157. rows[row][header] = data;
  158. }
  159.  
  160. void ConsoleTable::updateHeader(unsigned int header, std::string text) {
  161. if (header > headers.size())
  162. throw std::out_of_range{"Header index out of range."};
  163.  
  164. headers[header] = text;
  165. }
  166.  
  167.  
  168. std::string operator*(const std::string &other, int repeats) {
  169. std::string ret;
  170. ret.reserve(other.size() * repeats);
  171. for (; repeats; --repeats)
  172. ret.append(other);
  173. return ret;
  174. }
  175.  
  176. #ifndef CONSOLETABLE_CONSOLETABLE_H
  177. #define CONSOLETABLE_CONSOLETABLE_H
  178.  
  179. #include <string>
  180. #include <vector>
  181. #include <iostream>
  182. #include <sstream>
  183. #include <memory>
  184. #include <algorithm>
  185.  
  186. typedef std::vector<std::string> Headers;
  187. typedef std::vector<std::vector<std::string>> Rows;
  188. typedef std::vector<std::size_t> Widths;
  189.  
  190. class ConsoleTable {
  191. public:
  192.  
  193. /// Initialize a new ConsoleTable
  194. /// param headers Stringlist of the tables headers
  195. ConsoleTable(std::initializer_list<std::string> headers);
  196.  
  197.  
  198. /// Sets the distance from the text to the cell border
  199. /// param n Spaces between the text and the cell border
  200. void setPadding(unsigned int n);
  201.  
  202.  
  203. /// Sets the style of the table, default is 0
  204. /// n = 0 : Basic table style
  205. /// n = 1 : Single lined table style
  206. /// n = 2 : Double lined table style
  207. /// n = 3 : Invisivle table lines style
  208. /// param n The table style number
  209. void setStyle(unsigned int n);
  210.  
  211.  
  212. /// Sorts the table rows based on the first column
  213. /// param ascending Should table be sorted ascending or descending
  214. /// return True if sorting was successful, otherwise false
  215. bool sort(bool ascending);
  216.  
  217.  
  218. /// Adds a new row to the table
  219. /// param row A list of strings to add as row
  220. /// return True if the value was added successfully, otherwise false
  221. bool addRow(std::initializer_list<std::string> row);
  222.  
  223.  
  224. /// Removes a row from the table by the row index
  225. /// param index The index of the row that should be removed
  226. /// return True if the row was removed successfully, otherwise false
  227. bool removeRow(unsigned int index);
  228.  
  229.  
  230. /// Update an existing table cell with new data
  231. /// param row The index of the row that needs to be updated
  232. /// param header The index of the column that needs to be updated
  233. /// param data The new data that should be assigned to teh cell
  234. void updateRow(unsigned int row, unsigned int header, std::string data);
  235.  
  236.  
  237. /// Update a header with new text
  238. /// param header Index of the header that should be updated
  239. /// param text The new teext of the new header
  240. void updateHeader(unsigned int header, std::string text);
  241.  
  242.  
  243. /// Operator of the addRow() function
  244. /// param row A list of strings to add as row
  245. /// return this
  246. ConsoleTable &operator+=(std::initializer_list<std::string> row);
  247.  
  248.  
  249. /// Operator of the removeRow() function
  250. /// param rowIndex The index of the row that should be removed
  251. /// return this
  252. ConsoleTable &operator-=(unsigned int rowIndex);
  253.  
  254.  
  255. /// Holds all header strings of the table
  256. Headers headers;
  257.  
  258.  
  259. /// Holds all rows of the table
  260. Rows rows;
  261.  
  262.  
  263. /// Holds the size of widest string of each column of the table
  264. Widths widths;
  265.  
  266. private:
  267.  
  268. struct RowType {
  269. std::string left;
  270. std::string intersect;
  271. std::string right;
  272. };
  273.  
  274. struct TableStyle {
  275.  
  276. std::string horizontal;
  277. std::string vertical;
  278. RowType top;
  279. RowType middle;
  280. RowType bottom;
  281. };
  282.  
  283. TableStyle BasicStyle = {"-", "|", {"+", "+", "+"}, {"+", "+", "+"}, {"+", "+", "+"}};
  284. TableStyle LineStyle = {"━", "┃", {"┏", "┳", "┓"}, {"┣", "╋", "┫"}, {"┗", "┻", "┛"}};
  285. TableStyle DoubleLineStyle = {"═", "║", {"╔", "╦", "╗"}, {"╠", "╬", "╣"}, {"╚", "╩", "╝"}};
  286. TableStyle InvisibleStyle = {" ", " ", {" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}};
  287. TableStyle style = BasicStyle;
  288.  
  289. std::string space = " ";
  290.  
  291.  
  292. /// The distance between the cell text and the cell border
  293. unsigned int padding = 1;
  294.  
  295.  
  296. /// Returns a formatted horizontal separation line for the table
  297. /// param rowType The type of the row (top, middle, bottom)
  298. /// return The formatted row string
  299. std::string getLine(RowType rowType) const;
  300.  
  301.  
  302. /// Returns a formatted header string
  303. /// param headers The Headers-object that holds the header strings
  304. /// return The formatted header string
  305. std::string getHeaders(Headers headers) const;
  306.  
  307.  
  308. /// Returns a formmatted row string
  309. /// param rows The Rows-object that holds all rows of the table
  310. /// return A formatted string of all rows in the table
  311. std::string getRows(Rows rows) const;
  312.  
  313.  
  314. /// Writes the entire table with all its contents in the output stream
  315. /// This can be used to display the table using the std::cout function
  316. /// param out The output stream the table should be written to
  317. /// param consoleTable The ConsoleTable-object
  318. /// return Output stream with the formatted table string
  319. friend std::ostream &operator<<(std::ostream &out, const ConsoleTable &consoleTable);
  320.  
  321. };
  322.  
  323.  
  324. /// Repeats a given string n times
  325. /// param other The string to repeat
  326. /// param repeats The amount the string should be repeated
  327. /// return The repeated string
  328. std::string operator*(const std::string &other, int repeats);
  329.  
  330.  
  331. #endif //CONSOLETABLE_CONSOLETABLE_H
  332.  
  333. #include <iostream>
  334. #include "ConsoleTable.h"
  335.  
  336. int main() {
  337.  
  338. ConsoleTable table{"Country", "Capital", "Population", "Area", "Currency"};
  339.  
  340. table.setPadding(2);
  341. table.setStyle(3);
  342.  
  343. table += {"Germany", "Berlin", "82,800,000", "357,168 km2", "Euro"};
  344. table += {"France", "Paris", "67,201,000", "640,679 km2 ", "Euro"};
  345. table += {"South Korea", "Seoul", "51,446,201", "100,210 km2 ", "South Korean Won"};
  346. table += {"Australia", "Canberra", "24,877,800", "7,692,024 km2", "Australian Dollar"};
  347. table += {"China", "Beijing", "1,403,500,365", "9,596,961 km2", "Yuan"};
  348. table += {"Iceland", "Reykjavik", "348,580", "102,775 km2", "Icelandic Krona"};
  349. table += {"Netherlands", "Amsterdam", "17,200,671", "41,543 km2", "Euro"};
  350.  
  351. table.updateRow(3, 1, "NEW ENTRY");
  352.  
  353. // Remove some entries
  354. table -= 2;
  355. table -= 1;
  356. table -= 0;
  357. table.sort(true);
  358.  
  359. std::cout << table;
  360.  
  361. return 0;
  362. }
  363.  
  364. this->headers = {headers};
  365.  
  366. :headers{headers}
Add Comment
Please, Sign In to add comment