A library for working with phylogenetic and population genetic data.
v0.27.0
utils/text/table.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TEXT_TABLE_H_
2 #define GENESIS_UTILS_TEXT_TABLE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
36 #include <iosfwd>
37 #include <string>
38 #include <utility>
39 #include <vector>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // Forward Declarations
46 // =================================================================================================
47 
48 struct TableLayout;
49 class Table;
50 
51 std::ostream& operator << (std::ostream& out, Table const& table);
52 
53 // =================================================================================================
54 // Text Table
55 // =================================================================================================
56 
60 class Table
61 {
62  // ---------------------------------------------------------------------
63  // Member Types
64  // ---------------------------------------------------------------------
65 
66 public:
67 
68  class Column;
69 
70  // ---------------------------------------------------------------------
71  // Constructor and Rule of Five
72  // ---------------------------------------------------------------------
73 
74 public:
75 
76  Table() = default;
77  ~Table() = default;
78 
79  Table( Table const& ) = default;
80  Table( Table&& ) = default;
81 
82  Table& operator= ( Table const& ) = default;
83  Table& operator= ( Table&& ) = default;
84 
85  // void swap( Table& other )
86  // {
87  // using std::swap;
88  // }
89 
90  // ---------------------------------------------------------------------
91  // Accessors
92  // ---------------------------------------------------------------------
93 
94  size_t length() const;
95 
96  // ---------------------------------------------------------------------
97  // Modifiers
98  // ---------------------------------------------------------------------
99 
100  void clear();
101  void clear_content();
102 
103  Column& add_column( std::string label = "" );
104 
105  Table& operator << ( std::string value );
106  // Table& operator << ( Style const& value );
107 
108  Table& append ( std::string value );
109  Table& append ( Style const& style, std::string value );
110 
111  Table& line_break();
112 
113  // ---------------------------------------------------------------------
114  // Output
115  // ---------------------------------------------------------------------
116 
117  void write( std::ostream& out ) const;
118  void write( std::ostream& out, TableLayout const& layout ) const;
119 
120  std::string to_string() const;
121  std::string to_string( TableLayout const& layout ) const;
122 
123  friend std::ostream& operator << (std::ostream& out, Table const& table);
124 
125  // ---------------------------------------------------------------------
126  // Data members
127  // ---------------------------------------------------------------------
128 
129 private:
130 
131  size_t current_col_ = 0;
132  std::vector<Column> columns_;
133 
134 };
135 
136 // =================================================================================================
137 // Table Column
138 // =================================================================================================
139 
141 {
142  // ---------------------------------------------------------------------
143  // Member Types
144  // ---------------------------------------------------------------------
145 
146 public:
147 
148  enum class Justification
149  {
150  kLeft,
151  kRight,
152  kCentered
153  };
154 
155  // ---------------------------------------------------------------------
156  // Constructor and Rule of Five
157  // ---------------------------------------------------------------------
158 
159 public:
160 
161  Column() = default;
162 
163  explicit Column( std::string const& label, Justification justify = Justification::kLeft )
164  : label_( label )
165  , just_( justify )
166  , width_( label.size() )
167  {}
168 
169  ~Column() = default;
170 
171  Column( Column const& ) = default;
172  Column( Column&& ) = default;
173 
174  Column& operator= ( Column const& ) = default;
175  Column& operator= ( Column&& ) = default;
176 
177  // void swap( Column& other )
178  // {
179  // using std::swap;
180  // }
181 
182  // ---------------------------------------------------------------------
183  // Properties
184  // ---------------------------------------------------------------------
185 
186  void label( std::string value );
187  std::string label() const;
188 
189  void justify( Justification value );
190  Justification justify() const;
191 
192  void width( size_t value );
193  size_t width() const;
194 
195  void shrink_width();
196 
197  // ---------------------------------------------------------------------
198  // Accessors
199  // ---------------------------------------------------------------------
200 
201  size_t length() const;
202 
203  std::string row( size_t i ) const;
204 
205  // ---------------------------------------------------------------------
206  // Modifiers
207  // ---------------------------------------------------------------------
208 
209  void clear_content();
210 
211  void append( std::string value );
212  void append( Style const& style, std::string value );
213 
214  // ---------------------------------------------------------------------
215  // Output
216  // ---------------------------------------------------------------------
217 
218 public:
219 
220  void write_row( std::ostream& stream, size_t row ) const;
221  void write_label( std::ostream& stream ) const;
222 
223 private:
224 
225  void write( std::ostream& stream, Style style, std::string text ) const;
226 
227  // ---------------------------------------------------------------------
228  // Data members
229  // ---------------------------------------------------------------------
230 
231 private:
232 
233  std::string label_ = "";
235  size_t width_ = 0;
236 
237  std::vector< std::pair< Style, std::string >> data_;
238 
239 };
240 
241 // =================================================================================================
242 // TableLayout
243 // =================================================================================================
244 
246 {
247  // ---------------------------------------------------------------------
248  // Member Types
249  // ---------------------------------------------------------------------
250 
265  struct Line
266  {
267  bool enabled = false;
268 
269  std::string left_border = "";
270  std::string filler = "";
271  std::string separator = " ";
272  std::string right_border = "";
273  };
274 
282  struct Binder
283  {
284  Binder(TableLayout const& l, Table const& t)
285  : layout(l)
286  , table(t)
287  {}
288 
290  Table const& table;
291 
292  friend std::ostream& operator << (std::ostream& out, Binder const& binder);
293  };
294 
295  // ---------------------------------------------------------------------
296  // Operators
297  // ---------------------------------------------------------------------
298 
299  Binder operator () ( Table const& table );
300 
301  // ---------------------------------------------------------------------
302  // Data Members
303  // ---------------------------------------------------------------------
304 
310 
311 };
312 
313 // ---------------------------------------------------------------------
314 // Default Table Layouts
315 // ---------------------------------------------------------------------
316 
318 
319 TableLayout simple_layout( bool condensed = false );
320 TableLayout simple_grid( bool condensed = false );
321 TableLayout simple_frame( bool condensed = false );
322 
323 TableLayout extended_grid( bool condensed = false );
324 TableLayout extended_frame( bool condensed = false );
325 
326 TableLayout double_grid( bool condensed = false );
327 TableLayout double_frame( bool condensed = false );
328 
329 } // namespace utils
330 } // namespace genesis
331 
332 #endif // include guard
genesis::utils::Table::Column::clear_content
void clear_content()
Definition: utils/text/table.cpp:345
genesis::utils::operator<<
std::ostream & operator<<(std::ostream &os, const Matrix< signed char > &matrix)
Template specialization for signed char, in order to print nicely.
Definition: utils/containers/matrix/operators.cpp:89
genesis::utils::TableLayout::Binder::operator<<
friend std::ostream & operator<<(std::ostream &out, Binder const &binder)
Definition: utils/text/table.cpp:405
genesis::utils::Table::append
Table & append(std::string value)
Definition: utils/text/table.cpp:125
genesis::utils::simple_frame
TableLayout simple_frame(bool condensed)
Definition: utils/text/table.cpp:483
genesis::utils::Table::~Table
~Table()=default
genesis::utils::minimal_layout
TableLayout minimal_layout()
Definition: utils/text/table.cpp:439
genesis::utils::extended_frame
TableLayout extended_frame(bool condensed)
Definition: utils/text/table.cpp:523
genesis::utils::Table::Column::row
std::string row(size_t i) const
Definition: utils/text/table.cpp:335
genesis::utils::Table::operator=
Table & operator=(Table const &)=default
genesis::utils::TableLayout::row
Line row
Definition: utils/text/table.hpp:308
genesis::utils::TableLayout::Line::left_border
std::string left_border
Definition: utils/text/table.hpp:269
genesis::utils::Table
Definition: utils/text/table.hpp:60
genesis::utils::TableLayout::Line::filler
std::string filler
Definition: utils/text/table.hpp:270
genesis::utils::Table::Column::append
void append(std::string value)
Definition: utils/text/table.cpp:351
genesis::utils::double_frame
TableLayout double_frame(bool condensed)
Definition: utils/text/table.cpp:573
genesis::utils::Table::Column::justify
Justification justify() const
Definition: utils/text/table.cpp:285
genesis::utils::Table::clear_content
void clear_content()
Clears the data contents of all columns. Their labels etc stay unchanged.
Definition: utils/text/table.cpp:91
genesis::utils::Table::add_column
Column & add_column(std::string label="")
Add a column to the table.
Definition: utils/text/table.cpp:105
genesis::utils::Table::write
void write(std::ostream &out) const
Definition: utils/text/table.cpp:171
genesis::utils::Table::Column::operator=
Column & operator=(Column const &)=default
genesis::utils::Table::Column::Justification::kLeft
@ kLeft
genesis::utils::Table::to_string
std::string to_string() const
Definition: utils/text/table.cpp:241
genesis::utils::Table::Column::length
size_t length() const
Definition: utils/text/table.cpp:330
genesis::utils::Table::clear
void clear()
Clears all columns and their data from the table.
Definition: utils/text/table.cpp:82
genesis::utils::Table::Table
Table()=default
genesis::utils::TableLayout::Line::right_border
std::string right_border
Definition: utils/text/table.hpp:272
genesis::utils::TableLayout::bottom
Line bottom
Definition: utils/text/table.hpp:309
genesis::utils::Table::Column::Justification::kRight
@ kRight
genesis::utils::TableLayout::Binder::table
Table const & table
Definition: utils/text/table.hpp:290
genesis::utils::Style
Simple text style class for colorized and bold output to a terminal.
Definition: style.hpp:81
genesis::utils::Table::line_break
Table & line_break()
Finish the currently line and move to the next one.
Definition: utils/text/table.cpp:156
genesis::utils::TableLayout::Binder
Helper struct to bind a layout to a table.
Definition: utils/text/table.hpp:282
genesis::utils::TableLayout::Binder::layout
TableLayout const & layout
Definition: utils/text/table.hpp:289
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::Table::Column::write_row
void write_row(std::ostream &stream, size_t row) const
Definition: utils/text/table.cpp:367
genesis::utils::double_grid
TableLayout double_grid(bool condensed)
Definition: utils/text/table.cpp:554
genesis::utils::Table::Column::width
size_t width() const
Definition: utils/text/table.cpp:304
genesis::utils::simple_layout
TableLayout simple_layout(bool condensed)
Definition: utils/text/table.cpp:445
genesis::utils::TableLayout::operator()
Binder operator()(Table const &table)
Functional operator that allows to bind a TableLayout to a Table so that they can be used in one ostr...
Definition: utils/text/table.cpp:430
genesis::utils::Table::Column
Definition: utils/text/table.hpp:140
genesis::utils::TableLayout::header
Line header
Definition: utils/text/table.hpp:306
genesis::utils::Table::Column::Justification::kCentered
@ kCentered
genesis::utils::Table::length
size_t length() const
Definition: utils/text/table.cpp:54
genesis::utils::Table::Column::shrink_width
void shrink_width()
Shrinks the column width to the minmal width that suffices to fit in all values of the column (i....
Definition: utils/text/table.cpp:317
genesis::utils::Table::Column::Column
Column(std::string const &label, Justification justify=Justification::kLeft)
Definition: utils/text/table.hpp:163
genesis::utils::TableLayout
Definition: utils/text/table.hpp:245
genesis::utils::TableLayout::Line::separator
std::string separator
Definition: utils/text/table.hpp:271
genesis::utils::Table::Column::label
std::string label() const
Definition: utils/text/table.cpp:275
genesis::utils::extended_grid
TableLayout extended_grid(bool condensed)
Definition: utils/text/table.cpp:504
genesis::utils::Table::Column::Justification
Justification
Definition: utils/text/table.hpp:148
genesis::utils::Table::Column::Column
Column()=default
genesis::utils::TableLayout::Line
One line of the TableLayout.
Definition: utils/text/table.hpp:265
genesis::utils::Table::operator<<
Table & operator<<(std::string value)
Definition: utils/text/table.cpp:115
genesis::utils::TableLayout::Line::enabled
bool enabled
Definition: utils/text/table.hpp:267
genesis::utils::TableLayout::separator
Line separator
Definition: utils/text/table.hpp:307
genesis::utils::simple_grid
TableLayout simple_grid(bool condensed)
Definition: utils/text/table.cpp:464
genesis::utils::TableLayout::top
Line top
Definition: utils/text/table.hpp:305
genesis::utils::TableLayout::Binder::Binder
Binder(TableLayout const &l, Table const &t)
Definition: utils/text/table.hpp:284
style.hpp
genesis::utils::Table::Column::write_label
void write_label(std::ostream &stream) const
Definition: utils/text/table.cpp:374
genesis::utils::Table::Column::~Column
~Column()=default