1 // -*- C++ -*-
2 /**
3 * @brief This file provides definitions for tty control sequences
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include "openturns/TTY.hxx"
23
24 BEGIN_NAMESPACE_OPENTURNS
25
26 #ifndef _WIN32
27 static volatile Bool Colored = true; // default is colored output
28 #else
29 static volatile Bool Colored = false; // default is standard output
30 #endif
31
32 static const char * ColorMap[] =
33 {
34 /* ISO 6429 color sequences */
35 "\033[0m", /* DEFAULT */
36 "\033[1m", /* BOLD */
37 "\033[4m", /* UNDERLINE */
38 "\033[5m", /* BLINK */
39
40 /* Foreground */
41 "\033[30m", /* BLACKFG */
42 "\033[31m", /* REDFG */
43 "\033[32m", /* GREENFG */
44 "\033[33m", /* YELLOWFG */
45 "\033[34m", /* BLUEFG */
46 "\033[35m", /* PURPLEFG */
47 "\033[36m", /* CYANFG */
48 "\033[37m", /* WHITEFG */
49
50 /* Background */
51 "\033[30m", /* BLACKBG */
52 "\033[31m", /* REDBG */
53 "\033[32m", /* GREENBG */
54 "\033[33m", /* YELLOWBG */
55 "\033[34m", /* BLUEBG */
56 "\033[35m", /* PURPLEBG */
57 "\033[36m", /* CYANBG */
58 "\033[37m", /* WHITEBG */
59 };
60
61 /* Return a control sequence corresponding to the current color */
GetColor(Color c)62 const char * TTY::GetColor( Color c )
63 {
64 if ( !Colored || (c < 0) || (c >= LASTCOLOR) ) return "";
65 return ColorMap[ c ];
66 }
67
68 /* Define the colorization strategy. Flag = true if colored output */
69 /* Q: Do we need to perform MT-safe programming ? Not sure. Ivan */
ShowColors(Bool flag)70 void TTY::ShowColors( Bool flag )
71 {
72 Colored = flag;
73 }
74
75 /* Query the colorization strategy. Return true if colored output */
ColoredOutput()76 Bool TTY::ColoredOutput()
77 {
78 return Colored;
79 }
80 END_NAMESPACE_OPENTURNS
81