TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TablePrinter.h
1/****************************************************************************
2* Copyright (c) 2025, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15#ifndef TablePrinter_included
16#define TablePrinter_included
17
18#include <iostream>
19#include <iomanip>
20#include <vector>
21#include <string>
22#include <sstream>
23#include <cmath>
24
25namespace bprinter
26{
27class endl {};
28/** \class TablePrinter
29
30 Print a pretty table into your output of choice.
31
32 Usage:
33 TablePrinter tp(&std::cout);
34 tp.AddColumn("Name", 25);
35 tp.AddColumn("Age", 3);
36 tp.AddColumn("Position", 30);
37
38 tp.PrintHeader();
39 tp << "Dat Chu" << 25 << "Research Assistant";
40 tp << "John Doe" << 26 << "Professional Anonymity";
41 tp << "Jane Doe" << tp.SkipToNextLine();
42 tp << "Tom Doe" << 7 << "Student";
43 tp.PrintFooter();
44
45 \todo Add support for padding in each table cell
46 */
48{
49public:
50 TablePrinter(std::ostream * output, const std::string& separator = "│");
52
53 int get_num_columns() const;
54 int get_table_width() const;
55 void set_separator(const std::string& separator);
56 void set_flush_left();
57 void set_flush_right();
58
59 void AddColumn(const std::string& header_name, int column_width);
60 void PrintHeader();
61 void PrintFooter();
62
64 {
65 while (j_ != 0)
66 {
67 *this << "";
68 }
69 return *this;
70 }
71
72 // Can we merge these?
73 TablePrinter& operator<<(float input);
74 TablePrinter& operator<<(double input);
75
76 template<typename T> TablePrinter& operator<<(T input)
77 {
78 if (j_ == 0)
79 *out_stream_ << "│";
80
81 if(flush_left_)
82 *out_stream_ << std::left;
83 else
84 *out_stream_ << std::right;
85
86 // Leave 3 extra space: One for negative sign, one for zero, one for decimal
87 *out_stream_ << std::setw(column_widths_.at(j_))
88 << input;
89
90 if (j_ == get_num_columns()-1)
91 {
92 *out_stream_ << "│\n";
93 i_ = i_ + 1;
94 j_ = 0;
95 }
96 else
97 {
98 *out_stream_ << separator_;
99 j_ = j_ + 1;
100 }
101
102 return *this;
103 }
104
105private:
106 void PrintHorizontalLine(int updown = 0);
107
108 template<typename T> void OutputDecimalNumber(T input);
109
110 std::ostream * out_stream_;
111 std::vector<std::string> column_headers_;
112 std::vector<int> column_widths_;
113 std::string separator_;
114
115 int i_; // index of current row
116 int j_; // index of current column
117
118 int table_width_ = -10;
119 bool flush_left_ = false;
120};
121
122}
123
124#include <TablePrinter.tpp>
125#endif
TablePrinter & operator<<(endl input)
int get_table_width() const
void AddColumn(const std::string &header_name, int column_width)
Add a column to our table.
TablePrinter & operator<<(T input)
TablePrinter(std::ostream *output, const std::string &separator="│")
void set_separator(const std::string &separator)