Console.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file Console.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
36 */
37 
38 
39 #ifndef __OPENFLUID_TOOLS_CONSOLE_HPP__
40 #define __OPENFLUID_TOOLS_CONSOLE_HPP__
41 
42 
43 #include <string>
44 #include <iostream>
45 
46 #include <openfluid/global.hpp>
47 
48 
49 #if defined(OPENFLUID_OS_WINDOWS)
50 #include <windows.h>
51 #endif
52 
53 
54 namespace openfluid { namespace tools {
55 
56 
57 class Console
58 {
59  public:
60 
61  enum class Colors : char {
62  BLACK = 0,
63  BLUE = 1,
64  GREEN = 2,
65  CYAN = 3,
66  RED = 4,
67  MAGENTA = 5,
68  BROWN = 6,
69  GREY = 7,
70  DARKGREY = 8,
71  LIGHTBLUE = 9,
72  LIGHTGREEN = 10,
73  LIGHTCYAN = 11,
74  LIGHTRED = 12,
75  LIGHTMAGENTA = 13,
76  YELLOW = 14,
77  WHITE = 15
78  };
79 
80 
81  /**
82  Returns corresponding ANSI code for given color
83  @param[in] Color the given color
84  @return the color code as string
85  */
86  static std::string getANSIColorCode(const Colors& Color)
87  {
88  switch (Color)
89  {
90  case Colors::BLACK : return "\033[22;30m";
91  case Colors::RED : return "\033[22;31m";
92  case Colors::GREEN : return "\033[22;32m";
93  case Colors::BROWN : return "\033[22;33m";
94  case Colors::BLUE : return "\033[22;34m";
95  case Colors::MAGENTA : return "\033[22;35m";
96  case Colors::CYAN : return "\033[22;36m";
97  case Colors::GREY : return "\033[22;37m";
98  case Colors::DARKGREY : return "\033[01;30m";
99  case Colors::LIGHTRED : return "\033[01;31m";
100  case Colors::LIGHTGREEN : return "\033[01;32m";
101  case Colors::YELLOW : return "\033[01;33m";
102  case Colors::LIGHTBLUE : return "\033[01;34m";
103  case Colors::LIGHTMAGENTA: return "\033[01;35m";
104  case Colors::LIGHTCYAN : return "\033[01;36m";
105  case Colors::WHITE : return "\033[01;37m";
106  default : return "";
107  }
108  }
109 
110 
111  /**
112  Saves font attributes (color and weight)
113  @return the color code (-1 on non-Windows systems)
114  */
115  static int saveAttributes()
116  {
117 #if defined(OPENFLUID_OS_WINDOWS)
118  static bool Initialized = false;
119  static WORD Attributes;
120 
121  if (!Initialized)
122  {
123  CONSOLE_SCREEN_BUFFER_INFO Info;
124  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info);
125  Attributes = Info.wAttributes;
126  Initialized = 1;
127  }
128  return (int)Attributes;
129 #else
130  return -1;
131 #endif
132  }
133 
134  /**
135  Resets font attributes (color and weight)
136  */
137  static void resetAttributes()
138  {
139 #if defined(OPENFLUID_OS_WINDOWS)
140  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)saveAttributes());
141 #else
142  std::cout << "\033[0m";
143 #endif
144  }
145 
146  /**
147  Sets the given color for console text
148  @param[in] Color the given color
149  */
150  static void setColor(Colors Color)
151  {
152 #if defined(OPENFLUID_OS_WINDOWS)
153  HANDLE HStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
154  CONSOLE_SCREEN_BUFFER_INFO Info;
155 
156  GetConsoleScreenBufferInfo(HStdOut,&Info);
157 
158  SetConsoleTextAttribute(HStdOut,(Info.wAttributes & 0xFFF0) |(WORD)Color);
159 #else
160  std::cout << getANSIColorCode(Color);
161 #endif
162  }
163 
164  /**
165  Sets the predefined OK color for console text
166  */
167  static void setOKColor()
168  {
169 #if defined(OPENFLUID_OS_WINDOWS)
171 #else
172  std::cout << getANSIColorCode(Colors::LIGHTGREEN);
173 #endif
174  }
175 
176  /**
177  Sets the predefined warning color for console text
178  */
179  static void setWarningColor()
180  {
181 #if defined(OPENFLUID_OS_WINDOWS)
183 #else
184  std::cout << getANSIColorCode(Colors::YELLOW);
185 #endif
186  }
187 
188  /**
189  Sets the predefined error color for console text
190  */
191  static void setErrorColor()
192  {
193 #if defined(OPENFLUID_OS_WINDOWS)
195 #else
196  std::cout << getANSIColorCode(Colors::LIGHTRED);
197 #endif
198  }
199 };
200 
201 
202 } } // namespaces
203 
204 
205 #endif /* __OPENFLUID_TOOLS_CONSOLE_HPP__ */
Definition: Console.hpp:57
Colors
Definition: Console.hpp:61
static void setWarningColor()
Definition: Console.hpp:179
static int saveAttributes()
Definition: Console.hpp:115
static void setOKColor()
Definition: Console.hpp:167
static void setErrorColor()
Definition: Console.hpp:191
static void resetAttributes()
Definition: Console.hpp:137
Definition: ApplicationException.hpp:47
static void setColor(Colors Color)
Definition: Console.hpp:150
static std::string getANSIColorCode(const Colors &Color)
Definition: Console.hpp:86