REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestStringOutput.h
1 #ifndef RestCore_TRestStringOutput
2 #define RestCore_TRestStringOutput
3 
4 #include <RConfig.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 // #include <unistd.h>
8 
9 #include <cstdio>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <sstream>
14 
15 #include "TRestTools.h"
16 
17 #ifndef WIN32
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 #include <termios.h>
21 #ifndef __APPLE__
22 #include <termio.h>
23 #endif
24 
27 #define COLORCODE_TYPE std::string
28 constexpr const char* const COLOR_RESET = "\033[0m";
29 constexpr const char* const COLOR_BLACK = "\033[30m"; /* Black */
30 constexpr const char* const COLOR_RED = "\033[31m"; /* Red */
31 constexpr const char* const COLOR_GREEN = "\033[32m"; /* Green */
32 constexpr const char* const COLOR_YELLOW = "\033[33m"; /* Yellow */
33 constexpr const char* const COLOR_BLUE = "\033[34m"; /* Blue */
34 constexpr const char* const COLOR_MAGENTA = "\033[35m"; /* Magenta */
35 constexpr const char* const COLOR_CYAN = "\033[36m"; /* Cyan */
36 constexpr const char* const COLOR_WHITE = "\033[37m"; /* White */
37 constexpr const char* const COLOR_BOLDBLACK = "\033[1m\033[30m"; /* Bold Black */
38 constexpr const char* const COLOR_BOLDRED = "\033[1m\033[31m"; /* Bold Red */
39 constexpr const char* const COLOR_BOLDGREEN = "\033[1m\033[32m"; /* Bold Green */
40 constexpr const char* const COLOR_BOLDYELLOW = "\033[1m\033[33m"; /* Bold Yellow */
41 constexpr const char* const COLOR_BOLDBLUE = "\033[1m\033[34m"; /* Bold Blue */
42 constexpr const char* const COLOR_BOLDMAGENTA = "\033[1m\033[35m"; /* Bold Magenta */
43 constexpr const char* const COLOR_BOLDCYAN = "\033[1m\033[36m"; /* Bold Cyan */
44 constexpr const char* const COLOR_BOLDWHITE = "\033[1m\033[37m"; /* Bold White */
45 constexpr const char* const COLOR_BACKGROUNDBLACK = "\033[1m\033[40m"; /* BACKGROUND Black */
46 constexpr const char* const COLOR_BACKGROUNDRED = "\033[1m\033[41m"; /* BACKGROUND Red */
47 constexpr const char* const COLOR_BACKGROUNDGREEN = "\033[1m\033[42m"; /* BACKGROUND Green */
48 constexpr const char* const COLOR_BACKGROUNDYELLOW = "\033[1m\033[43m"; /* BACKGROUND Yellow */
49 constexpr const char* const COLOR_BACKGROUNDBLUE = "\033[1m\033[44m"; /* BACKGROUND Blue */
50 constexpr const char* const COLOR_BACKGROUNDMAGENTA = "\033[1m\033[45m"; /* BACKGROUND Magenta */
51 constexpr const char* const COLOR_BACKGROUNDCYAN = "\033[1m\033[46m"; /* BACKGROUND Cyan */
52 constexpr const char* const COLOR_BACKGROUNDWHITE = "\033[1m\033[47m"; /* BACKGROUND White */
53 
54 #else
55 #define COLORCODE_TYPE int
56 EXTERN_DEF int COLOR_RESET;
57 
58 constexpr int COLOR_BLACK = 0;
59 constexpr int COLOR_RED = 4;
60 constexpr int COLOR_GREEN = 2;
61 constexpr int COLOR_YELLOW = 14;
62 constexpr int COLOR_BLUE = 1;
63 constexpr int COLOR_MAGENTA = 5;
64 constexpr int COLOR_CYAN = 9;
65 constexpr int COLOR_WHITE = 7;
66 
67 // we don't use font setting for windows
68 constexpr int COLOR_BOLDBLACK = COLOR_BLACK;
69 constexpr int COLOR_BOLDRED = COLOR_RED;
70 constexpr int COLOR_BOLDGREEN = COLOR_GREEN;
71 constexpr int COLOR_BOLDYELLOW = COLOR_YELLOW;
72 constexpr int COLOR_BOLDBLUE = COLOR_BLUE;
73 constexpr int COLOR_BOLDMAGENTA = COLOR_MAGENTA;
74 constexpr int COLOR_BOLDCYAN = COLOR_CYAN;
75 constexpr int COLOR_BOLDWHITE = COLOR_WHITE;
76 
77 #endif // WIN32
78 
91 class Console {
92  public:
94  static int GetWidth();
96  static int GetHeight();
98  static bool kbhit();
100  static int Read();
102  static int ReadKey();
104  static std::string ReadLine();
106  static void WriteLine(std::string content);
108  static void CursorUp(int n);
110  static void CursorDown(int n);
112  static void CursorRight(int n);
114  static void CursorLeft(int n);
116  static void CursorToXY(int x, int y);
118  static void ClearScreen();
120  static void ClearCurrentLine();
122  static void ClearLinesAfterCursor();
123 };
124 
132 class TRestMetadata;
133 struct endl_t {
134  endl_t(TRestMetadata* ptr) { TRestMetadataPtr = ptr; }
135  TRestMetadata* TRestMetadataPtr = nullptr;
136  friend std::ostream& operator<<(std::ostream& a, endl_t& et) { return (a << std::endl); }
137 };
138 
149  public:
152  enum class REST_Verbose_Level {
153  REST_Silent = 0,
154  REST_Essential = 1,
155  REST_Warning = REST_Essential,
156  REST_Info = 2,
157  REST_Debug = 3,
158  REST_Extreme = 4
159  };
160 
163  enum class REST_Display_Orientation { kLeft = 1, kMiddle = 0 };
164 
165  protected:
166  COLORCODE_TYPE color;
167  std::string formatstring;
168  bool useborder;
169  bool iserror;
170  REST_Display_Orientation orientation; // 0->middle, 1->left
171 
172  std::stringstream buf;
173  int length;
174 
175  REST_Verbose_Level verbose;
176 
177  public:
178  REST_Verbose_Level GetVerboseLevel() { return verbose; }
179  std::string GetBuffer() { return buf.str(); }
180  bool isError() { return iserror; }
181  std::string FormattingPrintString(std::string input);
182  void resetstring();
183  void flushstring();
184  void setcolor(COLORCODE_TYPE colordef) { color = colordef; }
185  void setheader(std::string headerdef) {
186  formatstring = headerdef;
187  useborder = false;
188  }
189  void resetcolor() { color = COLOR_RESET; }
190  void resetheader() { formatstring = ""; }
191  void setborder(std::string b) {
192  formatstring = b;
193  useborder = true;
194  }
195  void resetborder() { formatstring = ""; }
196  void setlength(int n);
197  void setorientation(REST_Display_Orientation o) { orientation = o; }
198  void resetorientation() { orientation = REST_Display_Orientation::kMiddle; }
199  static void RESTendl(TRestStringOutput& input) { input.flushstring(); }
200 
201  // If formatter is in mirror form(e.g., "|| ||","< >"), it will use such border
202  // to wrap the std::string to be displayed. otherwise the formatter is used as
203  // prefix(e.g., "-- Warning: ")
205  COLORCODE_TYPE color = COLOR_RESET, std::string formatter = "",
206  REST_Display_Orientation orientation = TRestStringOutput::REST_Display_Orientation::kLeft);
207 
209  REST_Verbose_Level v, COLORCODE_TYPE _color = COLOR_RESET, std::string formatter = "",
210  REST_Display_Orientation orientation = TRestStringOutput::REST_Display_Orientation::kLeft,
211  bool _iserror = false)
212  : TRestStringOutput(_color, formatter, orientation) {
213  verbose = v;
214  iserror = _iserror;
215  }
216 
217  template <class T>
218  TRestStringOutput& operator<<(T const& content) {
219  buf << content;
220  return *this;
221  }
222 
223  TRestStringOutput& operator<<(void (*pfunc)(TRestStringOutput&));
224  friend TRestStringOutput& operator<<(TRestMetadata& mt, TRestStringOutput& so);
225  TRestStringOutput& operator<<(endl_t et);
226 };
227 
230 inline void PrintWelcome() { system("rest-config --welcome"); }
231 
233 // initialize formatted message output tool
234 // initialize formatted message output tool
236  "[== ==]", TRestStringOutput::REST_Display_Orientation::kMiddle);
238  "-- Error : ", TRestStringOutput::REST_Display_Orientation::kLeft, true);
239 static TRestStringOutput RESTWarning(TRestStringOutput::REST_Verbose_Level::REST_Warning, COLOR_BOLDYELLOW,
240  "-- Warning : ", TRestStringOutput::REST_Display_Orientation::kLeft,
241  true);
243  "", TRestStringOutput::REST_Display_Orientation::kMiddle);
245  "|| ||", TRestStringOutput::REST_Display_Orientation::kMiddle);
247  "-- Info : ", TRestStringOutput::REST_Display_Orientation::kLeft);
249  "-- Success : ", TRestStringOutput::REST_Display_Orientation::kLeft);
251  "-- Debug : ", TRestStringOutput::REST_Display_Orientation::kLeft);
253  "-- Extreme : ", TRestStringOutput::REST_Display_Orientation::kLeft);
254 
255 static void RESTendl(TRestStringOutput& input) { input.flushstring(); }
256 
257 EXTERN_DEF TRestStringOutput::REST_Verbose_Level gVerbose;
259 EXTERN_DEF bool REST_Display_CompatibilityMode;
260 #endif
A base class for any REST metadata class.
Definition: TRestMetadata.h:74
REST_Display_Orientation
Enumerate of TRestStringOutput display orientation.
REST_Verbose_Level
Enumerate of verbose level, containing five levels.
@ REST_Essential
+show some essential information, as well as warnings
@ REST_Info
+show most of the information for each steps
@ REST_Debug
+show the defined debug messages
@ REST_Silent
show minimum information of the software, as well as error messages