fastdo  0.6.16
console.hpp
浏览该文件的文档.
1 #ifndef __CONSOLE_HPP__
2 #define __CONSOLE_HPP__
3 //
4 // console 提供控制台相关的功能,例如彩色文字输出
5 //
6 
7 #include <iostream>
8 
9 namespace winux
10 {
13 {
14 #if defined(OS_WIN)
15  fgBlack = 0,
16  bgBlack = 0,
17 
18  fgNavy = 0x0001,
19  fgAtrovirens = 0x0002,
21  fgMaroon = 0x0004,
25 
26  fgIntensity = 0x0008,
28 
29  fgBlue = fgIntensity | fgNavy,
30  fgGreen = fgIntensity | fgAtrovirens,
31  fgAqua = fgIntensity | fgNavy | fgAtrovirens,
32  fgRed = fgIntensity | fgMaroon,
33  fgFuchsia = fgIntensity | fgNavy | fgMaroon,
34  fgYellow = fgIntensity | fgAtrovirens | fgMaroon,
35  fgWhite = fgIntensity | fgNavy | fgAtrovirens | fgMaroon,
36 
38  bgNavy = 0x0010,
39  bgAtrovirens = 0x0020,
40  bgTeal = bgNavy | bgAtrovirens,
41  bgMaroon = 0x0040,
42  bgPurple = bgNavy | bgMaroon,
44  bgSilver = bgNavy | bgAtrovirens | bgMaroon,
45 
46  bgIntensity = 0x0080,
47  bgGray = bgIntensity,
48 
49  bgBlue = bgIntensity | bgNavy,
50  bgGreen = bgIntensity | bgAtrovirens,
51  bgAqua = bgIntensity | bgNavy | bgAtrovirens,
52  bgRed = bgIntensity | bgMaroon,
53  bgFuchsia = bgIntensity | bgNavy | bgMaroon,
54  bgYellow = bgIntensity | bgAtrovirens | bgMaroon,
55  bgWhite = bgIntensity | bgNavy | bgAtrovirens | bgMaroon,
56 #else
57  fgBlack = 0,
58  fgNavy = 1,
60  fgTeal = 3,
61  fgMaroon = 4,
62  fgPurple = 5,
63  fgOlive = 6,
64  fgSilver = 7,
65  fgGray = 8,
66  fgIntensity = fgGray,
67  fgBlue = 9,
68  fgGreen = 10,
69  fgAqua = 11,
70  fgRed = 12,
71  fgFuchsia = 13,
72  fgYellow = 14,
73  fgWhite = 15,
74 
75  bgNavy = 0x0100,
76  bgAtrovirens = 0x0200,
77  bgTeal = 0x0300,
78  bgMaroon = 0x0400,
79  bgPurple = 0x0500,
80  bgOlive = 0x0600,
81  bgSilver = 0x0700,
82  bgBlack = 0x0800,
83  bgWhite = 0x0000,
85  bgBlue = bgNavy,
86  bgGreen = bgAtrovirens,
87  bgAqua = bgTeal,
88  bgRed = bgMaroon,
89  bgFuchsia = bgPurple,
90  bgYellow = bgOlive,
91 #endif
92  ccaIgnore = 0x8000,
93 };
94 
97 {
98 public:
99  ConsoleAttr( winux::ushort attr, bool isSetBgColor = false );
100 
101  void modify() const;
102  void resume() const;
103 
104 private:
105 #if defined(OS_WIN)
106  WORD _wPrevAttributes;
107  WORD _wAttributes;
108  HANDLE _hStdHandle;
109 #else
110  winux::AnsiString _strAttr;
111 #endif
112  bool _isSetBgColor;
113 };
114 
116 template < typename _VarType >
117 class ConsoleAttrT : public ConsoleAttr
118 {
119 public:
120  ConsoleAttrT( winux::ushort attr, _VarType const & v, bool isSetBgColor = false ) : ConsoleAttr( attr, isSetBgColor ), _v( const_cast<_VarType&>(v) )
121  {
122  }
123 
124  _VarType & val() const { return _v; }
125 
126 private:
127  _VarType & _v;
128 };
129 
130 template < typename _VarType >
131 inline std::ostream & operator << ( std::ostream & out, ConsoleAttrT<_VarType> const & tr )
132 {
133  tr.modify();
134  out << tr.val();
135  tr.resume();
136  return out;
137 }
138 
139 template < typename _VarType >
140 inline std::istream & operator >> ( std::istream & in, ConsoleAttrT<_VarType> const & tr )
141 {
142  tr.modify();
143  in >> tr.val();
144  tr.resume();
145  return in;
146 }
147 
148 template < typename _VarType >
149 inline std::wostream & operator << ( std::wostream & out, ConsoleAttrT<_VarType> const & tr )
150 {
151  tr.modify();
152  out << tr.val();
153  tr.resume();
154  return out;
155 }
156 
157 template < typename _VarType >
158 inline std::wistream & operator >> ( std::wistream & in, ConsoleAttrT<_VarType> const & tr )
159 {
160  tr.modify();
161  in >> tr.val();
162  tr.resume();
163  return in;
164 }
165 
167 template < typename _VarType >
168 inline ConsoleAttrT<_VarType> ConsoleColor( winux::ushort attr, _VarType const & v, bool isSetBgColor = false )
169 {
170  return ConsoleAttrT<_VarType>( attr, v, isSetBgColor );
171 }
172 
175 {
176 public:
179 
180 private:
182 };
183 
184 template < typename _ChTy >
185 inline static void OutputV( std::basic_ostream<_ChTy> & out )
186 {
187 }
188 
189 template < typename _ChTy, typename _Ty, typename... _ArgType >
190 inline static void OutputV( std::basic_ostream<_ChTy> & out, _Ty&& a, _ArgType&& ... arg )
191 {
192  out << std::forward<_Ty>(a);
193  OutputV<_ChTy>( out, std::forward<_ArgType>(arg)... );
194 }
195 
196 template < typename... _ArgType >
197 inline static void ColorOutputLine( winux::ConsoleAttr const & ca, _ArgType&& ... arg )
198 {
200  ca.modify();
201 #if defined(_UNICODE) || defined(UNICODE)
202  OutputV( std::wcout, std::forward<_ArgType>(arg)... );
203 #else
204  OutputV( std::cout, std::forward<_ArgType>(arg)... );
205 #endif
206  ca.resume();
207 #if defined(_UNICODE) || defined(UNICODE)
208  std::wcout << std::endl;
209 #else
210  std::cout << std::endl;
211 #endif
212 }
213 
214 template < typename... _ArgType >
215 inline static void ColorOutput( winux::ConsoleAttr const & ca, _ArgType&& ... arg )
216 {
218  ca.modify();
219 #if defined(_UNICODE) || defined(UNICODE)
220  OutputV( std::wcout, std::forward<_ArgType>(arg)... );
221 #else
222  OutputV( std::cout, std::forward<_ArgType>(arg)... );
223 #endif
224  ca.resume();
225 }
226 
227 // 控制台输出布局控制函数 --------------------------------------------------------------------------------
230 {
234 };
235 
237 WINUX_FUNC_DECL(String) ConsoleTextLayout( String const & text, size_t maxLength, ConsoleTextLayoutAlignment align = ctlaLeft, String::value_type blankCh = ' ', bool isTrunc = true, size_t ellipsis = 3 );
238 
239 
240 } // namespace winux
241 
242 #endif // __CONSOLE_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
ConsoleAttrT(winux::ushort attr, _VarType const &v, bool isSetBgColor=false)
Definition: console.hpp:120
控制台属性类
Definition: console.hpp:96
#define WINUX_DLL
Definition: utilities.hpp:60
void resume() const
static void ColorOutputLine(winux::ConsoleAttr const &ca, _ArgType &&...arg)
Definition: console.hpp:197
static void ColorOutput(winux::ConsoleAttr const &ca, _ArgType &&...arg)
Definition: console.hpp:215
XString< tchar > String
Definition: utilities.hpp:261
控制台属性类模板
Definition: console.hpp:117
std::istream & operator>>(std::istream &in, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:140
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
ConsoleColorAttrFlags
颜色属性标记
Definition: console.hpp:12
ConsoleAttrT< _VarType > ConsoleColor(winux::ushort attr, _VarType const &v, bool isSetBgColor=false)
控制台颜色函数
Definition: console.hpp:168
ConsoleTextLayoutAlignment
文本对齐方式
Definition: console.hpp:229
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
void modify() const
_VarType & val() const
Definition: console.hpp:124
unsigned short ushort
Definition: utilities.hpp:218
String ConsoleTextLayout(String const &text, size_t maxLength, ConsoleTextLayoutAlignment align=ctlaLeft, String::value_type blankCh= ' ', bool isTrunc=true, size_t ellipsis=3)
控制台文本布局,按指定长度输出文本。可以控制对齐方式,是否截断,截断省略号
static void OutputV(std::basic_ostream< _ChTy > &out)
Definition: console.hpp:185
控制台输出ScopeGuard
Definition: console.hpp:174
跨平台基础功能库
Definition: archives.hpp:7