Terminal.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Terminal_H
21 #define MIRTK_Terminal_H
22 
23 #include "mirtk/CommonExport.h"
24 
25 #include "mirtk/Config.h" // WINDOWS
26 #include "mirtk/Stream.h"
27 
28 #include <cstdio>
29 #ifndef WINDOWS
30 # include <unistd.h>
31 #endif
32 
33 
34 namespace mirtk {
35 
36 
37 // =============================================================================
38 // Global terminal options
39 // =============================================================================
40 
41 /// Whether to use color output to STDOUT
42 MIRTK_Common_EXPORT extern bool stdout_color;
43 
44 // =============================================================================
45 // Command help
46 // =============================================================================
47 
48 /// Check if given option is a terminal option
49 bool IsTerminalOption(const char *);
50 
51 /// Parse terminal option
52 void ParseTerminalOption(int &, int &, char *[]);
53 
54 /// Print terminal command-line options
55 void PrintTerminalOptions(ostream &);
56 
57 // =============================================================================
58 // Auxiliary functions
59 // =============================================================================
60 
61 // -----------------------------------------------------------------------------
62 inline bool StdOutIsRedirected()
63 {
64 #ifdef WINDOWS
65  return false;
66 #else
67  return !isatty(fileno(stdout));
68 #endif
69 }
70 
71 // =============================================================================
72 // Terminal colors
73 // =============================================================================
74 
75 // -----------------------------------------------------------------------------
76 /// Color escape sequences
77 MIRTK_Common_EXPORT extern const char *xreset;
78 
79 MIRTK_Common_EXPORT extern const char *xblack;
80 MIRTK_Common_EXPORT extern const char *xred;
81 MIRTK_Common_EXPORT extern const char *xgreen;
82 MIRTK_Common_EXPORT extern const char *xyellow;
83 MIRTK_Common_EXPORT extern const char *xblue;
84 MIRTK_Common_EXPORT extern const char *xmagenta;
85 MIRTK_Common_EXPORT extern const char *xcyan;
86 MIRTK_Common_EXPORT extern const char *xwhite;
87 
88 MIRTK_Common_EXPORT extern const char *xbrightblack;
89 MIRTK_Common_EXPORT extern const char *xbrightred;
90 MIRTK_Common_EXPORT extern const char *xbrightgreen;
91 MIRTK_Common_EXPORT extern const char *xbrightyellow;
92 MIRTK_Common_EXPORT extern const char *xbrightblue;
93 MIRTK_Common_EXPORT extern const char *xbrightmagenta;
94 MIRTK_Common_EXPORT extern const char *xbrightcyan;
95 MIRTK_Common_EXPORT extern const char *xbrightwhite;
96 
97 MIRTK_Common_EXPORT extern const char *xboldblack;
98 MIRTK_Common_EXPORT extern const char *xboldred;
99 MIRTK_Common_EXPORT extern const char *xboldgreen;
100 MIRTK_Common_EXPORT extern const char *xboldyellow;
101 MIRTK_Common_EXPORT extern const char *xboldblue;
102 MIRTK_Common_EXPORT extern const char *xboldmagenta;
103 MIRTK_Common_EXPORT extern const char *xboldcyan;
104 MIRTK_Common_EXPORT extern const char *xboldwhite;
105 MIRTK_Common_EXPORT extern const char *xboldbrightblack;
106 MIRTK_Common_EXPORT extern const char *xboldbrightred;
107 MIRTK_Common_EXPORT extern const char *xboldbrightgreen;
108 MIRTK_Common_EXPORT extern const char *xboldbrightyellow;
109 MIRTK_Common_EXPORT extern const char *xboldbrightblue;
110 MIRTK_Common_EXPORT extern const char *xboldbrightmagenta;
111 MIRTK_Common_EXPORT extern const char *xboldbrightcyan;
112 MIRTK_Common_EXPORT extern const char *xboldbrightwhite;
113 
114 
115 } // namespace mirtk
116 
117 #endif // MIRTK_Terminal_H
MIRTK_Common_EXPORT bool stdout_color
Whether to use color output to STDOUT.
bool IsTerminalOption(const char *)
Check if given option is a terminal option.
Definition: IOConfig.h:41
void PrintTerminalOptions(ostream &)
Print terminal command-line options.
void ParseTerminalOption(int &, int &, char *[])
Parse terminal option.
MIRTK_Common_EXPORT const char * xreset
Color escape sequences.