osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
printer.hpp
Go to the documentation of this file.
1 //====================================================
2 // Preprocessor settings
3 //====================================================
4 #ifndef PRINTER_HPP
5 #define PRINTER_HPP
6 
7 //====================================================
8 // Headers
9 //====================================================
10 
11 //My headers
12 #include "common.hpp"
13 #include "colsty.hpp"
14 
15 //Extra headers
16 #include <arsenalgear/stream.hpp>
17 #include <arsenalgear/utils.hpp>
18 
19 //STD headers
20 #include <iostream>
21 #include <vector>
22 #include <string>
23 #include <unordered_map>
24 
25 namespace osm
26  {
27  //====================================================
28  // print (first overload)
29  //====================================================
37  template <typename... Args>
38  inline void print( std::ostream& os = agr::null_stream, const Args&... args )
39  {
40  if( &os == &agr::null_stream ) std::cout << "\n";
41  else if( &os == &std::cerr ) os << feat( col, "red" ) << feat( sty, "bold" );
42  else if( &os == &std::clog ) os << feat( col, "lt blue" ) << feat( sty, "bold" );
43 
44  ( os << ... << args ) << "\n";
45 
46  if( &os == &std::cerr || &os == &std::clog || &os == &std::cout ) os << feat( rst, "all" );
47  }
48 
49  //====================================================
50  // print (second overload)
51  //====================================================
58  template <typename... Args>
59  inline void print( Args&... args )
60  {
61  ( std::cout << ... << args ) << "\n" << feat( rst, "all" );
62  }
63 
64  //====================================================
65  // OS_Decorator class
66  //====================================================
72  {
73  public:
74 
75  //====================================================
76  // Constructors and destructors
77  //====================================================
78  OS_Decorator();
79  ~OS_Decorator();
80 
81  //====================================================
82  // Setters
83  //====================================================
84  void setColor( const std::string& color, std::ostream& os = std::cout );
85  void setStyle( const std::string& style, std::ostream& os = std::cout );
86 
87  //====================================================
88  // Resetters
89  //====================================================
90  void resetColor( std::ostream& os = std::cout );
91  void resetStyle( std::ostream& os = std::cout );
92  void removeStyle( const std::string& style, std::ostream& os = std::cout );
93  void resetFeatures( std::ostream& os = std::cout );
94 
95  //====================================================
96  // Getters
97  //====================================================
98  std::string getColor( std::ostream& os = std::cout );
99  std::string getStyle( std::ostream& os = std::cout );
100  std::unordered_map <std::ostream*, std::string> getColorList();
101  std::unordered_map <std::ostream*, std::string> getStyleList();
102  std::ostream& getCurrentStream();
103 
104  //====================================================
105  // Operators
106  //====================================================
107  const OS_Decorator& operator () ( std::ostream& os = std::cout );
108 
109  private:
110 
111  //====================================================
112  // Attributes
113  //====================================================
114  std::unordered_map <std::ostream*, std::string> colors, styles;
115  std::ostream* current_stream;
116  };
117 
118  //====================================================
119  // Operator <<
120  //====================================================
129  template <typename T>
130  std::ostream& operator << ( OS_Decorator my_shell, const T& elem )
131  {
132  if ( my_shell.getColor( my_shell.getCurrentStream() ) != "" )
133  {
134  my_shell.getCurrentStream() << feat( col, my_shell.getColor( my_shell.getCurrentStream() ) );
135  }
136  if ( my_shell.getStyle( my_shell.getCurrentStream() ) != "" )
137  {
138  std::vector<std::string> list_of_styles = agr::split_string( my_shell.getStyle( my_shell.getCurrentStream() ), " " );
139  for ( auto elem: list_of_styles )
140  {
141  my_shell.getCurrentStream() << feat( sty, elem );
142  }
143  }
144  my_shell.getCurrentStream() << elem << feat( rst, "all" );
145 
146  return my_shell.getCurrentStream();
147  }
148  }
149 
150 #endif
Class used to decorate an output stream. Each setting is set permanently on the chosen output stream ...
Definition: printer.hpp:72
std::unordered_map< std::ostream *, std::string > getStyleList()
Method used to return the map of streams with the respective style.
Definition: printer.cpp:187
void resetStyle(std::ostream &os=std::cout)
Method used to reset the style or of a stream.
Definition: printer.cpp:95
void setColor(const std::string &color, std::ostream &os=std::cout)
Method used to set the color of a stream.
Definition: printer.cpp:51
std::ostream & getCurrentStream()
Method used to return the stream that is used to output stuff.
Definition: printer.cpp:200
std::unordered_map< std::ostream *, std::string > getColorList()
Method used to return the map of streams with the respective color.
Definition: printer.cpp:174
void resetFeatures(std::ostream &os=std::cout)
Method used to reset all the features of a stream.
Definition: printer.cpp:129
std::string getStyle(std::ostream &os=std::cout)
Method used to return the selected style of a stream.
Definition: printer.cpp:161
OS_Decorator()
Default constructor of OS_Decorator class.
Definition: printer.cpp:28
std::string getColor(std::ostream &os=std::cout)
Method used to return the selected color of a stream.
Definition: printer.cpp:146
void resetColor(std::ostream &os=std::cout)
Method used to reset the color of a stream.
Definition: printer.cpp:81
const OS_Decorator & operator()(std::ostream &os=std::cout)
Operator overload to assign the value into parentheses to the "current_stream" variable.
Definition: printer.cpp:214
void removeStyle(const std::string &style, std::ostream &os=std::cout)
Method used to remove one of the set styles (useful in case they are more than one).
Definition: printer.cpp:109
void setStyle(const std::string &style, std::ostream &os=std::cout)
Method used to set the style of a stream.
Definition: printer.cpp:66
~OS_Decorator()
Destructor of OS_Decorator class.
Definition: printer.cpp:37
Definition: canvas.cpp:15
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:90
std::ostream & operator<<(OS_Decorator my_shell, const T &elem)
Operator overload to output a modified ostream object which properties are set thanks to the OS_Decor...
Definition: printer.hpp:130
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:31
void print(std::ostream &os=agr::null_stream, const Args &... args)
It can be used to print messages and strings to the output stream, specifying also the stream you wan...
Definition: printer.hpp:38
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:28