4#include "../io/ansiescapecodes.h"
10#ifdef PLATFORM_WINDOWS
31 cout <<
'/' << (defaultResponse ==
Response::No ?
'N' :
'n');
36 if (line ==
"y" || line ==
"Y" || (defaultResponse ==
Response::Yes && line.empty())) {
38 }
else if (line ==
"n" || line ==
"N" || (defaultResponse ==
Response::No && line.empty())) {
41 cout <<
"Please enter [y] or [n]: ";
52 const char *envValue = std::getenv(variableName);
56 for (; *envValue; ++envValue) {
75#ifndef PLATFORM_WINDOWS
76 ioctl(STDOUT_FILENO, TIOCGWINSZ,
reinterpret_cast<winsize *
>(&size));
78 CONSOLE_SCREEN_BUFFER_INFO consoleBufferInfo;
79 if (
const HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE)) {
80 GetConsoleScreenBufferInfo(stdHandle, &consoleBufferInfo);
81 if (consoleBufferInfo.dwSize.X > 0) {
82 size.
columns =
static_cast<unsigned short>(consoleBufferInfo.dwSize.X);
84 if (consoleBufferInfo.dwSize.Y > 0) {
85 size.
rows =
static_cast<unsigned short>(consoleBufferInfo.dwSize.Y);
92#ifdef PLATFORM_WINDOWS
98static bool enableVirtualTerminalProcessing(DWORD nStdHandle)
100 auto stdHandle = GetStdHandle(nStdHandle);
101 if (stdHandle == INVALID_HANDLE_VALUE) {
104 auto dwMode = DWORD();
105 if (!GetConsoleMode(stdHandle, &dwMode)) {
108 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
109 return SetConsoleMode(stdHandle, dwMode);
116bool handleVirtualTerminalProcessing()
119 if (enableVirtualTerminalProcessing(STD_OUTPUT_HANDLE) && enableVirtualTerminalProcessing(STD_ERROR_HANDLE)) {
123 const char *
const msyscon = std::getenv(
"MSYSCON");
124 if (msyscon && std::strstr(msyscon,
"mintty")) {
127 const char *
const term = std::getenv(
"TERM");
128 if (term && std::strstr(term,
"xterm")) {
143 if (
auto *
const consoleWindow = GetConsoleWindow()) {
144 PostMessage(consoleWindow, WM_KEYUP, VK_RETURN, 0);
168 if ((!consoleEnabled.has_value() || consoleEnabled.value()) && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
170 auto stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_OUTPUT_HANDLE));
171 auto conHandle = _open_osfhandle(stdHandle, _O_TEXT);
172 auto fp = _fdopen(conHandle,
"w");
174 setvbuf(stdout,
nullptr, _IONBF, 0);
176 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_INPUT_HANDLE));
177 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
178 fp = _fdopen(conHandle,
"r");
180 setvbuf(stdin,
nullptr, _IONBF, 0);
182 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_ERROR_HANDLE));
183 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
184 fp = _fdopen(conHandle,
"w");
186 setvbuf(stderr,
nullptr, _IONBF, 0);
188 ios::sync_with_stdio(
true);
195 if (!utf8Enabled.has_value() || utf8Enabled.value()) {
196 SetConsoleCP(CP_UTF8);
197 SetConsoleOutputCP(CP_UTF8);
201 handleVirtualTerminalProcessing();
208pair<vector<unique_ptr<char[]>>, vector<char *>> convertArgsToUtf8()
210 pair<vector<unique_ptr<char[]>>, vector<char *>> res;
213 LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
214 if (!argv_w || argc <= 0) {
218 res.first.reserve(
static_cast<size_t>(argc));
219 res.second.reserve(
static_cast<size_t>(argc));
220 for (LPWSTR *
i = argv_w, *end = argv_w + argc;
i != end; ++
i) {
221 int requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1,
nullptr, 0, 0, 0);
222 if (requiredSize <= 0) {
226 auto argv = make_unique<char[]>(
static_cast<size_t>(requiredSize));
227 requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1, argv.get(), requiredSize, 0, 0);
228 if (requiredSize <= 0) {
232 res.second.emplace_back(argv.get());
233 res.first.emplace_back(move(argv));
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Contains all utilities provides 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