A library for working with phylogenetic and population genetic data.
v0.32.0
style.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TEXT_STYLE_H_
2 #define GENESIS_UTILS_TEXT_STYLE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 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 
34 #include <array>
35 #include <iosfwd>
36 #include <utility>
37 #include <string>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Text Style
44 // =================================================================================================
45 
81 class Style
82 {
83  // -------------------------------------------------------------------
84  // Constructors and Rule of Five
85  // -------------------------------------------------------------------
86 
87 public:
88 
89  Style() = default;
90 
91  explicit Style( std::string const& foreground_color )
92  : foreground_(foreground_color)
93  {}
94 
95  explicit Style( const char * foreground_color )
96  : foreground_(foreground_color)
97  {}
98 
99  explicit Style( bool bold )
100  : bold_(bold)
101  {}
102 
103  Style( std::string const& foreground_color, bool bold )
104  : foreground_(foreground_color)
105  , bold_(bold)
106  {}
107 
108  Style( std::string const& foreground_color, std::string const& background_color )
109  : foreground_(foreground_color)
110  , background_(background_color)
111  {}
112 
113  Style( std::string const& foreground_color, const char * background_color )
114  : foreground_(foreground_color)
115  , background_(background_color)
116  {}
117 
118  Style( std::string const& foreground_color, std::string const& background_color, bool bold )
119  : foreground_(foreground_color)
120  , background_(background_color)
121  , bold_(bold)
122  {}
123 
124  ~Style() = default;
125 
126  Style(Style const&) = default;
127  Style(Style&&) = default;
128 
129  Style& operator= (Style const&) = default;
130  Style& operator= (Style&&) = default;
131 
132  void swap (Style& other)
133  {
134  using std::swap;
135 
136  swap(foreground_, other.foreground_);
137  swap(background_, other.background_);
138  swap(bold_, other.bold_);
139  }
140 
141  // -------------------------------------------------------------------
142  // Properties
143  // -------------------------------------------------------------------
144 
145  Style& reset();
146 
147  bool enabled() const;
148  Style& enabled( bool value );
149 
150  bool bold() const;
151  Style& bold( bool value );
152 
153  std::string foreground_color() const;
154  Style& foreground_color( std::string const& color );
155 
156  std::string background_color() const;
157  Style& background_color( std::string const& color );
158 
159  // -------------------------------------------------------------------
160  // Output
161  // -------------------------------------------------------------------
162 
163  std::string operator() ( std::string const& text ) const;
164 
165  std::string to_bash_string( std::string const& text ) const;
166  std::string to_python_string( std::string const& text ) const;
167 
168  // -------------------------------------------------------------------
169  // Style Data
170  // -------------------------------------------------------------------
171 
172  static bool is_foreground_color( std::string name );
173  static bool is_background_color( std::string name );
174 
175  static std::string get_foreground_color_value( std::string name );
176  static std::string get_background_color_value( std::string name );
177 
178  static const std::array<std::pair<std::string, std::string>, 17> foreground_colors;
179  static const std::array<std::pair<std::string, std::string>, 17> background_colors;
180 
181  // -------------------------------------------------------------------
182  // Data Members
183  // -------------------------------------------------------------------
184 
185 private:
186 
187  std::string foreground_ = "";
188  std::string background_ = "";
189 
190  bool bold_ = false;
191  bool enabled_ = true;
192 };
193 
194 // =================================================================================================
195 // Basic Operators
196 // =================================================================================================
197 
198 inline void swap (Style& lhs, Style& rhs)
199 {
200  lhs.swap(rhs);
201 }
202 
203 std::ostream& operator << ( std::ostream& out, Style const& style );
204 
205 } // namespace utils
206 } // namespace genesis
207 
208 #endif // include guard
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::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::swap
void swap(Style &lhs, Style &rhs)
Definition: style.hpp:198
genesis::utils::Style::Style
Style(bool bold)
Definition: style.hpp:99
genesis::utils::operator<<
std::ostream & operator<<(std::ostream &os, Color const &color)
Write a textual representation of the Color the a stream, in the format "(r, g, b,...
Definition: utils/color/functions.cpp:129
genesis::utils::Style::foreground_color
std::string foreground_color() const
Definition: style.cpp:141
genesis::utils::Style::Style
Style()=default
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::~Style
~Style()=default
genesis::utils::swap
void swap(Color &lhs, Color &rhs)
Definition: color.hpp:207
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
genesis::utils::Style::Style
Style(std::string const &foreground_color, std::string const &background_color)
Definition: style.hpp:108
genesis::utils::Style::Style
Style(std::string const &foreground_color, bool bold)
Definition: style.hpp:103
genesis::utils::Style::Style
Style(std::string const &foreground_color, std::string const &background_color, bool bold)
Definition: style.hpp:118
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
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::Style::Style
Style(const char *foreground_color)
Definition: style.hpp:95
genesis::utils::Style::Style
Style(std::string const &foreground_color)
Definition: style.hpp:91
genesis::utils::Style::background_color
std::string background_color() const
Definition: style.cpp:163
genesis::utils::Style::operator=
Style & operator=(Style const &)=default
genesis::utils::Style::reset
Style & reset()
Reset the Style to use not colors and not bold.
Definition: style.cpp:91
genesis::utils::Style::Style
Style(std::string const &foreground_color, const char *background_color)
Definition: style.hpp:113
genesis::utils::Style::swap
void swap(Style &other)
Definition: style.hpp:132
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