A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2017 Lucas Czech
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 
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
TableLayout minimal_layout()
void write_label(std::ostream &stream) const
Column(std::string label, Justification justify=Justification::kLeft)
TableLayout double_grid(bool condensed)
void shrink_width()
Shrinks the column width to the minmal width that suffices to fit in all values of the column (i...
TableLayout simple_layout(bool condensed)
void clear()
Clears all columns and their data from the table.
Column & operator=(Column const &)=default
Table & operator=(Table const &)=default
std::string to_string() const
One line of the TableLayout.
Simple text style class for colorized and bold output to a terminal.
Definition: style.hpp:81
void clear_content()
Clears the data contents of all columns. Their labels etc stay unchanged.
Column & add_column(std::string label="")
Add a column to the table.
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...
std::string row(size_t i) const
Binder(TableLayout const &l, Table const &t)
Helper struct to bind a layout to a table.
void append(std::string value)
Table & line_break()
Finish the currently line and move to the next one.
std::ostream & operator<<(std::ostream &os, const Matrix< T > &matrix)
Print the elements of a Matrix to a stream, using operator << for each element.
TableLayout extended_frame(bool condensed)
TableLayout extended_grid(bool condensed)
TableLayout simple_grid(bool condensed)
friend std::ostream & operator<<(std::ostream &out, Binder const &binder)
void write(std::ostream &out) const
Table & operator<<(std::string value)
void write_row(std::ostream &stream, size_t row) const
Justification justify() const
Table & append(std::string value)
TableLayout simple_frame(bool condensed)
TableLayout double_frame(bool condensed)