CppConsoleTable Save

C++ Console Table: make text table in console easy and set up as you need

Project README



:ru:На русском

With this tool you can make text table in console easily and set up as you need.

Integration

The single required source, file CppConsoleTable.hpp is in the root directory or released here. All you need to do is add

#include "CppConsoleTable.hpp"

// for convenience
using ConsoleTable = samilton::ConsoleTable;

to the files you want to use ConsoleTable.

CppConsoleTable uses C++17 standard, so it's necessary to enable C++17.

Examples

With the ConsoleTable class, you could write:

// create an empty structure (null)
ConsoleTable table;

// add a number of double type
table[0][1] = 3.141;

// add a Boolean 
table[1][0] = true;

// add a number of int type
table[2][1] = 56;

// add a "const char[]" string (it also could be an std::string)
table[2][0] = "some";

If you want to add new row, column to existence table, or assign new table, you should try this methods:

// add new column to table using initializer_list
table.addColumn({ 1, 2, 3 }); 

// add new row to table from vector
std::vector<char> vec = { 'a', 'b', 'c' };
table.addRow(vec);

// add new column to table from vanilla dynamic array
double *arr = new double[3];
arr[0] = 1.3; arr[1] = 2.34; arr[2] = 3.14;
table.addColumn(arr, 3);

// assign new table with elements by function using initializer_list
table.assign({ {"stringA1", "stringA2"}, {"stringB1"}, {"stringC1", "stringC2", "stringC3"} }); 

// assign new table with elements by function using vanilla dynamic array
std::string **arr = new std::string*[3];
for (auto i = 0; i < 3; ++i) {
	arr[i] = new std::string[2];
	for (auto j = 0; j < 2; ++j) { arr[i][j] = std::to_string(i+j); }
}
table.assign(arr, 2, 3);

// assign new table with elements by overloaded assign operator using initializer_list
table = { {1, 22}, {33}, {37, 74, 945} }; 

// assign new table with elements from vector
std::vector<std::vector<int>> vec = { {1, 4, 6}, {2, 3} };
table = vec;

You can also change the alignment of all objects in table (for default it's left):

// using constructor
ConsoleTable table(samilton::Alignment::center);

// or using a method
table.setAlignment(samilton::Alignment::right);

Or, you can change alignment of specific row or cell:

// set alignment to specific row
table[0](samilton::Alignment::center);

// set alignment to specific row with element assign
table[0](samilton::Alignment::right)[1] = 3.14;

// set alignment to specific cell
table[1][2](samilton::Alignment::left) = "ExampleString";

If you need indent before or after your elements, you can change it:

// using constructor
ConsoleTable table(2, 3);

// or using a method
table.setIndent(2, 3);

If you need to change the characters of table, you can easily do it with TableChars struct:

// creating struct
ConsoleTable::TableChars chars;

// modifying characters
chars.topDownSimple = '-';
chars.leftSeparation = '+';

// changing characters in table
table.setTableChars(chars);

Then, all you need to do is out this table to stream:

// using iostream
std::cout << table;

// you can also out it to file using fstream
std::fstream file("test.txt");
file << table;

// of course you can out it also to stringstream
std::stringstream stream;
stream << table;

Full example of using:

#include <iostream>

#include "CppConsoleTable.hpp"

using namespace std;
using ConsoleTable = samilton::ConsoleTable;

int main()
{
	ConsoleTable table(1, 1, samilton::Alignment::centre);

	table[0][0] = "Some String";
	table[1][3] = true;
	table[2][1] = 10;
	table[3][3] = "Some\nMulti String";
	table[2][2] = 2.354;
	table[0][1] = false;

	cout << table;

	system("pause");
}

Output:

╔═════════════╦═══════╦══════════╦══════════════╗
║ Some String ║ false ║          ║              ║
╠═════════════╬═══════╬══════════╬══════════════╣
║             ║       ║          ║     true     ║
╠═════════════╬═══════╬══════════╬══════════════╣
║             ║  10   ║ 2.354000 ║              ║
╠═════════════╬═══════╬══════════╬══════════════╣
║             ║       ║          ║     Some     ║
║             ║       ║          ║ Multi String ║
╚═════════════╩═══════╩══════════╩══════════════╝

License

The class is licensed under the MIT License:

Copyright © 2019 DenisSamilton

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contact

If you have questions regarding the library, I would like to invite you to open an issue at Github. Please describe your request, problem, or question as detailed as possible, and also mention the version of the library you are using as well as the version of your compiler and operating system. Opening an issue at Github allows other users and contributors to this library to collaborate.

Only if your request would contain confidential information, please send me an email.

Open Source Agenda is not affiliated with "CppConsoleTable" Project. README Source: DenisSamilton/CppConsoleTable
Stars
57
Open Issues
13
Last Commit
1 year ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating