Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
Console.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
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/**
34 @file Console.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37*/
38
39
40// OpenFLUID:stylecheck:!inco
41// OpenFLUID:stylecheck:!incs
42
43
44#ifndef __OPENFLUID_TOOLS_CONSOLE_HPP__
45#define __OPENFLUID_TOOLS_CONSOLE_HPP__
46
47
48#include <string>
49#include <iostream>
50
51#include <openfluid/global.hpp>
52
53#if defined(OPENFLUID_OS_WINDOWS)
54#include <windows.h>
55#endif
56
57
58namespace openfluid { namespace tools {
59
60
62{
63 public:
64
65 enum class Colors : char {
66 BLACK = 0,
67 BLUE = 1,
68 GREEN = 2,
69 CYAN = 3,
70 RED = 4,
71 MAGENTA = 5,
72 BROWN = 6,
73 GREY = 7,
74 DARKGREY = 8,
75 LIGHTBLUE = 9,
76 LIGHTGREEN = 10,
77 LIGHTCYAN = 11,
78 LIGHTRED = 12,
79 LIGHTMAGENTA = 13,
80 YELLOW = 14,
81 WHITE = 15
82 };
83
84
85 /**
86 Returns corresponding ANSI code for given color
87 @param[in] Color the given color
88 @return the color code as string
89 */
90 static std::string getANSIColorCode(const Colors& Color)
91 {
92 switch (Color)
93 {
94 case Colors::BLACK : return "\033[22;30m";
95 case Colors::RED : return "\033[22;31m";
96 case Colors::GREEN : return "\033[22;32m";
97 case Colors::BROWN : return "\033[22;33m";
98 case Colors::BLUE : return "\033[22;34m";
99 case Colors::MAGENTA : return "\033[22;35m";
100 case Colors::CYAN : return "\033[22;36m";
101 case Colors::GREY : return "\033[22;37m";
102 case Colors::DARKGREY : return "\033[01;30m";
103 case Colors::LIGHTRED : return "\033[01;31m";
104 case Colors::LIGHTGREEN : return "\033[01;32m";
105 case Colors::YELLOW : return "\033[01;33m";
106 case Colors::LIGHTBLUE : return "\033[01;34m";
107 case Colors::LIGHTMAGENTA: return "\033[01;35m";
108 case Colors::LIGHTCYAN : return "\033[01;36m";
109 case Colors::WHITE : return "\033[01;37m";
110 default : return "";
111 }
112 }
113
114
115 /**
116 Saves font attributes (color and weight)
117 @return the color code (-1 on non-Windows systems)
118 */
119 static int saveAttributes()
120 {
121#if defined(OPENFLUID_OS_WINDOWS)
122 static bool Initialized = false;
123 static WORD Attributes;
124
125 if (!Initialized)
126 {
127 CONSOLE_SCREEN_BUFFER_INFO Info;
128 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info);
129 Attributes = Info.wAttributes;
130 Initialized = 1;
131 }
132 return (int)Attributes;
133#else
134 return -1;
135#endif
136 }
137
138 /**
139 Resets font attributes (color and weight)
140 */
141 static void resetAttributes()
142 {
143#if defined(OPENFLUID_OS_WINDOWS)
144 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)saveAttributes());
145#else
146 std::cout << "\033[0m";
147#endif
148 }
149
150 /**
151 Sets the given color for console text
152 @param[in] Color the given color
153 */
154 static void setColor(Colors Color)
155 {
156#if defined(OPENFLUID_OS_WINDOWS)
157 HANDLE HStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
158 CONSOLE_SCREEN_BUFFER_INFO Info;
159
160 GetConsoleScreenBufferInfo(HStdOut,&Info);
161
162 SetConsoleTextAttribute(HStdOut,(Info.wAttributes & 0xFFF0) |(WORD)Color);
163#else
164 std::cout << getANSIColorCode(Color);
165#endif
166 }
167
168 /**
169 Sets the predefined OK color for console text
170 */
171 static void setOKColor()
172 {
173#if defined(OPENFLUID_OS_WINDOWS)
175#else
177#endif
178 }
179
180 /**
181 Sets the predefined warning color for console text
182 */
183 static void setWarningColor()
184 {
185#if defined(OPENFLUID_OS_WINDOWS)
187#else
188 std::cout << getANSIColorCode(Colors::YELLOW);
189#endif
190 }
191
192 /**
193 Sets the predefined error color for console text
194 */
195 static void setErrorColor()
196 {
197#if defined(OPENFLUID_OS_WINDOWS)
199#else
201#endif
202 }
203};
204
205
206} } // namespaces
207
208
209#endif /* __OPENFLUID_TOOLS_CONSOLE_HPP__ */
Definition Console.hpp:62
static std::string getANSIColorCode(const Colors &Color)
Definition Console.hpp:90
static int saveAttributes()
Definition Console.hpp:119
static void resetAttributes()
Definition Console.hpp:141
Colors
Definition Console.hpp:65
static void setErrorColor()
Definition Console.hpp:195
static void setColor(Colors Color)
Definition Console.hpp:154
static void setOKColor()
Definition Console.hpp:171
static void setWarningColor()
Definition Console.hpp:183
Definition ApplicationException.hpp:47