knowL: Knowledge Libraries
Loading...
Searching...
No Matches
IOFormat.h
1#include <unistd.h>
2#include <stdio.h>
3#include <iostream>
4
5// Source for number https://en.wikipedia.org/wiki/ANSI_escape_code
6namespace IOFormat
7{
8 namespace details
9 {
10 inline bool is_atty(std::ostream &s)
11 {
12 return ( (s.rdbuf() == std::cout.rdbuf() and isatty(fileno(stdout)))
13 or (s.rdbuf() == std::cerr.rdbuf() and isatty(fileno(stderr))));
14 }
15 }
16
17 inline std::ostream& bold(std::ostream &s)
18 {
19 if (details::is_atty(s))
20 {
21 s << "\033[1m";
22 }
23 return s;
24 }
25
26 inline std::ostream& default_color(std::ostream &s)
27 {
28 if (details::is_atty(s))
29 {
30 s << "\033[39m";
31 }
32 return s;
33 }
34
35 inline std::ostream& red(std::ostream &s)
36 {
37 if (details::is_atty(s))
38 {
39 s << "\033[31m";
40 }
41 return s;
42 }
43
44 inline std::ostream& green(std::ostream &s)
45 {
46 if (details::is_atty(s))
47 {
48 s << "\033[32m";
49 }
50 return s;
51 }
52
53 inline std::ostream& yellow(std::ostream &s)
54 {
55 if (details::is_atty(s))
56 {
57 s << "\033[33m";
58 }
59 return s;
60 }
61
62 inline std::ostream& blue(std::ostream &s)
63 {
64 if (details::is_atty(s))
65 {
66 s << "\033[34m";
67 }
68 return s;
69 }
70
71 inline std::ostream& reset(std::ostream &s)
72 {
73 if (details::is_atty(s))
74 {
75 s << "\033[0m";
76 }
77 return s;
78 }
79
80}
81
82#ifdef QDEBUG_H
83
84inline QDebug operator<<(QDebug _stream, QDebug (*_f)(QDebug)) { return (*_f)(_stream); }
85
86namespace IOFormat
87{
88 namespace details
89 {
90 inline bool is_atty(QDebug s)
91 {
92 Q_UNUSED(s);
93 return isatty(fileno(stderr));
94 }
95 }
96
97 inline QDebug bold(QDebug s)
98 {
99 if (details::is_atty(s))
100 {
101 s << "\033[1m";
102 }
103 return s;
104 }
105
106 inline QDebug red(QDebug s)
107 {
108 if (details::is_atty(s))
109 {
110 s << "\033[31m";
111 }
112 return s;
113 }
114
115 inline QDebug green(QDebug s)
116 {
117 if (details::is_atty(s))
118 {
119 s << "\033[32m";
120 }
121 return s;
122 }
123
124 inline QDebug yellow(QDebug s)
125 {
126 if (details::is_atty(s))
127 {
128 s << "\033[33m";
129 }
130 return s;
131 }
132
133 inline QDebug blue(QDebug s)
134 {
135 if (details::is_atty(s))
136 {
137 s << "\033[34m";
138 }
139 return s;
140 }
141
142 inline QDebug reset(QDebug s)
143 {
144 if (details::is_atty(s))
145 {
146 s << "\033[0m";
147 }
148 return s;
149 }
150
151}
152
153#endif