4#include "../io/ansiescapecodes.h"
9#ifndef PLATFORM_WINDOWS
30 cout <<
'/' << (defaultResponse ==
Response::No ?
'N' :
'n');
35 if (line ==
"y" || line ==
"Y" || (defaultResponse ==
Response::Yes && line.empty())) {
37 }
else if (line ==
"n" || line ==
"N" || (defaultResponse ==
Response::No && line.empty())) {
40 cout <<
"Please enter [y] or [n]: ";
51 const char *envValue = std::getenv(variableName);
55 for (; *envValue; ++envValue) {
74#ifndef PLATFORM_WINDOWS
75 ioctl(STDOUT_FILENO, TIOCGWINSZ,
reinterpret_cast<winsize *
>(&size));
77 CONSOLE_SCREEN_BUFFER_INFO consoleBufferInfo;
78 if (
const HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE)) {
79 GetConsoleScreenBufferInfo(stdHandle, &consoleBufferInfo);
80 if (consoleBufferInfo.dwSize.X > 0) {
81 size.
columns =
static_cast<unsigned short>(consoleBufferInfo.dwSize.X);
83 if (consoleBufferInfo.dwSize.Y > 0) {
84 size.
rows =
static_cast<unsigned short>(consoleBufferInfo.dwSize.Y);
91#ifdef PLATFORM_WINDOWS
97static bool enableVirtualTerminalProcessing(DWORD nStdHandle)
99 auto stdHandle = GetStdHandle(nStdHandle);
100 if (stdHandle == INVALID_HANDLE_VALUE) {
103 auto dwMode = DWORD();
104 if (!GetConsoleMode(stdHandle, &dwMode)) {
107 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
108 return SetConsoleMode(stdHandle, dwMode);
115bool handleVirtualTerminalProcessing()
118 if (enableVirtualTerminalProcessing(STD_OUTPUT_HANDLE) && enableVirtualTerminalProcessing(STD_ERROR_HANDLE)) {
122 const char *
const msyscon = std::getenv(
"MSYSCON");
123 if (msyscon && std::strstr(msyscon,
"mintty")) {
126 const char *
const term = std::getenv(
"TERM");
127 if (term && std::strstr(term,
"xterm")) {
142 if (
auto *
const consoleWindow = GetConsoleWindow()) {
143 PostMessage(consoleWindow, WM_KEYUP, VK_RETURN, 0);
167 if ((!consoleEnabled.has_value() || consoleEnabled.value()) && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
169 auto stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_OUTPUT_HANDLE));
170 auto conHandle = _open_osfhandle(stdHandle, _O_TEXT);
171 auto fp = _fdopen(conHandle,
"w");
173 setvbuf(stdout,
nullptr, _IONBF, 0);
175 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_INPUT_HANDLE));
176 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
177 fp = _fdopen(conHandle,
"r");
179 setvbuf(stdin,
nullptr, _IONBF, 0);
181 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_ERROR_HANDLE));
182 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
183 fp = _fdopen(conHandle,
"w");
185 setvbuf(stderr,
nullptr, _IONBF, 0);
187 ios::sync_with_stdio(
true);
194 if (!utf8Enabled.has_value() || utf8Enabled.value()) {
195 SetConsoleCP(CP_UTF8);
196 SetConsoleOutputCP(CP_UTF8);
200 handleVirtualTerminalProcessing();
207pair<vector<unique_ptr<char[]>>, vector<char *>> convertArgsToUtf8()
209 pair<vector<unique_ptr<char[]>>, vector<char *>> res;
212 LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
213 if (!argv_w || argc <= 0) {
217 res.first.reserve(
static_cast<size_t>(argc));
218 res.second.reserve(
static_cast<size_t>(argc));
219 for (LPWSTR *
i = argv_w, *end = argv_w + argc;
i != end; ++
i) {
220 int requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1,
nullptr, 0, 0, 0);
221 if (requiredSize <= 0) {
225 auto argv = make_unique<char[]>(
static_cast<size_t>(requiredSize));
226 requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1, argv.get(), requiredSize, 0, 0);
227 if (requiredSize <= 0) {
231 res.second.emplace_back(argv.get());
232 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