Skip to content

Commit 868a723

Browse files
committed
Added working left and right indent
1 parent 67445d6 commit 868a723

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

CppConsoleTable.hpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ namespace samilton {
163163

164164
friend std::ostream &operator<<(std::ostream &stream, ConsoleTable &table);
165165
private:
166+
void _fillStreamByChar(std::ostream &stream, const char &fillChar, const size_t &lenght) {
167+
if (lenght > 0)
168+
stream << std::setfill(fillChar) << std::setw(lenght);
169+
}
170+
171+
void _fillStreamByChar(std::ostream &stream, const char &fillChar, const char &endChar, const size_t &lenght) {
172+
if (lenght > 0)
173+
stream << std::setfill(fillChar) << std::setw(lenght) << endChar;
174+
}
175+
166176
std::map<size_t, ConsoleRow*> _tableData;
167177
Alignment _alignment;
168178
size_t _leftIndent, _rightIndent;
@@ -203,11 +213,12 @@ namespace samilton {
203213
const char centreSeparation = 206;
204214

205215
if (table._alignment != ConsoleTable::Alignment::centre) {
206-
stream << std::right << topLeft << std::setfill(topDownSimple) << std::setw(columnWidth[0] + 1);
216+
stream << std::right << topLeft;
217+
table._fillStreamByChar(stream, topDownSimple, columnWidth[0] + 1 + table._leftIndent + table._rightIndent);
207218

208219
if (column != 1) {
209220
for (size_t i = 1; i < column; i++) {
210-
stream << topSeparation << std::setw(columnWidth[i] + 1);
221+
stream << topSeparation << std::setw(columnWidth[i] + 1 + table._leftIndent + table._rightIndent);
211222
}
212223
}
213224
stream << topRight << std::endl;
@@ -220,30 +231,44 @@ namespace samilton {
220231

221232
for (size_t j = 0; j < column; j++) {
222233
if (table._tableData[i] != nullptr && table._tableData[i]->_rowData[j] != nullptr) {
223-
stream << leftRightSimple << std::setfill(' ') << std::setw(columnWidth[j]) << table._tableData[i]->_rowData[j]->_str;
234+
stream << leftRightSimple;
235+
table._fillStreamByChar(stream, ' ', ' ', table._leftIndent);
236+
237+
table._fillStreamByChar(stream, ' ', columnWidth[j]);
238+
stream << table._tableData[i]->_rowData[j]->_str;
239+
240+
table._fillStreamByChar(stream, ' ', ' ', table._rightIndent);
224241
}
225242
else {
226-
stream << leftRightSimple << std::setfill(' ') << std::setw(columnWidth[j]) << ' ';
243+
stream << leftRightSimple;
244+
table._fillStreamByChar(stream, ' ', ' ', table._leftIndent);
245+
246+
table._fillStreamByChar(stream, ' ', columnWidth[j]);
247+
stream << ' ';
248+
249+
table._fillStreamByChar(stream, ' ', ' ', table._rightIndent);
227250
}
228251
}
229252
stream << std::right << leftRightSimple << std::endl;
230253

231254
if (i == row - 1) {
232-
stream << downLeft << std::setfill(topDownSimple) << std::setw(columnWidth[0] + 1);
255+
stream << downLeft;
256+
table._fillStreamByChar(stream, topDownSimple, columnWidth[0] + 1 + table._leftIndent + table._rightIndent);
233257

234258
if (column != 1) {
235259
for (size_t j = 1; j < column; j++) {
236-
stream << downSeparation << std::setw(columnWidth[j] + 1);
260+
stream << downSeparation << std::setw(columnWidth[j] + 1 + table._leftIndent + table._rightIndent);
237261
}
238262
}
239263
stream << downRight << std::endl;
240264
}
241265
else {
242-
stream << leftSeparation << std::setfill(topDownSimple) << std::setw(columnWidth[0] + 1);
266+
stream << leftSeparation;
267+
table._fillStreamByChar(stream, topDownSimple, columnWidth[0] + 1 + table._leftIndent + table._rightIndent);
243268

244269
if (column != 1) {
245270
for (size_t j = 1; j < column; j++) {
246-
stream << centreSeparation << std::setw(columnWidth[j] + 1);
271+
stream << centreSeparation << std::setw(columnWidth[j] + 1 + table._leftIndent + table._rightIndent);
247272
}
248273
}
249274
stream << rightSeparation << std::endl;

Example.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#include <iostream>
2-
#include <iomanip>
32

43
#include "CppConsoleTable.hpp"
54

6-
using namespace std;
7-
using namespace samilton;
5+
using ConsoleTable = samilton::ConsoleTable;
86

97
int main()
108
{
11-
ConsoleTable table(ConsoleTable::Alignment::right);
9+
ConsoleTable table(2, 3, ConsoleTable::Alignment::right);
1210

1311
table[0][0] = "some";
1412
table[1][3] = true;
@@ -17,7 +15,7 @@ int main()
1715
table[2][2] = 2.354;
1816
table[0][1] = false;
1917

20-
cout << table;
18+
std::cout << table;
2119

2220
system("pause");
2321
}

0 commit comments

Comments
 (0)