mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2026-07-04 12:26:58 +03:00
[Backport release-10.x] refactor(console): attach console early (#4998)
refactor(console): attach console early
also use RAII guard to free it instead of tracking it with a member variable
(cherry picked from commit ffd50e318a)
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
parent
8d8220283b
commit
b178c1e84d
10 changed files with 79 additions and 84 deletions
|
|
@ -24,12 +24,16 @@
|
|||
#endif
|
||||
#include <windows.h>
|
||||
|
||||
#include <consoleapi.h>
|
||||
#include <fcntl.h>
|
||||
#include <fileapi.h>
|
||||
#include <io.h>
|
||||
#include <stdio.h>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
|
||||
namespace console {
|
||||
|
||||
void RedirectHandle(DWORD handle, FILE* stream, const char* mode)
|
||||
{
|
||||
HANDLE stdHandle = GetStdHandle(handle);
|
||||
|
|
@ -157,3 +161,31 @@ std::error_code EnableAnsiSupport()
|
|||
|
||||
return {};
|
||||
}
|
||||
|
||||
void FreeWindowsConsole()
|
||||
{
|
||||
fclose(stdout);
|
||||
fclose(stdin);
|
||||
fclose(stderr);
|
||||
FreeConsole();
|
||||
}
|
||||
|
||||
WindowsConsoleGuard::WindowsConsoleGuard() : m_consoleAttached(false)
|
||||
{
|
||||
if (console::AttachWindowsConsole()) {
|
||||
m_consoleAttached = true;
|
||||
if (auto err = console::EnableAnsiSupport(); err) {
|
||||
std::cout << "Error setting up ansi console" << err.message() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WindowsConsoleGuard::~WindowsConsoleGuard()
|
||||
{
|
||||
// Detach from Windows console
|
||||
if (m_consoleAttached) {
|
||||
console::FreeWindowsConsole();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace console
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue