osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
decorator.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
13 //====================================================
14 // Preprocessor settings
15 //====================================================
16 #pragma once
17 #ifndef OSMANIP_MANIPULATORS_DECORATOR_HPP
18 #define OSMANIP_MANIPULATORS_DECORATOR_HPP
19 
20 //====================================================
21 // Headers
22 //====================================================
23 
24 // My headers
29 
30 // STD headers
31 #include <string>
32 #include <string_view>
33 #include <unordered_map>
34 #include <vector>
35 
36 namespace osm {
37 
38  //====================================================
39  // Decorator
40  //====================================================
49  class Decorator {
50  public:
51 
52  // Constructors and destructor
53  Decorator();
54  ~Decorator();
55 
56  // Setters
57  void setColor(const std::string &color, std::ostream &os = osm::cout);
58  void setStyle(const std::string &style, std::ostream &os = osm::cout);
59  void resetColor(std::ostream &os = osm::cout);
60  void resetStyle(std::ostream &os = osm::cout);
61  void removeStyle(std::string_view style, std::ostream &os = osm::cout);
62  void resetFeatures(std::ostream &os = osm::cout);
63 
64  // Getters
65  std::string getColor(std::ostream &os = osm::cout);
66  std::string getStyle(std::ostream &os = osm::cout);
67  std::unordered_map<std::ostream *, std::string> getColorList();
68  std::unordered_map<std::ostream *, std::string> getStyleList();
69  std::ostream &getCurrentStream();
70 
71  // Operators
72  const Decorator &operator()(std::ostream &os = osm::cout);
73 
74  private:
75 
76  // Members
77  std::unordered_map<std::ostream *, std::string> colors, styles;
78  std::ostream *current_stream;
79  };
80 
81  //====================================================
82  // Operator <<
83  //====================================================
97  template <typename T>
98  std::ostream &operator<<(Decorator my_shell, const T &elem) {
99  if (my_shell.getColor(my_shell.getCurrentStream()) != "") {
100  my_shell.getCurrentStream() << feat(col, my_shell.getColor(my_shell.getCurrentStream()));
101  }
102  if (my_shell.getStyle(my_shell.getCurrentStream()) != "") {
103  std::vector<std::string> list_of_styles{
104  osm::split_string(my_shell.getStyle(my_shell.getCurrentStream()), " ")};
105  for (auto elem: list_of_styles) {
106  my_shell.getCurrentStream() << feat(sty, elem);
107  }
108  }
109  my_shell.getCurrentStream() << elem << feat(rst, "all");
110 
111  return my_shell.getCurrentStream();
112  }
113 } // namespace osm
114 
115 #endif
Class used to decorate an output stream. Each setting is set permanently on the chosen output stream ...
Definition: decorator.hpp:49
std::string getStyle(std::ostream &os=osm::cout)
Method used to return the selected style of a stream.
Definition: decorator.cpp:151
void setStyle(const std::string &style, std::ostream &os=osm::cout)
Method used to set the style of a stream.
Definition: decorator.cpp:73
const Decorator & operator()(std::ostream &os=osm::cout)
Operator overload to assign the value into parentheses to the "current_stream" variable.
Definition: decorator.cpp:188
~Decorator()
Destructor of Decorator class.
Definition: decorator.cpp:46
Decorator()
Default constructor of Decorator class.
Definition: decorator.cpp:37
void resetStyle(std::ostream &os=osm::cout)
Method used to reset the style or of a stream.
Definition: decorator.cpp:96
std::unordered_map< std::ostream *, std::string > getColorList()
Method used to return the map of streams with the respective color.
Definition: decorator.cpp:159
void resetColor(std::ostream &os=osm::cout)
Method used to reset the color of a stream.
Definition: decorator.cpp:87
std::unordered_map< std::ostream *, std::string > getStyleList()
Method used to return the map of streams with the respective style.
Definition: decorator.cpp:167
std::string getColor(std::ostream &os=osm::cout)
Method used to return the selected color of a stream.
Definition: decorator.cpp:141
std::ostream & getCurrentStream()
Method used to return the stream that is used to output stuff.
Definition: decorator.cpp:175
void setColor(const std::string &color, std::ostream &os=osm::cout)
Method used to set the color of a stream.
Definition: decorator.cpp:59
void resetFeatures(std::ostream &os=osm::cout)
Method used to reset all the features of a stream.
Definition: decorator.cpp:123
void removeStyle(std::string_view style, std::ostream &os=osm::cout)
Method used to remove one of the set styles (useful in case they are more than one).
Definition: decorator.cpp:105
Definition: canvas.cpp:30
std::ostream & operator<<(Decorator my_shell, const T &elem)
Operator overload to output a modified ostream object which properties are set thanks to the Decorato...
Definition: decorator.hpp:98
const std::unordered_map< std::string, std::string > rst
It is used to store the reset features commands.
Definition: colsty.cpp:114
const std::unordered_map< std::string, std::string > sty
It is used to store the styles.
Definition: colsty.cpp:95
const std::unordered_map< std::string, std::string > col
It is used to store the colors. Note: "bg" is the prefix of the background color features and "bd" is...
Definition: colsty.cpp:39
std::ostream cout
std::vector< std::string > split_string(const std::string &input, const std::string &regex)
Function used to split a string based on a certain reges.
Definition: generic.cpp:39
const std::string & feat(const std::unordered_map< std::string, std::string > &generic_map, const std::string &feat_string)
It takes an std::map object as the first argument and an std::string object (map key) as the second a...
Definition: common.cpp:41