11#ifdef PLATFORM_WINDOWS
34 cout <<
'/' << (defaultResponse ==
Response::No ?
'N' :
'n');
39 if (line ==
"y" || line ==
"Y" || (defaultResponse ==
Response::Yes && line.empty())) {
41 }
else if (line ==
"n" || line ==
"N" || (defaultResponse ==
Response::No && line.empty())) {
44 cout <<
"Please enter [y] or [n]: ";
55 const char *envValue = std::getenv(variableName);
59 for (; *envValue; ++envValue) {
78#ifndef PLATFORM_WINDOWS
79 ioctl(STDOUT_FILENO, TIOCGWINSZ,
reinterpret_cast<winsize *
>(&size));
81 CONSOLE_SCREEN_BUFFER_INFO consoleBufferInfo;
82 if (
const HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE)) {
83 GetConsoleScreenBufferInfo(stdHandle, &consoleBufferInfo);
84 if (consoleBufferInfo.dwSize.X > 0) {
85 size.
columns =
static_cast<unsigned short>(consoleBufferInfo.dwSize.X);
87 if (consoleBufferInfo.dwSize.Y > 0) {
88 size.
rows =
static_cast<unsigned short>(consoleBufferInfo.dwSize.Y);
95#ifdef PLATFORM_WINDOWS
101 static const auto mintty = [] {
102 const char *
const msyscon = std::getenv(
"MSYSCON");
103 const char *
const termprog = std::getenv(
"TERM_PROGRAM");
104 return (msyscon && std::strstr(msyscon,
"mintty")) || (termprog && std::strstr(termprog,
"mintty"));
114static bool enableVirtualTerminalProcessing(DWORD nStdHandle)
116 auto stdHandle = GetStdHandle(nStdHandle);
117 if (stdHandle == INVALID_HANDLE_VALUE) {
120 auto dwMode = DWORD();
121 if (!GetConsoleMode(stdHandle, &dwMode)) {
124 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
125 return SetConsoleMode(stdHandle, dwMode);
132bool handleVirtualTerminalProcessing()
135 if (enableVirtualTerminalProcessing(STD_OUTPUT_HANDLE) && enableVirtualTerminalProcessing(STD_ERROR_HANDLE)) {
142 if (
const char *
const term = std::getenv(
"TERM"); term && std::strstr(term,
"xterm")) {
157 if (GetConsoleWindow()) {
187 if (
const auto e =
isEnvVariableSet(
"ENABLE_CONSOLE"); !e.has_value() || !e.value()) {
192 auto pos = std::fpos_t();
193 std::fgetpos(stdout, &pos);
194 const auto skipstdout = pos >= 0;
195 std::fgetpos(stderr, &pos);
196 const auto skipstderr = pos >= 0;
197 std::fgetpos(stdin, &pos);
198 const auto skipstdin = pos >= 0;
199 const auto skip = skipstdout || skipstderr || skipstdin;
202 if (!skip && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
207 freopen_s(&fp,
"CONOUT$",
"w", stdout);
212 freopen_s(&fp,
"CONOUT$",
"w", stderr);
216 freopen_s(&fp,
"CONIN$",
"r", stdin);
220 auto hConOut = CreateFile(
221 _T(
"CONOUT$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
222 auto hConIn = CreateFile(
223 _T(
"CONIN$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
225 SetStdHandle(STD_OUTPUT_HANDLE, hConOut);
230 SetStdHandle(STD_ERROR_HANDLE, hConOut);
234 SetStdHandle(STD_INPUT_HANDLE, hConIn);
239 auto stdHandle = std::intptr_t();
240 auto conHandle = int();
242 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_OUTPUT_HANDLE));
243 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
244 fp = _fdopen(conHandle,
"w");
246 setvbuf(stdout,
nullptr, _IONBF, 0);
250 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_INPUT_HANDLE));
251 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
252 fp = _fdopen(conHandle,
"r");
254 setvbuf(stdin,
nullptr, _IONBF, 0);
258 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_ERROR_HANDLE));
259 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
260 fp = _fdopen(conHandle,
"w");
262 setvbuf(stderr,
nullptr, _IONBF, 0);
265 ios::sync_with_stdio(
true);
268 std::atexit(stopConsole);
272 if (
const auto e =
isEnvVariableSet(
"ENABLE_CP_UTF8"); !e.has_value() || e.value()) {
273 SetConsoleCP(CP_UTF8);
274 SetConsoleOutputCP(CP_UTF8);
278 if (
const auto e =
isEnvVariableSet(
"ENABLE_HANDLING_VIRTUAL_TERMINAL_PROCESSING"); !e.has_value() || e.value()) {
279 handleVirtualTerminalProcessing();
287pair<vector<unique_ptr<char[]>>, vector<char *>> convertArgsToUtf8()
289 pair<vector<unique_ptr<char[]>>, vector<char *>> res;
292 LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
293 if (!argv_w || argc <= 0) {
297 res.first.reserve(
static_cast<size_t>(argc));
298 res.second.reserve(
static_cast<size_t>(argc));
299 for (LPWSTR *
i = argv_w, *end = argv_w + argc;
i != end; ++
i) {
300 int requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1,
nullptr, 0, 0, 0);
301 if (requiredSize <= 0) {
305 auto argv = make_unique<char[]>(
static_cast<size_t>(requiredSize));
306 requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1, argv.get(), requiredSize, 0, 0);
307 if (requiredSize <= 0) {
311 res.second.emplace_back(argv.get());
312 res.first.emplace_back(std::move(argv));
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Contains all utilities provided by the c++utilities library.
CPP_UTILITIES_EXPORT TerminalSize determineTerminalSize()
Returns the current size of the terminal.
Response
The Response enum is used to specify the default response for the confirmPrompt() method.
CPP_UTILITIES_EXPORT bool confirmPrompt(const char *message, Response defaultResponse=Response::None)
Prompts for confirmation displaying the specified message.
CPP_UTILITIES_EXPORT std::optional< bool > isEnvVariableSet(const char *variableName)
Returns whether the specified env variable is set to a non-zero and non-white-space-only value.
The TerminalSize struct describes a terminal size.
unsigned short columns
number of columns
unsigned short rows
number of rows