fastdo  0.5.12
console.hpp
浏览该文件的文档.
1 #ifndef __CONSOLE_HPP__
2 #define __CONSOLE_HPP__
3 
4 #include <iostream>
5 
6 namespace winux
7 {
8 
11 {
12 #if defined(OS_WIN)
13  fgBlack = 0,
14  bgBlack = 0,
15 
16  fgNavy = 0x0001,
17  fgAtrovirens = 0x0002,
19  fgMaroon = 0x0004,
23 
24  fgIntensity = 0x0008,
26 
27  fgBlue = fgIntensity | fgNavy,
28  fgGreen = fgIntensity | fgAtrovirens,
29  fgAqua = fgIntensity | fgNavy | fgAtrovirens,
30  fgRed = fgIntensity | fgMaroon,
31  fgFuchsia = fgIntensity | fgNavy | fgMaroon,
32  fgYellow = fgIntensity | fgAtrovirens | fgMaroon,
33  fgWhite = fgIntensity | fgNavy | fgAtrovirens | fgMaroon,
34 
36  bgNavy = 0x0010,
37  bgAtrovirens = 0x0020,
38  bgTeal = bgNavy | bgAtrovirens,
39  bgMaroon = 0x0040,
40  bgPurple = bgNavy | bgMaroon,
42  bgSilver = bgNavy | bgAtrovirens | bgMaroon,
43 
44  bgIntensity = 0x0080,
45  bgGray = bgIntensity,
46 
47  bgBlue = bgIntensity | bgNavy,
48  bgGreen = bgIntensity | bgAtrovirens,
49  bgAqua = bgIntensity | bgNavy | bgAtrovirens,
50  bgRed = bgIntensity | bgMaroon,
51  bgFuchsia = bgIntensity | bgNavy | bgMaroon,
52  bgYellow = bgIntensity | bgAtrovirens | bgMaroon,
53  bgWhite = bgIntensity | bgNavy | bgAtrovirens | bgMaroon,
54 #else
55  fgBlack = 0,
56  fgNavy = 1,
58  fgTeal = 3,
59  fgMaroon = 4,
60  fgPurple = 5,
61  fgOlive = 6,
62  fgSilver = 7,
63  fgGray = 8,
64  fgIntensity = fgGray,
65  fgBlue = 9,
66  fgGreen = 10,
67  fgAqua = 11,
68  fgRed = 12,
69  fgFuchsia = 13,
70  fgYellow = 14,
71  fgWhite = 15,
72 
73  bgNavy = 0x0100,
74  bgAtrovirens = 0x0200,
75  bgTeal = 0x0300,
76  bgMaroon = 0x0400,
77  bgPurple = 0x0500,
78  bgOlive = 0x0600,
79  bgSilver = 0x0700,
80  bgBlack = 0x0800,
81  bgWhite = 0x0000,
83  bgBlue = bgNavy,
84  bgGreen = bgAtrovirens,
85  bgAqua = bgTeal,
86  bgRed = bgMaroon,
87  bgFuchsia = bgPurple,
88  bgYellow = bgOlive,
89 #endif
90 
91 };
92 
93 #if defined(OS_WIN)
94 #else
95 extern WINUX_DLL char const * __TerminalFgColorAttrs[];
96 extern WINUX_DLL char const * __TerminalBgColorAttrs[];
97 
98 #endif
99 
101 {
102 private:
103 #if defined(OS_WIN)
104  WORD _wPrevAttributes;
105  WORD _wAttributes;
106  HANDLE _hStdHandle;
107 #else
108  winux::String _strAttr;
109 #endif
110  bool _isSetBgColor;
111 public:
112  ConsoleAttr( winux::ushort attr, bool isSetBgColor = false );
113 
114  void modify() const;
115 
116  void resume() const;
117 };
118 
119 template < typename _VarType >
120 class ConsoleAttrT : public ConsoleAttr
121 {
122 private:
123  _VarType & _v;
124 public:
125  ConsoleAttrT( winux::ushort attr, _VarType const & v, bool isSetBgColor = false ) : ConsoleAttr( attr, isSetBgColor ), _v( const_cast<_VarType&>(v) )
126  {
127  }
128 
129  _VarType & val() const { return _v; }
130 
131 };
132 
133 template < typename _VarType >
134 inline std::ostream & operator << ( std::ostream & o, ConsoleAttrT<_VarType> const & tr )
135 {
136  tr.modify();
137  o << tr.val();
138  tr.resume();
139  return o;
140 }
141 
142 template < typename _VarType >
143 inline std::istream & operator >> ( std::istream & in, ConsoleAttrT<_VarType> const & tr )
144 {
145  tr.modify();
146  in >> tr.val();
147  tr.resume();
148  return in;
149 }
150 
151 template < typename _VarType >
152 inline ConsoleAttrT<_VarType> ConsoleColor( winux::ushort attr, _VarType const & v, bool isSetBgColor = false )
153 {
154  return ConsoleAttrT<_VarType>( attr, v, isSetBgColor );
155 }
156 
158 {
159 public:
162 private:
164 };
165 
166 inline static void OutputV()
167 {
168 }
169 
170 template < typename _Ty, typename... _ArgType >
171 inline static void OutputV( _Ty&& a, _ArgType&& ... arg )
172 {
173  std::cout << a;
174  OutputV( std::forward<_ArgType>(arg)... );
175 }
176 
177 template < typename... _ArgType >
178 inline static void ColorOutput( winux::ConsoleAttr const & ca, _ArgType&& ... arg )
179 {
180  //ScopeGuard guard(__mtxConsoleOutput);
182  ca.modify();
183  OutputV( std::forward<_ArgType>(arg)... );
184  ca.resume();
185  std::cout << std::endl;
186 }
187 
188 } // namespace winux
189 
190 #endif // __CONSOLE_HPP__
ConsoleAttrT(winux::ushort attr, _VarType const &v, bool isSetBgColor=false)
Definition: console.hpp:125
WINUX_DLL char const * __TerminalBgColorAttrs[]
#define WINUX_DLL
Definition: utilities.hpp:57
static void OutputV()
Definition: console.hpp:166
void resume() const
static void ColorOutput(winux::ConsoleAttr const &ca, _ArgType &&...arg)
Definition: console.hpp:178
std::istream & operator>>(std::istream &in, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:143
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:78
ConsoleColorAttrFlags
颜色属性标记
Definition: console.hpp:10
ConsoleAttrT< _VarType > ConsoleColor(winux::ushort attr, _VarType const &v, bool isSetBgColor=false)
Definition: console.hpp:152
void modify() const
_VarType & val() const
Definition: console.hpp:129
unsigned short ushort
Definition: utilities.hpp:131
WINUX_DLL char const * __TerminalFgColorAttrs[]
std::basic_string< tchar > String
Definition: utilities.hpp:162
跨平台基础功能库
Definition: archives.hpp:7