A library for working with phylogenetic and population genetic data.
v0.27.0
style.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
34 
35 #include <algorithm>
36 #include <cassert>
37 #include <ostream>
38 #include <stdexcept>
39 #include <vector>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // Local Helper Functions
46 // =================================================================================================
47 
51 static std::array<std::pair<std::string, std::string>, 17>::const_iterator get_foreground_color_iterator(
52  std::string name
53 ) {
54  name = replace_all(name, " ", "");
55  name = replace_all(name, "_", "");
56 
57  return std::find_if(
60  [&name] ( std::pair<std::string, std::string> const& elem ) {
61  return equals_ci( elem.first, name );
62  }
63  );
64 }
65 
69 static std::array<std::pair<std::string, std::string>, 17>::const_iterator get_background_color_iterator(
70  std::string name
71 ) {
72  name = replace_all(name, " ", "");
73  name = replace_all(name, "_", "");
74 
75  return std::find_if(
78  [&name] ( std::pair<std::string, std::string> const& elem ) {
79  return equals_ci( elem.first, name );
80  }
81  );
82 }
83 
84 // =================================================================================================
85 // Properties
86 // =================================================================================================
87 
92 {
93  foreground_ = "";
94  background_ = "";
95  bold_ = false;
96 
97  return *this;
98 }
99 
103 bool Style::enabled() const
104 {
105  return enabled_;
106 }
107 
116 Style& Style::enabled( bool value )
117 {
118  enabled_ = value;
119  return *this;
120 }
121 
125 bool Style::bold() const
126 {
127  return bold_;
128 }
129 
135 Style& Style::bold( bool value )
136 {
137  bold_ = value;
138  return *this;
139 }
140 
141 std::string Style::foreground_color() const
142 {
143  if( foreground_ == "" ) {
144  return "";
145  }
146 
147  auto it = get_foreground_color_iterator(foreground_);
148  assert( it != Style::foreground_colors.end() );
149  return it->first;
150 }
151 
152 Style& Style::foreground_color( std::string const& color )
153 {
154  // Check if valid color, throw if not.
155  if( color != "" ) {
157  }
158 
159  foreground_ = color;
160  return *this;
161 }
162 
163 std::string Style::background_color() const
164 {
165  if( background_ == "" ) {
166  return "";
167  }
168 
169  auto it = get_background_color_iterator(background_);
170  assert( it != Style::background_colors.end() );
171  return it->first;
172 }
173 
174 Style& Style::background_color( std::string const& color )
175 {
176  // Check if valid color, throw if not.
177  if( color != "" ) {
179  }
180 
181  background_ = color;
182  return *this;
183 }
184 
185 // =================================================================================================
186 // Output
187 // =================================================================================================
188 
193 static std::string get_attribute_string_( Style const& s)
194 {
195  if( ! s.enabled() ) {
196  return "";
197  }
198 
199  std::vector<std::string> attribs;
200 
201  if( s.bold() ) {
202  attribs.push_back( "1" );
203  }
204  if( s.foreground_color() != "" ) {
205  attribs.push_back( s.get_foreground_color_value( s.foreground_color() ));
206  }
207  if( s.background_color() != "" ) {
208  attribs.push_back( s.get_background_color_value( s.background_color() ));
209  }
210 
211  return join( attribs, ";" );
212 }
213 
220 std::string Style::operator() ( std::string const& text ) const
221 {
222  return to_bash_string( text );
223 }
224 
231 std::string Style::to_bash_string( std::string const& text ) const
232 {
233  // return "\e[" + get_attribute_string_(*this) + "m" + text_ + "\e[0m";
234 
235  auto attribs = get_attribute_string_(*this);
236  if( attribs.size() > 0 ) {
237  return "\033[" + get_attribute_string_(*this) + "m" + text + "\033[0m";
238  } else {
239  return text;
240  }
241 }
242 
249 std::string Style::to_python_string( std::string const& text ) const
250 {
251  auto attribs = get_attribute_string_(*this);
252  if( attribs.size() > 0 ) {
253  return "\x1b[" + get_attribute_string_(*this) + "m" + text + "\x1b[0m";
254  } else {
255  return text;
256  }
257 }
258 
259 // =================================================================================================
260 // Style Data
261 // =================================================================================================
262 
263 // -------------------------------------------------------------------
264 // Foreground Color
265 // -------------------------------------------------------------------
266 
270 bool Style::is_foreground_color( std::string name )
271 {
273 }
274 
281 std::string Style::get_foreground_color_value( std::string name )
282 {
283  auto it = get_foreground_color_iterator(name);
284 
285  if( it == Style::foreground_colors.end() ) {
286  throw std::out_of_range("No foreground color named " + name + ".");
287  }
288  return it->second;
289 }
290 
294 const std::array<std::pair<std::string, std::string>, 17> Style::foreground_colors = {{
295  { "Default", "39" },
296  { "Black", "30" },
297  { "Red", "31" },
298  { "Green", "32" },
299  { "Yellow", "33" },
300  { "Blue", "34" },
301  { "Magenta", "35" },
302  { "Cyan", "36" },
303  { "LightGray", "37" },
304  { "DarkGray", "90" },
305  { "LightRed", "91" },
306  { "LightGreen", "92" },
307  { "LightYellow", "93" },
308  { "LightBlue", "94" },
309  { "LightMagenta", "95" },
310  { "LightCyan", "96" },
311  { "White", "97" }
312 }};
313 
314 // -------------------------------------------------------------------
315 // Background Color
316 // -------------------------------------------------------------------
317 
321 bool Style::is_background_color( std::string name )
322 {
324 }
325 
332 std::string Style::get_background_color_value( std::string name )
333 {
334  auto it = get_background_color_iterator(name);
335 
336  if( it == Style::background_colors.end() ) {
337  throw std::out_of_range("No background color named " + name + ".");
338  }
339  return it->second;
340 }
341 
345 const std::array<std::pair<std::string, std::string>, 17> Style::background_colors = {{
346  { "Default", "49" },
347  { "Black", "40" },
348  { "Red", "41" },
349  { "Green", "42" },
350  { "Yellow", "43" },
351  { "Blue", "44" },
352  { "Magenta", "45" },
353  { "Cyan", "46" },
354  { "LightGray", "47" },
355  { "DarkGray", "100" },
356  { "LightRed", "101" },
357  { "LightGreen", "102" },
358  { "LightYellow", "103" },
359  { "LightBlue", "104" },
360  { "LightMagenta", "105" },
361  { "LightCyan", "106" },
362  { "White", "107" }
363 }};
364 
365 // =================================================================================================
366 // Basic Operators
367 // =================================================================================================
368 
380 std::ostream& operator << ( std::ostream& out, Style const& style )
381 {
382  out << "Foreground Color: " << style.foreground_color() << "\n";
383  out << "Background Color: " << style.background_color() << "\n";
384  out << "Bold: " << ( style.bold() ? "true" : "false" ) << "\n";
385  return out;
386 }
387 
388 } // namespace utils
389 } // namespace genesis
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::Style::get_background_color_value
static std::string get_background_color_value(std::string name)
Return the color value string for a given background color name.
Definition: style.cpp:332
genesis::utils::get_attribute_string_
static std::string get_attribute_string_(Style const &s)
Internal helper function that returns the attribute string of a Style based on its properties.
Definition: style.cpp:193
genesis::utils::equals_ci
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings case insensitive.
Definition: string.cpp:67
genesis::utils::replace_all
std::string replace_all(std::string const &text, std::string const &search, std::string const &replace)
Return a copy of a string, where all occurrences of a search string are replaced by a replace string.
Definition: string.cpp:530
genesis::utils::Style::is_background_color
static bool is_background_color(std::string name)
Return true iff the given name is a background color name.
Definition: style.cpp:321
genesis::utils::Style::foreground_color
std::string foreground_color() const
Definition: style.cpp:141
genesis::utils::Style::background_colors
static const std::array< std::pair< std::string, std::string >, 17 > background_colors
List of all valid background color names and their color strings.
Definition: style.hpp:179
genesis::utils::Style::get_foreground_color_value
static std::string get_foreground_color_value(std::string name)
Return the color value string for a given foreground color name.
Definition: style.cpp:281
string.hpp
Provides some commonly used string utility functions.
genesis::utils::Style::is_foreground_color
static bool is_foreground_color(std::string name)
Return true iff the given name is a foreground color name.
Definition: style.cpp:270
genesis::utils::Style::to_bash_string
std::string to_bash_string(std::string const &text) const
Additional output function with the same purpose as operator().
Definition: style.cpp:231
genesis::utils::Style
Simple text style class for colorized and bold output to a terminal.
Definition: style.hpp:81
genesis::utils::Style::foreground_colors
static const std::array< std::pair< std::string, std::string >, 17 > foreground_colors
List of all valid foreground color names and their color strings.
Definition: style.hpp:178
genesis::utils::join
Interval< DataType, NumericalType, IntervalKind > join(Interval< DataType, NumericalType, IntervalKind > const &a, Interval< DataType, NumericalType, IntervalKind > const &b)
Creates a new Interval that contains both intervals and whatever is between.
Definition: utils/containers/interval_tree/functions.hpp:127
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::Style::to_python_string
std::string to_python_string(std::string const &text) const
Additional output function with the same purpose as operator().
Definition: style.cpp:249
genesis::utils::Style::enabled
bool enabled() const
Return whether the Style is currently enabled.
Definition: style.cpp:103
genesis::utils::Style::bold
bool bold() const
Return whether the Style uses bold.
Definition: style.cpp:125
genesis::utils::get_foreground_color_iterator
static std::array< std::pair< std::string, std::string >, 17 >::const_iterator get_foreground_color_iterator(std::string name)
Internal helper function that returns an iterator into the foreground color list.
Definition: style.cpp:51
genesis::utils::Style::background_color
std::string background_color() const
Definition: style.cpp:163
genesis::utils::get_background_color_iterator
static std::array< std::pair< std::string, std::string >, 17 >::const_iterator get_background_color_iterator(std::string name)
Internal helper function that returns an iterator into the background color list.
Definition: style.cpp:69
style.hpp
genesis::utils::Style::reset
Style & reset()
Reset the Style to use not colors and not bold.
Definition: style.cpp:91
genesis::utils::Style::operator()
std::string operator()(std::string const &text) const
Operator that returns a text with the current Style applied to it.
Definition: style.cpp:220