TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
TablePrinter.cpp
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#include <TablePrinter.h>
16#include <stdexcept>
17#include <iomanip>
18#include <stdexcept>
19
20namespace bprinter
21{
22TablePrinter::TablePrinter(std::ostream * output, const std::string& separator)
23{
24 out_stream_ = output;
25 i_ = 0;
26 j_ = 0;
27 separator_ = separator;
28}
29
34
36{
37 return (int)column_headers_.size();
38}
39
40void TablePrinter::set_separator(const std::string& separator)
41{
42 separator_ = separator;
43}
44
46{
47 flush_left_ = true;
48}
49
51{
52 flush_left_ = false;
53}
54
55/** \brief Add a column to our table
56 **
57 ** \param header_name Name to be print for the header
58 ** \param column_width the width of the column (has to be >=5)
59 ** */
60void TablePrinter::AddColumn(const std::string& header_name, int column_width)
61{
62 if (column_width < 4)
63 {
64 throw std::invalid_argument("Column size has to be >= 4");
65 }
66
67 column_headers_.push_back(header_name);
68 column_widths_.push_back(column_width);
69}
70
71void TablePrinter::PrintHorizontalLine(int updown)
72{
73 *out_stream_ << (updown > 0 ? "┌" : updown < 0 ? "└" : "├"); // the left bar
74
75 for (int i = 0; i < (int) column_widths_.size(); i++)
76 for (int j = 0; j < column_widths_[i]; j++)
77 *out_stream_ << (!i || j ? "─" : updown > 0 ? "┬─" : updown < 0 ? "┴─" : "┼─");
78
79 *out_stream_ << (updown > 0 ? "┐" : updown < 0 ? "┘" : "┤"); // the right bar
80 *out_stream_ << "\n";
81}
82
84{
85 PrintHorizontalLine(1);
86 *out_stream_ << "│";
87
88 for (int i=0; i<get_num_columns(); ++i)
89 {
90
91 if(flush_left_)
92 *out_stream_ << std::left;
93 else
94 *out_stream_ << std::right;
95
96 *out_stream_ << std::setw(column_widths_.at(i)) << column_headers_.at(i).substr(0, column_widths_.at(i));
97 if (i != get_num_columns()-1)
98 {
99 *out_stream_ << separator_;
100 }
101 }
102
103 *out_stream_ << "│\n";
104 PrintHorizontalLine();
105}
106
108{
109 PrintHorizontalLine(-1);
110}
111
113{
114 OutputDecimalNumber<float>(input);
115 return *this;
116}
117
119{
120 OutputDecimalNumber<double>(input);
121 return *this;
122}
123
124}
TablePrinter & operator<<(endl input)
void AddColumn(const std::string &header_name, int column_width)
Add a column to our table.
TablePrinter(std::ostream *output, const std::string &separator="│")
void set_separator(const std::string &separator)