YSTest: Consoles.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Consoles.cpp
浏览该文件的文档.
1 /*
2 © 2013-2016, 2020 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
29 #include "YCLib/YModules.h"
30 #include YFM_YCLib_Platform
31 #if YCL_Win32
32 # include YFM_Win32_YCLib_Consoles // for UTF8ToWCS;
33 # include YFM_YSLib_Core_YCoreUtilities // for YSLib::CheckPositive;
34 # include YFM_Win32_YCLib_NLS // for platform_ex::MBCSToWCS;
35
36 using namespace YSLib;
37 #endif
38
39 namespace platform_ex
40 {
41
42 #if YCL_Win32
43
44 inline namespace Windows
45 {
46
48 namespace
49 {
50
52 YB_PURE int WINAPI
53 ConsoleHandler(unsigned long ctrl)
54 {
55 switch(ctrl)
56 {
57 case CTRL_C_EVENT:
58 case CTRL_BREAK_EVENT:
59 case CTRL_CLOSE_EVENT:
60 case CTRL_LOGOFF_EVENT:
61 case CTRL_SHUTDOWN_EVENT:
62 std::_Exit(int(STATUS_CONTROL_C_EXIT));
63 }
64 return 0;
65 }
66
67 } // unnamed namespace;
68
69 void
70 FixConsoleHandler(int(WINAPI* handler)(unsigned long), bool add)
71 {
72 YCL_CallF_Win32(SetConsoleCtrlHandler, handler ? handler : ConsoleHandler,
73 add);
74 }
75
76
77 WConsole::WConsole(unsigned long dev)
78 : WConsole(::GetStdHandle(dev))
79 {}
80 WConsole::WConsole(::HANDLE h)
81 : h_std(h), saved_attr(GetScreenBufferInfo().wAttributes),
82 Attributes(saved_attr)
83 {
84 YAssert(h_std && h_std != INVALID_HANDLE_VALUE, "Invalid handle found.");
85 }
86 WConsole::~WConsole()
87 {
88 FilterExceptions(std::bind(&WConsole::RestoreAttributes, this), yfsig);
89 }
90
91 ::CONSOLE_SCREEN_BUFFER_INFO
92 WConsole::GetScreenBufferInfo() const
93 {
94 ::CONSOLE_SCREEN_BUFFER_INFO info;
95
96 YCL_CallF_Win32(GetConsoleScreenBufferInfo, h_std, &info);
97 return info;
98 }
99
100 void
101 WConsole::SetSystemColor(std::uint8_t color)
102 {
103 char cmd[9];
104
105 std::sprintf(cmd, "COLOR %02x", int(color));
106 std::system(cmd);
107 }
108
109 void
110 WConsole::SetBackColor(std::uint8_t bc) ynothrow
111 {
112 Attributes = ComposeAttributes(FetchForeColor(Attributes), bc);
113 }
114 void
115 WConsole::SetCursorPosition(::COORD pos)
116 {
117 // NOTE: %::SetConsoleCursorPosition expects 1-based.
118 YCL_CallF_Win32(SetConsoleCursorPosition, h_std,
119 {short(pos.X + 1), short(pos.Y + 1)});
120 }
121 void
122 WConsole::SetForeColor(std::uint8_t fc) ynothrow
123 {
124 Attributes = ComposeAttributes(fc, FetchBackColor(Attributes));
125 }
126
127 ::WORD
128 WConsole::ComposeAttributes(std::uint8_t fore, std::uint8_t back) ynothrow
129 {
130 return (fore & 15) | ((back & 15) << 4);
131 }
132
133 void
134 WConsole::CursorUp(size_t num_rows)
135 {
136 if(num_rows != 0)
137 {
138 const auto pos(GetCursorPosition());
139
140 // XXX: Conversion to 'short' might be implementation-defined.
141 SetCursorPosition({short(pos.Y - short(num_rows)), pos.X});
142 }
143 }
144
145 void
146 WConsole::Erase(wchar_t c)
147 {
148 const auto size(GetScreenBufferInfo().dwSize);
149
150 Fill({short(), short()}, CheckPositive<unsigned long>(size.X)
151 * CheckPositive<unsigned long>(size.Y), c);
152 }
153
154 void
155 WConsole::Fill(::COORD coord, unsigned long n, wchar_t c)
156 {
157 YCL_CallF_Win32(FillConsoleOutputCharacterW, h_std, c, n, coord, {});
158 YCL_CallF_Win32(FillConsoleOutputAttribute, h_std, Attributes, n, coord, {});
159 YCL_CallF_Win32(SetConsoleCursorPosition, h_std, {coord.X, coord.Y});
160 }
161
162 void
163 WConsole::RestoreAttributes()
164 {
165 // SetColor();
166 Update(GetSavedAttributes());
167 }
168
169 void
170 WConsole::Update()
171 {
172 Update(Attributes);
173 }
174 void
175 WConsole::Update(::WORD value)
176 {
177 YCL_CallF_Win32(SetConsoleTextAttribute, h_std, value);
178 }
179
180 void
181 WConsole::UpdateBackColor(std::uint8_t fc)
182 {
183 SetBackColor(fc);
184 Update();
185 }
186
187 void
188 WConsole::UpdateForeColor(std::uint8_t fc)
189 {
190 SetForeColor(fc);
191 Update();
192 }
193
194 size_t
195 WConsole::WriteString(string_view sv)
196 {
197 YAssertNonnull(sv.data());
198 // FIXME: Support for non-BMP characters.
199 return WriteString(UTF8ToWCS(sv));
200 }
201 size_t
202 WConsole::WriteString(string_view sv, unsigned cp)
203 {
204 YAssertNonnull(sv.data());
205 return WriteString(MBCSToWCS(sv, cp));
206 }
207 size_t
208 WConsole::WriteString(wstring_view sv)
209 {
210 YAssertNonnull(sv.data());
211
212 unsigned long n;
213
214 YCL_CallF_Win32(WriteConsoleW, h_std, sv.data(),
215 static_cast<unsigned long>(sv.length()), &n, yimpl({}));
216 return size_t(n);
217 }
218
219 unique_ptr<WConsole>
220 MakeWConsole(unsigned long h)
221 {
222 unique_ptr<WConsole> p_con;
223
224 TryExpr(p_con.reset(new WConsole(h)))
225 CatchIgnore(Win32Exception&)
226 return p_con;
227 }
228
229 } // inline namespace Windows;
230
231 #endif
232
233 } // namespace platform_ex;
234
size
size_t size
Definition: memory_resource.cpp:295
platform_ex::Windows::WConsole::UpdateBackColor
void UpdateBackColor(std::uint8_t)
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
CHRLib::c
c
Definition: CharacterProcessing.h:84
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
platform_ex::Windows::WConsole::Erase
void Erase(wchar_t=L' ')
platform_ex
平台扩展命名空间。
Definition: Android.h:46
yfsig
#define yfsig
展开为表示函数签名的预定义变量的宏。
Definition: ydef.h:1133
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
ystdex::cpp2017::wstring_view
basic_string_view< wchar_t > wstring_view
Definition: string_view.hpp:440
platform::size_t
YB_ATTR_nodiscard YF_API size_t
Definition: FileIO.h:1345
YSLib::UI::fc
fc
Definition: Console.h:93
platform_ex::Windows::FixConsoleHandler
YF_API void FixConsoleHandler(int(WINAPI *)(unsigned long)={}, bool=true)
安装控制台处理器。
platform_ex::Windows::WConsole::h_std
::HANDLE h_std
Definition: Consoles.h:86
platform_ex::Windows::WConsole::UpdateForeColor
void UpdateForeColor(std::uint8_t)
platform_ex::Windows::WConsole::Attributes
::WORD Attributes
Definition: Consoles.h:92
YSLib::Drawing::Fill
YF_API void Fill(const Graphics &, Color)
使用指定颜色填充图形接口上下文缓冲区。
Definition: YBlit.cpp:93
platform_ex::Windows::WConsole::Update
void Update()
platform_ex::Windows::UTF8ToWCS
UTF8ToWCS
Definition: NLS.h:72
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
platform_ex::Windows::WConsole::SetForeColor
void SetForeColor(std::uint8_t) ynothrow
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:165
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:225
YCL_CallF_Win32
#define YCL_CallF_Win32(_fn,...)
Definition: Win32/include/YCLib/MinGW32.h:199
platform_ex::Windows::WConsole::WConsole
WConsole(unsigned long h=STD_OUTPUT_HANDLE)
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
platform_ex::Windows::WConsole::WriteString
size_t WriteString(string_view)
输出字符串。
platform_ex::Windows::MBCSToWCS
YF_API wstring MBCSToWCS(string_view, unsigned=CP_ACP)
YModules.h
YCLib 库模块配置文件。
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120