GEOS
TableFormatter.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP
21 #define GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP
22 
23 #include "TableData.hpp"
24 #include "TableLayout.hpp"
25 
26 namespace geos
27 {
28 
33 {
34 
35 protected:
36 
39 
40  TableFormatter() = default;
41 
46  TableFormatter( TableLayout const & tableLayout );
47 
51  virtual ~TableFormatter() = default;
52 };
53 
58 {
59 public:
60 
66  {}
67 
72  TableCSVFormatter( TableLayout const & tableLayout );
73 
77  virtual ~TableCSVFormatter() = default;
78 
82  string headerToString() const;
83 
89  string dataToString( TableData const & tableData ) const;
90 
97  template< typename DATASOURCE >
98  string toString( DATASOURCE const & tableData ) const;
99 
100 };
101 
107 template<>
108 string TableCSVFormatter::toString< TableData >( TableData const & tableData ) const;
109 
110 
115 {
116 public:
117 
118 
124  {}
125 
130  TableTextFormatter( TableLayout const & tableLayout );
131 
132 
136  virtual ~TableTextFormatter() = default;
137 
141  string layoutToString() const;
142 
148  template< typename DATASOURCE >
149  string toString( DATASOURCE const & tableData ) const;
150 
151 private:
152 
154  static constexpr char m_verticalLine = '|';
156  static constexpr char m_horizontalLine = '-';
157 
163  void fillTableColumnsFromRows( std::vector< TableLayout::Column > & columns,
164  std::vector< std::vector< string > > & tableData ) const;
165 
173  void outputLayout( std::ostringstream & tableOutput,
174  std::vector< TableLayout::Column > & columns,
175  std::vector< string > const & msgTableError,
176  string & sectionSeparatingLine ) const;
177 
185  void splitAndSetColumnNames( std::vector< TableLayout::Column > & columns,
186  size_t & largestHeaderVectorSize,
187  std::vector< std::vector< string > > & splitHeaders ) const;
188 
193  void findAndSetMaxStringSize( std::vector< TableLayout::Column > & columns ) const;
194 
200  void increaseColumnsSize( std::vector< TableLayout::Column > & columns,
201  integer const extraCharacters ) const;
202 
209  void computeTableWidth( std::vector< TableLayout::Column > & columns,
210  std::vector< string > const & msgTableError ) const;
211 
218  void buildTableSeparators( std::vector< TableLayout::Column > const & columns,
219  string & topSeparator,
220  string & sectionSeparatingLine ) const;
221 
229  void outputTopRows( std::ostringstream & tableOutput,
230  std::vector< string > const & msg,
231  string_view topSeparator,
232  TableLayout::Alignment alignment ) const;
233 
243  void outputSectionRows( std::vector< TableLayout::Column > const & columns,
244  string_view sectionSeparatingLine,
245  std::ostringstream & rows,
246  integer const nbRows,
247  TableLayout::Section const section ) const;
248 };
249 
255 template<>
256 string TableTextFormatter::toString< TableData >( TableData const & tableData ) const;
257 }
258 
259 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP */
class for CSV formatting
string toString(DATASOURCE const &tableData) const
Convert a data source to a CSV string.
TableCSVFormatter()
Construct a new Table Formatter.
string headerToString() const
string dataToString(TableData const &tableData) const
Convert the table data to a CSV string.
TableCSVFormatter(TableLayout const &tableLayout)
Construct a new Table Formatter from a tableLayout.
virtual ~TableCSVFormatter()=default
Destroy the TableCSVFormatter object.
Class for managing table data.
Definition: TableData.hpp:34
abstract class for formatting table data
TableFormatter(TableLayout const &tableLayout)
Construct a new Table Formatter from a tableLayout.
TableLayout m_tableLayout
Layout for a table.
virtual ~TableFormatter()=default
Destroy the Table Formatter object.
Class for setup the table layout.
Definition: TableLayout.hpp:32
Alignment
Type of aligment for a column.
Definition: TableLayout.hpp:37
Section
Enumeration for table sections.
Definition: TableLayout.hpp:51
class for log formatting
TableTextFormatter()
Construct a new TableFormatter.
TableTextFormatter(TableLayout const &tableLayout)
Construct a new TableFormatter from a tableLayout.
string layoutToString() const
virtual ~TableTextFormatter()=default
Destroy the Table Text Formatter object.
string toString(DATASOURCE const &tableData) const
Convert a data source to a table string.
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
std::string_view string_view
String type.
Definition: DataTypes.hpp:94