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