10#ifdef PLATFORM_WINDOWS
32 cout <<
'/' << (defaultResponse ==
Response::No ?
'N' :
'n');
37 if (line ==
"y" || line ==
"Y" || (defaultResponse ==
Response::Yes && line.empty())) {
39 }
else if (line ==
"n" || line ==
"N" || (defaultResponse ==
Response::No && line.empty())) {
42 cout <<
"Please enter [y] or [n]: ";
53 const char *envValue = std::getenv(variableName);
57 for (; *envValue; ++envValue) {
76#ifndef PLATFORM_WINDOWS
77 ioctl(STDOUT_FILENO, TIOCGWINSZ,
reinterpret_cast<winsize *
>(&size));
79 CONSOLE_SCREEN_BUFFER_INFO consoleBufferInfo;
80 if (
const HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE)) {
81 GetConsoleScreenBufferInfo(stdHandle, &consoleBufferInfo);
82 if (consoleBufferInfo.dwSize.X > 0) {
83 size.
columns =
static_cast<unsigned short>(consoleBufferInfo.dwSize.X);
85 if (consoleBufferInfo.dwSize.Y > 0) {
86 size.
rows =
static_cast<unsigned short>(consoleBufferInfo.dwSize.Y);
93#ifdef PLATFORM_WINDOWS
99 static const auto mintty = [] {
100 const char *
const msyscon = std::getenv(
"MSYSCON");
101 const char *
const termprog = std::getenv(
"TERM_PROGRAM");
102 return (msyscon && std::strstr(msyscon,
"mintty")) || (termprog && std::strstr(termprog,
"mintty"));
112static bool enableVirtualTerminalProcessing(DWORD nStdHandle)
114 auto stdHandle = GetStdHandle(nStdHandle);
115 if (stdHandle == INVALID_HANDLE_VALUE) {
118 auto dwMode = DWORD();
119 if (!GetConsoleMode(stdHandle, &dwMode)) {
122 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
123 return SetConsoleMode(stdHandle, dwMode);
130bool handleVirtualTerminalProcessing()
133 if (enableVirtualTerminalProcessing(STD_OUTPUT_HANDLE) && enableVirtualTerminalProcessing(STD_ERROR_HANDLE)) {
140 if (
const char *
const term = std::getenv(
"TERM"); term && std::strstr(term,
"xterm")) {
155 if (
auto *
const consoleWindow = GetConsoleWindow()) {
185 if (
const auto e =
isEnvVariableSet(
"ENABLE_CONSOLE"); !e.has_value() || !e.value()) {
190 auto pos = std::fpos_t();
191 std::fgetpos(stdout, &pos);
192 const auto skipstdout = pos >= 0;
193 std::fgetpos(stderr, &pos);
194 const auto skipstderr = pos >= 0;
195 std::fgetpos(stdin, &pos);
196 const auto skipstdin = pos >= 0;
197 const auto skip = skipstdout || skipstderr || skipstdin;
200 if (!skip && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
205 freopen_s(&fp,
"CONOUT$",
"w", stdout);
210 freopen_s(&fp,
"CONOUT$",
"w", stderr);
214 freopen_s(&fp,
"CONIN$",
"r", stdin);
218 auto hConOut = CreateFile(
219 _T(
"CONOUT$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
220 auto hConIn = CreateFile(
221 _T(
"CONIN$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
223 SetStdHandle(STD_OUTPUT_HANDLE, hConOut);
228 SetStdHandle(STD_ERROR_HANDLE, hConOut);
232 SetStdHandle(STD_INPUT_HANDLE, hConIn);
237 auto stdHandle = std::intptr_t();
238 auto conHandle = int();
240 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_OUTPUT_HANDLE));
241 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
242 fp = _fdopen(conHandle,
"w");
244 setvbuf(stdout,
nullptr, _IONBF, 0);
248 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_INPUT_HANDLE));
249 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
250 fp = _fdopen(conHandle,
"r");
252 setvbuf(stdin,
nullptr, _IONBF, 0);
256 stdHandle =
reinterpret_cast<intptr_t
>(GetStdHandle(STD_ERROR_HANDLE));
257 conHandle = _open_osfhandle(stdHandle, _O_TEXT);
258 fp = _fdopen(conHandle,
"w");
260 setvbuf(stderr,
nullptr, _IONBF, 0);
263 ios::sync_with_stdio(
true);
266 std::atexit(stopConsole);
270 if (
const auto e =
isEnvVariableSet(
"ENABLE_CP_UTF8"); !e.has_value() || e.value()) {
271 SetConsoleCP(CP_UTF8);
272 SetConsoleOutputCP(CP_UTF8);
276 if (
const auto e =
isEnvVariableSet(
"ENABLE_HANDLING_VIRTUAL_TERMINAL_PROCESSING"); !e.has_value() || e.value()) {
277 handleVirtualTerminalProcessing();
285pair<vector<unique_ptr<char[]>>, vector<char *>> convertArgsToUtf8()
287 pair<vector<unique_ptr<char[]>>, vector<char *>> res;
290 LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
291 if (!argv_w || argc <= 0) {
295 res.first.reserve(
static_cast<size_t>(argc));
296 res.second.reserve(
static_cast<size_t>(argc));
297 for (LPWSTR *
i = argv_w, *end = argv_w + argc;
i != end; ++
i) {
298 int requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1,
nullptr, 0, 0, 0);
299 if (requiredSize <= 0) {
303 auto argv = make_unique<char[]>(
static_cast<size_t>(requiredSize));
304 requiredSize = WideCharToMultiByte(CP_UTF8, 0, *
i, -1, argv.get(), requiredSize, 0, 0);
305 if (requiredSize <= 0) {
309 res.second.emplace_back(argv.get());
310 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 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